from collections import deque
n = int(input())
a, b = map(int, input().split())
m = int(input())
graph = [[] for _ in range(n + 1)]
for i in range(m):
x, y = map(int, input().split())
graph[x].append(y)
graph[y].append(x)
vis = [0] * (n + 1)
dq = deque([a])
vis[a] = 1
while dq:
cur = dq.popleft()
for next in graph[cur]:
if vis[next] == 0:
vis[next] = vis[cur] + 1
dq.append(next)
print(vis[b] - 1)
To embed this project on your website, copy the following code and paste it into your website's HTML: