from collections import deque n, m = map(int, input().split()) g = [[] for _ in range(n)] for i in range(m): u, v = map(lambda x: x-1, map(int, input().split())) g[u].append(v) q = deque([0]) dist = [-1]*n dist[0] = 0 while q: v = q.popleft() for v2 in g[v]: if dist[v2]!=-1: continue dist[v2] = dist[v] + 1 q.append(v2) if dist[n-2]==-1 and dist[n-1]==-1: exit(print('No')) # 町nから q = deque([n-1]) dist2 = [-1]*n dist2[n-1] = 0 while q: v = q.popleft() for v2 in g[v]: if dist2[v2]!=-1: continue dist2[v2] = dist2[v] + 1 q.append(v2) # 町n-1から q = deque([n-2]) dist3 = [-1]*n dist3[n-2] = 0 while q: v = q.popleft() for v2 in g[v]: if dist3[v2]!=-1: continue dist3[v2] = dist3[v] + 1 q.append(v2) ans = 10**18 if dist[n-2]!=-1 and dist[n-1]==-1: if dist3[n-1]!=-1 and dist2[0]!=-1: ans = min(ans, dist[n-2] + dist3[n-1] + dist2[0]) if dist3[n-1]!=-1 and dist2[n-2]!=-1 and dist3[0]!=-1: ans = min(ans, dist[n-2]+dist3[n-1]+dist2[n-2]+dist3[0]) elif dist[n-2]==-1 and dist[n-1]!=-1: if dist2[n-2]!=-1 and dist3[0]!=-1: ans = min(ans, dist[n-1]+dist2[n-2]+dist3[0]) if dist2[n-2]!=-1 and dist3[n-1]!=-1 and dist2[0]!=-1: ans = min(ans, dist[n-1]+dist2[n-2]+dist3[n-1]+dist2[0]) else: if dist3[n-1]!=-1 and dist2[0]!=-1: ans = min(ans, dist[n-2] + dist3[n-1] + dist2[0]) if dist3[n-1]!=-1 and dist2[n-2]!=-1 and dist3[0]!=-1: ans = min(ans, dist[n-2]+dist3[n-1]+dist2[n-2]+dist3[0]) if dist2[n-2]!=-1 and dist3[0]!=-1: ans = min(ans, dist[n-1]+dist2[n-2]+dist3[0]) if dist2[n-2]!=-1 and dist3[n-1]!=-1 and dist2[0]!=-1: ans = min(ans, dist[n-1]+dist2[n-2]+dist3[n-1]+dist2[0]) if ans==10**18: print(-1) else: print(ans)