from collections import deque N,M = map(int,input().split()) G = {i:[] for i in range(1,N+1)} for _ in range(M): a,b = map(int,input().split()) G[a].append(b) INFTY = 10**5+100 dist1 = [INFTY]*(N+1) dist1[1] = 0 que = deque([1]) while que: x = que.popleft() for y in G[x]: if dist1[y]>=INFTY: dist1[y] = dist1[x]+1 que.append(y) dist2 = [INFTY]*(N+1) dist2[N-1] = 0 que = deque([N-1]) while que: x = que.popleft() for y in G[x]: if dist2[y]>=INFTY: dist2[y] = dist2[x]+1 que.append(y) dist3 = [INFTY]*(N+1) dist3[N] = 0 que = deque([N]) while que: x = que.popleft() for y in G[x]: if dist3[y]>=INFTY: dist3[y] = dist3[x]+1 que.append(y) ans = dist1[N-1]+dist2[N]+dist3[1] ans = min(ans,dist1[N]+dist3[N-1]+dist2[1]) if ans>=INFTY: print(-1) else: print(ans)