import sys,math #if len(sys.argv)==2:sys.stdin=open(sys.argv[1]) from collections import defaultdict,deque from itertools import combinations,permutations,accumulate,product from bisect import bisect,bisect_left,bisect_right from heapq import heappop,heappush,heapify #from sortedcontainers import SortedList,SortedSet def input(): return sys.stdin.readline().rstrip() def ii(): return int(input()) def ms(): return map(int, input().split()) def li(): return list(map(int,input().split())) N,M = ms() G = [set() for _ in range(N+1)] for _ in range(M): u,v = ms() G[u].add(v) ans = 10**18 tmp = 0 ng = False visited = [False]*(N+1) visited[1] = True dist = [-1]*(N+1) dist[1] = 0 que = deque() que.append(1) end = False while que: v = que.popleft() visited[v] = True for v2 in G[v]: if visited[v2]: continue dist[v2] = dist[v] + 1 if v2==N-1: end = True break que.append(v2) if end: break if end==False: ng = True if ng==False: tmp += dist[N-1] visited = [False]*(N+1) visited[N-1] = True dist = [-1]*(N+1) dist[N-1] = 0 que = deque() que.append(N-1) end = False while que: v = que.popleft() visited[v] = True for v2 in G[v]: if visited[v2]: continue dist[v2] = dist[v] + 1 if v2==N: end = True break que.append(v2) if end: break if end==False: ng = True tmp += dist[N] end = False visited = [False]*(N+1) visited[N] = True dist = [-1]*(N+1) dist[N] = 0 que = deque() que.append(N) end = False while que: v = que.popleft() visited[v] = True for v2 in G[v]: if visited[v2]: continue dist[v2] = dist[v] + 1 if v2==1: end = True break que.append(v2) if end: break if end==False: ng = True tmp += dist[1] if ng==False: ans = min(ans,tmp) tmp = 0 ng = False visited = [False]*(N+1) visited[1] = True dist = [-1]*(N+1) dist[1] = 0 que = deque() que.append(1) end = False while que: v = que.popleft() visited[v] = True for v2 in G[v]: if visited[v2]: continue dist[v2] = dist[v] + 1 if v2==N: end = True break que.append(v2) if end: break if end==False: ng = True if ng==False: tmp += dist[N] visited = [False]*(N+1) visited[N] = True dist = [-1]*(N+1) dist[N] = 0 que = deque() que.append(N) end = False while que: v = que.popleft() visited[v] = True for v2 in G[v]: if visited[v2]: continue dist[v2] = dist[v] + 1 if v2==N-1: end = True break que.append(v2) if end: break if end==False: ng = True tmp += dist[N-1] end = False visited = [False]*(N+1) visited[N-1] = True dist = [-1]*(N+1) dist[N-1] = 0 que = deque() que.append(N-1) end = False while que: v = que.popleft() visited[v] = True for v2 in G[v]: if visited[v2]: continue dist[v2] = dist[v] + 1 if v2==1: end = True break que.append(v2) if end: break if end==False: ng = True tmp += dist[1] if ng==False: ans = min(ans,tmp) if ans!=10**18: print(ans) else: print(-1)