from collections import defaultdict, deque, Counter import copy from itertools import combinations, permutations, product, accumulate, groupby, chain from heapq import heapify, heappop, heappush import math import bisect from pprint import pprint from random import randint import sys # sys.setrecursionlimit(700000) input = lambda: sys.stdin.readline().rstrip('\n') inf = float('inf') mod1 = 10**9+7 mod2 = 998244353 def ceil_div(x, y): return -(-x//y) ################################################ def bfs(s, t): dist = [-1]*N dq = deque([s]) dist[s] = 0 while dq: now = dq.pop() for nxt in adj[now]: if dist[nxt] == -1: dist[nxt] = dist[now]+1 if nxt == t: return dist[t] dq.appendleft(nxt) return inf N, M = map(int, input().split()) adj = [[] for _ in range(N)] for _ in range(M): u, v = map(int, input().split()) u -= 1; v -= 1 adj[u].append(v) d1 = 0 d1 += bfs(0, N-2) d1 += bfs(N-2, N-1) d1 += bfs(N-1, 0) d2 = 0 d2 += bfs(0, N-1) d2 += bfs(N-1, N-2) d2 += bfs(N-2, 0) print(min(d1, d2) if min(d1, d2) != inf else -1)