# DFS 解答 (想定: WA) import sys sys.setrecursionlimit(10**7) INF = 1 << 60 N, M = map(int, input().split()) graph = [[] for _ in range(N)] for _ in range(M): u, v = map(int, input().split()) u -= 1; v -= 1 graph[u].append(v) dist = { 0: [INF for _ in range(N)], N-2: [INF for _ in range(N)], N-1: [INF for _ in range(N)] } def dfs(start, now, dist): dist[start][now] = dist for next in graph[now]: if dist[start][next] < INF: continue dfs(start, next, dist + 1) dfs(0, 0, 0) dfs(N-2, N-2, 0) dfs(N-1, N-1, 0) ans = min( dist[0][N-2] + dist[N-2][N-1] + dist[N-1][0], dist[0][N-1] + dist[N-1][N-2] + dist[N-2][0] ) if ans < INF: print(ans) else: print(-1)