############################################################# import sys sys.setrecursionlimit(10**7) from heapq import heappop,heappush from collections import deque,defaultdict,Counter from bisect import bisect_left, bisect_right from itertools import product,combinations,permutations from math import sin,cos #from math import isqrt #DO NOT USE PyPy ipt = sys.stdin.readline iin = lambda :int(ipt()) lmin = lambda :list(map(int,ipt().split())) INF = 1<<30 def bfs01(s,N,G): dq = deque() dq.append(s) cost = [INF]*N cost[s] = 0 while dq: cur = dq.pop() for nxt in G[cur]: if cost[nxt] != INF: continue cost[nxt] = cost[cur]+1 dq.appendleft(nxt) return cost ############################################################# N,M = lmin() G = [[] for _ in range(N)] for _ in range(M): a,b = lmin() G[a-1].append(b-1) cost1 = bfs01(0,N,G) cost2 = bfs01(N-2,N,G) cost3 = bfs01(N-1,N,G) ans = min(cost1[N-2]+cost2[N-1]+cost3[0],cost1[N-1]+cost3[N-2]+cost2[0]) if ans >= INF: print(-1) else: print(ans)