from collections import deque 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) def bfs(start): dists = [INF]*N dists[start] = 0 que = deque([(start, 0)]) while que: now, d = que.popleft() for next in graph[now]: if dists[next] < INF: continue dists[next] = d + 1 que.append((next, d + 1)) return dists di = { 0: bfs(0), N - 2: bfs(N - 2), N - 1: bfs(N - 1) } # 町1 -> 町N-1 -> 町N -> 町1 の最短経路の長さ ans_1 = di[0][N - 2] + di[N - 2][N - 1] + di[N - 1][0] # 町1 -> 町N -> 町N-1 -> 町1 の最短経路の長さ ans_2 = di[0][N - 1] + di[N - 1][N - 2] + di[N - 2][0] ans = min(ans_1, ans_2) print(ans if ans < INF else -1)