from collections import deque INF = 10 ** 17 N, M = map(int, input().split()) graph = [[] for _ in range(N)] for i in range(M): u, v = map(int, input().split()) graph[u - 1].append(v - 1) def bfs(v): dq = deque() isvisited = [False for _ in range(N)] dist = [INF for _ in range(N)] isvisited[v] = True dist[v] = 0 dq.append(v) while len(dq): p = dq.popleft() for x in graph[p]: if isvisited[x]: continue isvisited[x] = True dq.append(x) dist[x] = dist[p] + 1 return dist dist1 = bfs(0) distn1 = bfs(N - 2) distn = bfs(N - 1) ans = min(dist1[N - 2] + distn1[N - 1] + distn[0], dist1[N - 1] + distn[N - 2] + distn1[0]) if ans >= INF: print(-1) else: print(ans)