from collections import deque 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) def culc_dist(st: int, g: list): dist = [1<<60]*n q = deque() q.append(st) dist[st] = 0 while q: v = q.popleft() for nv in g[v]: if dist[nv] == 1<<60: 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) ans1 = dist1[n-2]+dist2[n-1]+dist3[0] ans2 = dist1[n-1]+dist3[n-2]+dist2[0] ans = min(ans1, ans2) print(ans if ans < 1<<60 else -1)