@@ -0,0 +1,842 @@
+#Uses python3%0A%0Aimport sys%0Aimport queue%0A%0Adef BFS(adj, s):%0A    dist = %5B-1%5D * len(adj)%0A    dist%5Bs%5D = 0%0A    q = queue.Queue()%0A    q.put(s)%0A    while not q.empty():%0A        u = q.get()%0A        for v in adj%5Bu%5D:%0A            if dist%5Bv%5D == -1:%0A                q.put(v)%0A                dist%5Bv%5D = dist%5Bu%5D + 1%0A    return dist%0A%0Adef distance(adj, s, t):%0A    dist = BFS(adj, s)%0A    return dist%5Bt%5D%0A%0Aif __name__ == '__main__':%0A    #input = sys.stdin.read()%0A    with open('test','r') as f:%0A        input = f.read()%0A    data = list(map(int, input.split()))%0A    n, m = data%5B0:2%5D%0A    data = data%5B2:%5D%0A    edges = list(zip(data%5B0:(2 * m):2%5D, data%5B1:(2 * m):2%5D))%0A    adj = %5B%5B%5D for _ in range(n)%5D%0A    for (a, b) in edges:%0A        adj%5Ba - 1%5D.append(b - 1)%0A        adj%5Bb - 1%5D.append(a - 1)%0A    s, t = data%5B2 * m%5D - 1, data%5B2 * m + 1%5D - 1%0A    print(distance(adj, s, t))%0A

Create bfs.py