# n, m = map(int, input().split()) # c = list(map(int, input().split())) # cnt = [set() for _ in range(n)] # for i in range(n): # cnt[c[i]-1].add(i) # s = set() # edge = [0]*n # for _ in range(m): # v, u = map(int, input().split()) # v -= 1 # u -= 1 # if c[v] == c[u]: # if (v, u) not in s and (u, v) not in s: # edge[c[v]-1] += 1 # s.add((v, u)) # s.add((u, v)) # ans = 0 # for e, t in zip(edge, cnt): # if len(t) == 0: # continue # ans += max(len(t)-1-e, 0) # print(ans) n, m = map(int, input().split()) g = [[] for _ in range(n)] for _ in range(m): v, u = map(int, input().split()) v -= 1 u -= 1 g[v].append(u) from collections import deque def culc_dist(st: int, g: list): dist = [-1]*n q = deque() q.append(st) dist[st] = 0 while q: v = q.popleft() for nv in g[v]: if dist[nv] == -1: dist[nv] = dist[v] + 1 q.append(nv) return dist dist1 = culc_dist(0, g) dist2 = culc_dist(n-2, g) dist3 = culc_dist(n-1, g) if (dist1[n-2] == -1 or dist2[n-1] == -1 or dist3[0] == -1) and (dist3[n-3] == -1 or dist1[n-1] == -1 or dist2[0] == -1): print(-1) elif dist1[n-2] == -1 or dist2[n-1] == -1 or dist3[0] == -1: print(dist1[n-1]+dist3[n-2]+dist2[0]) elif dist3[n-3] == -1 or dist1[n-1] == -1 or dist2[0] == -1: print(dist1[n-2]+dist2[n-1]+dist3[0]) else: print(min(dist1[n-2]+dist2[n-1]+dist3[0], dist1[n-1]+dist3[n-2]+dist2[0]))