import sys input = lambda: sys.stdin.readline()[:-1] sys.setrecursionlimit(1000000) import heapq n, m = map(int, input().split()) G = [[] for i in range(n)] for i in range(m): u, v = map(int, input().split()) G[u-1].append(v-1) def bfs(u, v, v2=None): q = [u] dists = [-1]*m dists[u] = 0 now = 1 while q: new_q = [] for i in q: for j in G[i]: if dists[j] == -1: new_q.append(j) dists[j] = now now += 1 q = new_q if v2: return dists[v], dists[v2] return dists[v] p, pp = bfs(0, n-1, n-2) ppp, pppp = bfs(n-1, 0, n-2) ppppp, pppppp = bfs(n-2, 0, n-1) if p == -1 or pppp == -1 or ppppp == -1: if pp == -1 or ppp == -1 or pppppp == -1: print(-1) else: print(pp+ppp+pppppp) elif pp == -1 or ppp == -1 or pppppp == -1: print(p+pppp+ppppp) else: print(min(p+pppp+ppppp, pp+ppp+pppppp))