from collections import deque def bfs(s): dist = [inf] * n dist[s] = 0 q = deque() q.append(s) while q: now = q.popleft() for nex in g[now]: if dist[nex] != inf: continue dist[nex] = dist[now] + 1 q.append(nex) return dist n, m = map(int, input().split()) g = [[] for _ in range(n)] for _ in range(m): u, v = map(int, input().split()) u -= 1 v -= 1 g[u].append(v) inf = 10 ** 18 d = { 0 : bfs(0), n - 2 : bfs(n - 2), n - 1 : bfs(n - 1) } ans1 = d[0][n - 2] + d[n - 2][n - 1] + d[n - 1][0] ans2 = d[0][n - 1] + d[n - 1][n - 2] + d[n - 2][0] ans = min(ans1, ans2) if ans >= inf: print(-1) else: print(ans)