import sys from collections import deque,defaultdict import itertools import heapq import bisect import math #sys.setrecursionlimit(10 ** 9) input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) ml = lambda: map(str, input().split()) li = lambda: list(mi()) li_st = lambda: list(map(str, input().split())) lli = lambda n: [li() for _ in range(n)] mod = 998244353 INF = 10**18 N,M = mi() G = [[] for _ in range(N)] for i in range(M): u,v = mi() u -= 1 v -= 1 G[u].append(v) check = [INF] * N check2 = [INF] * N check3 = [INF] * N q = deque() q.append((0,0,-1)) check[0] = 0 while(q): now,times,flag = q.popleft() for nxt in G[now]: if check[nxt] == INF: q.append((nxt,times+1,flag)) check[nxt] = min(times + 1, check[nxt]) else: check[nxt] = min(times+1,check[nxt]) if check[N-2] != INF: q = deque() q.append((N-2,0)) check2[N-2] = 0 while(q): now,times = q.popleft() for nxt in G[now]: if check2[nxt] == INF: q.append((nxt,times+1)) check2[nxt] = min(check2[nxt],times + 1) else: check2[nxt] = min(check2[nxt],times+1) if now == 0: break if check[N-1] != INF: q = deque() q.append((N-1,0)) check3[N-1] = 0 while(q): now,times = q.popleft() for nxt in G[now]: if check3[nxt] == INF: q.append((nxt,times+1)) check3[nxt] = min(check3[nxt],times + 1) else: check3[nxt] = min(check3[nxt],times+1) if now == 0: break ans1 = check[N-1] + check3[N-2] + check2[0] ans2 = check[N-2] + check2[N-1] + check3[0] #print(check) #print(check2) #print(check3) #print(ans1,ans2) print(min(ans1,ans2) if min(ans1,ans2) < INF else -1)