from collections import defaultdict, deque n, m = map(int, input().split()) G = defaultdict(list) for _ in range(m): u, v = map(int, input().split()) G[u - 1].append(v - 1) INF = 10**18 DP = [[INF for _ in range(1 << 2)] for _ in range(n)] DP[0][0] = 0 Que = deque([(0, 0)]) while Que: cp, cbit = Que.popleft() cc = DP[cp][cbit] nc = cc + 1 for np in G[cp]: if np == n - 2: nbit = cbit | 1 elif np == n - 1: nbit = cbit | (1 << 1) else: nbit = cbit if nc >= DP[np][nbit]: continue DP[np][nbit] = nc Que.append((np, nbit)) if DP[0][-1] == INF: print(-1) else: print(DP[0][-1])