from collections import deque def bfs(s): dist = [INF] * N dist[s] = 0 que = deque([s]) while que: v = que.popleft() for nv in g[v]: if dist[nv] == INF: dist[nv] = dist[v] + 1 que.append(nv) return dist INF = 1 << 60 N, M = map(int, input().split()) g = [[] for _ in range(N)] for _ in range(M): u, v = map(int, input().split()) u, v = u - 1, v - 1 g[u].append(v) dist_1 = bfs(0) dist_N = bfs(N - 1) dist_Nm1 = bfs(N - 2) ans = min(dist_1[-1] + dist_N[N - 2] + dist_Nm1[0], dist_1[N - 2] + dist_Nm1[-1] + dist_N[0]) print(ans if ans < INF else -1)