from collections import deque N,M=map(int,input().split()) E=[[] for i in range(N+1)] for i in range(M): x,y=map(int,input().split()) E[x].append(y) def distance_from(x): DIS=[1<<30]*(N+1) DIS[x]=0 Q=deque([x]) while Q: x=Q.popleft() for to in E[x]: if DIS[to]>DIS[x]+1: DIS[to]=DIS[x]+1 Q.append(to) return DIS ONE=distance_from(1) DN=distance_from(N-1) DN1=distance_from(N) ANS=ONE[N]+DN1[N-1]+DN[1] ANS2=ONE[N-1]+DN[N]+DN1[1] LA=min(ANS,ANS2) if LA>10**7: print(-1) else: print(LA)