from collections import defaultdict, deque INF = 1 << 60 N, M = map(int, input().split()) adj = defaultdict(list) for _ in range(M): u, v = map(lambda x: int(x)-1, input().split()) adj[u].append(v) def calc(src: int) -> list: dists = [INF] * N dists[src] = 0 q = deque([src]) while q: v = q.popleft() for to in adj[v]: if dists[to] <= dists[v] + 1: continue dists[to] = dists[v] + 1 q.append(to) return dists def distance(src, dst) -> int: d = calc(src) return d[dst] d1 = distance(0, N-2) + distance(N-2, N-1) + distance(N-1, 0) d2 = distance(0, N-1) + distance(N-1, N-2) + distance(N-2, 0) if d1 >= INF and d2 >= INF: print(-1) exit() ans = min(d1, d2) print(ans)