from collections import defaultdict,deque n,m = map(int,input().split()) g = defaultdict(list) for i in range(m): u,v = map(int,input().split()) g[u].append(v) dist1 = [-1]*(n+1) que = deque([1]) dist1[1] = 0 while que: q = que.popleft() for i in g[q]: if dist1[i] == -1: dist1[i] = dist1[q] + 1 que.append(i) dist2 = [-1]*(n+1) que = deque([n-1]) dist2[n-1] = 0 while que: q = que.popleft() for i in g[q]: if dist2[i] == -1: dist2[i] = dist2[q] + 1 que.append(i) dist3 = [-1]*(n+1) que = deque([n]) dist3[n] = 0 while que: q = que.popleft() for i in g[q]: if dist3[i] == -1: dist3[i] = dist3[q] + 1 que.append(i) if -1 in [dist1[n],dist3[n-1],dist2[1]] or -1 in [dist1[n-1],dist2[n],dist3[1]]: print(-1) exit() print(min(dist1[n] + dist3[n-1] + dist2[1], dist1[n-1] + dist2[n] + dist3[1]))