from collections import deque INF = float("inf") def bfs(start, edge): #始点,edge _N = len(edge) dist = [INF]*_N dist[start] = 0 deq = deque() deq.append(start) while len(deq) > 0: crr = deq.popleft() for nxt in edge[crr]: if dist[nxt] == INF: dist[nxt] = dist[crr] + 1 deq.append(nxt) return dist #-------------------------------------------------- N,M = map(int,input().split()) E = [[] for _ in range(N)] for _ in range(M): u,v = map(int,input().split()) E[u-1].append(v-1) b1 = bfs(0,E) b2 = bfs(N-2,E) b3 = bfs(N-1,E) r1 = b1[N-2] + b2[N-1] + b3[0] r2 = b1[N-1] + b3[N-2] + b2[0] r3 = b1[N-2] + b2[0] + b1[N-1] + b3[0] ans = min(r1,r2,r3) if ans == INF: print(-1) else: print(ans)