n,m=map(int,input().split()) g=[[] for _ in range(n+1)] from collections import deque for _ in range(m): a,b=map(int,input().split()) g[a].append(b) g[b].append(a) d=[-1]*(n+1) d[1]=0 q=deque() q.append(1) while q: now=q.popleft() for to in g[now]: if d[to]==-1: d[to]=d[now]+1 q.append(to) print(d[n])