# import pypyjit # pypyjit.set_param('max_unroll_recursion=-1') from collections import defaultdict as dd S = input R = range P = print def I(): return int(S()) def M(): return map(int, S().split()) def L(): return list(M()) def O(): return list(map(int, open(0).read().split())) def yn(b): print("Yes" if b else "No") biga = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" smaa = "abcdefghijklmnopqrstuvwxyz" ctoi = lambda c: ord(c) - ord('a') itoc = lambda i: chr(ord('a') + i) inf = 10 ** 18 mod = 998244353 def acc(a): b = [0] for i in a: b.append(b[-1] + i) return b from collections import deque n,m=M() g=[[] for _ in R(n)] g2=[[] for _ in R(n)] for i in R(m): u,v=M() u-=1;v-=1 g[u].append(v);g2[v].append(u) di=[10**18]*n di[0]=0 q=deque([0]) while q: u=q.popleft() for v in g[u]: if di[v]>di[u]+1: di[v]=di[u]+1 q.append(v) di2=[10**18]*n di2[0]=0 q2=deque([0]) while q2: u=q2.popleft() for v in g2[u]: if di2[v]>di2[u]+1: di2[v]=di2[u]+1 q2.append(v) di3=[10**18]*n di3[n-1]=0 q3=deque([n-1]) while q3: u=q3.popleft() for v in g[u]: if di3[v]>di3[u]+1: di3[v]=di3[u]+1 q3.append(v) di4=[10**18]*n di4[n-1]=0 q4=deque([n-1]) while q4: u=q4.popleft() for v in g2[u]: if di4[v]>di4[u]+1: di4[v]=di4[u]+1 q4.append(v) ans=min(di[n-1]+di3[n-2]+di2[n-2],di[n-2]+di4[n-2]+di2[n-1]) if ans>10**9:P(-1) else:P(ans)