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): 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 = { 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)