from collections import deque def main(): N,M = map(int,input().split()) G = [[] for _ in range(N)] for _ in range(M): a,b = map(int,input().split()) a-=1;b-=1 G[a].append(b) G[b].append(a) Q = deque([]) Q.append(0) d = [-1]*N d[0] = 0 while Q: v = Q.popleft() for u in G[v]: if d[u] != -1: continue d[u] = d[v] + 1 Q.append(u) print(d[N-1]) if __name__ == '__main__': main()