N, M = map(int, input().split()) UV = [map(int, input().split()) for _ in range(M)] graph = [[] for _ in range(N)] for U, V in UV: U -= 1 V -= 1 graph[U].append(V) import collections def bfs(start, goal): q = collections.deque() q.append(start) reached = [0] * N while q: now = q.popleft() for g in graph[now]: if reached[g]: continue reached[g] = reached[now] + 1 q.append(g) return reached[goal] ans = 10**18 a = bfs(0, N-2) b = bfs(N-2, N-1) c = bfs(N-1, 0) if a and b and c: ans = min(ans, a + b + c) a = bfs(0, N-1) b = bfs(N-1, N-2) c = bfs(N-2, 0) if a and b and c: ans = min(ans, a + b + c) print(ans if ans != 10**18 else -1)