N,M = map(int,input().split()) G = [[] for _ in range(N)] for _ in range(M): u,v = map(int,input().split()) G[u-1].append(v- 1) from collections import deque def calc(start): inf = 10 ** 8 d = [inf] * N d[start] = 0 stack = deque() stack.append(start) while stack: now = stack.popleft() for v in G[now]: if d[v] == inf: d[v] = d[now] + 1 stack.append(v) return d d0 = calc(0) d1 = calc(N -2) d2 = calc(N - 1) tmp = d0[N - 2] + d2[0] + d1[-1] tmp2 = d0[-1] + d2[N-2] + d1[0] ans = min(tmp,tmp2) if ans >= 10 ** 7: print(-1) else: print(ans)