N,M = map(int,input().split()) G = [[] for i in range(N)] for _ in range(M): u,v = map(int,input().split()) G[u - 1].append(v - 1) from collections import deque def get_dist(start, goal, G, N): q = deque([]) q.append([start, 0]) visited = [False] * N visited[start] = True while q: v, d = q.popleft() for child in G[v]: if visited[child]: continue visited[child] = True if child == goal: return d + 1 q.append([child, d + 1]) return -1 D = [0, N - 2, N - 1] routes = ((0,1,2,0),(0,2,1,0)) INF = 1 << 60 ans = INF for route in routes: d = 0 for i in range(len(route) - 1): dist = get_dist(D[route[i]], D[route[i + 1]], G, N) if dist == -1: break d += dist else: ans = min(ans, d) if ans == INF: print(-1) else: print(ans)