https://www.acmicpc.net/problem/2644
import sys
from collections import deque
input = sys.stdin.readline
n = int(input())
a, b = map(int, input().split())
graph = [[]for _ in range(n+1)]
visited = [-1 for _ in range(n+1)]
m = int(input())
for _ in range(m):
i, j = map(int, input().split())
graph[i].append(j)
graph[j].append(i)
que = deque()
que.append(a)
visited[a] = 0
flag = False
while que:
node = que.popleft()
if node == b:
print(visited[b])
flag = True
break
for next in graph[node]:
if visited[next] == -1:
visited[next] = visited[node] + 1
que.append(next)
if flag == False:
print(-1)