from collections import deque INF = 1 << 60 N, M = map(int, input().split()) # graph[u]: 町u と繋がっている町のリスト graph = [[] for _ in range(N)] for _ in range(M): u, v = map(int, input().split()) u -= 1 v -= 1 graph[u].append(v) # 町start からN個全ての町までの距離のリストを返す関数 def bfs(start): distances = [INF]*N distances[start] = 0 que = deque([(start, 0)]) while que: now, d = que.popleft() for next in graph[now]: if distances[next] < INF: continue distances[next] = d + 1 que.append((next, d + 1)) return distances # dist[u][v]: 町u ->町v の距離 (INF ならば到達不能) dist = { 0: bfs(0), N - 2: bfs(N - 2), N - 1: bfs(N - 1) } 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)