import heapq n, m = map(int, input().split()) edges = [[] for _ in range(n)] for _ in range(m): u, v = map(int, input().split()) u, v = u - 1, v - 1 edges[u].append(v) INF = float("inf") def dijkstra(s, g): dist = [INF] * n que = [(0, s)] heapq.heapify(que) while que: cost, crt = heapq.heappop(que) if dist[crt] <= cost: continue dist[crt] = cost for nxt in edges[crt]: if dist[nxt] <= cost + 1: continue heapq.heappush(que, (cost + 1, nxt)) return dist[g] ans = INF for l in ((0, n - 1, n - 2, 0), (0, n - 2, n - 1, 0)): tmp = 0 for a, b in zip(l, l[1:]): tmp += dijkstra(a, b) ans = min(ans, tmp) print(-1 if ans == INF else ans)