from collections import defaultdict from sys import setrecursionlimit setrecursionlimit(10**9) def dfs(pos,r,visited): global ans if pos in visited: if pos == 1 and n in visited and (n - 1) in visited: ans = min(ans,len(r)) return for i in g[pos]: dfs(i,r+[pos], visited | set([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,[],set()) if ans == 10**6: print(-1) exit() print(ans)