import sys
from collections import deque
n = int(sys.stdin.readline().rstrip())
start , end = map(int , sys.stdin.readline().rstrip().split(" "))
m = int(sys.stdin.readline().rstrip())
edge = [[] for _ in range(n+1)]
for i in range(m):
a, b= map(int, sys.stdin.readline().rstrip().split(" "))
edge[a].append(b)
edge[b].append(a)
def BFS():
queue = deque([[start , 0]])
visit = [False] * (n+1)
visit[start] =True
while queue:
pop = queue.popleft()
for i in edge[pop[0]]:
if(visit[i] == False):
visit[i] = True
queue.append([i , pop[1]+1])
if(i == end):
return(pop[1]+1)
return -1
print(BFS())
파이썬에 점점 익숙해져간다