N, M = map(int, input().split()) UV = [list(map(int, input().split())) for _ in range(M)] E = [[] for _ in range(N + 1)] for u, v in UV: E[u].append(v) INF = 10 ** 6 def bfs(a, b): nv = [True] * (N + 1) q = [a] cnt = 0 while q: q2 = [] while q: x = q.pop(-1) if x == b: return cnt for y in E[x]: if nv[y]: nv[y] = False q2.append(y) cnt += 1 q, q2 = q2, q return INF ans = min(bfs(1, N - 1) + bfs(N - 1, N) + bfs(N, 1), bfs(1, N) + bfs(N, N - 1) + bfs(N - 1, 1)) if ans >= INF: print(-1) else: print(ans)