from collections import defaultdict from sys import setrecursionlimit setrecursionlimit(10**9) def dfs(pos,step,cnt): global ans if pos == 1 and cnt == 2: ans = min(ans,step) return if pos in visited: return visited.add(pos) for i in g[pos]: if pos == n or pos == n-1: dfs(i,step+1,cnt+1) else: dfs(i,step+1,cnt) visited.discard(pos) n,m = map(int,input().split()) g = defaultdict(list) for i in range(m): u,v = map(int,input().split()) g[u].append(v) visited = set() ans = 10**6 dfs(1,0,0) if ans == 10**6: print(-1) exit() print(ans)