# import系 --- # 入力用 --- INT = lambda: int(input()) MI = lambda: map(int, input().split()) MI_DEC = lambda: map(lambda x: int(x) - 1, input().split()) LI = lambda: list(map(int, input().split())) LI_DEC = lambda: list(map(lambda x: int(x) - 1, input().split())) LS = lambda: list(input()) LSS = lambda: input().split() # コード --- # 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, d): dist[start][now] = d for next in graph[now]: if dist[start][next] < INF: continue dfs(start, next, d + 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)