import sys from collections import deque input = sys.stdin.buffer.readline def solve(N, G): INF = 10 ** 9 # bfs d = deque([0]) dist = [INF] * N dist[0] = 0 while d: v = d.popleft() if v == N - 1: return dist[v] dx = dist[v] + 1 for x in G[v]: if dist[x] == INF: d.append(x) dist[x] = dx return -1 N, M = map(int, input().split()) G = [[] for _ in range(N)] for _ in range(M): a, b = tuple(int(x) - 1 for x in input().split()) G[a].append(b) G[b].append(a) print(solve(N, G))