#include <bits/stdc++.h>
using namespace std;
int n, m;
bool vis[401][401];
int que[401], dis[401];
int bfs() {
  for (int i = 1; i <= n; i++) dis[i] = -1;
  int p = 0, q = 0;
  que[q++] = 1;
  while (p != q) {
    int i = que[p++];
    for (int j = 1; j <= n; j++)
      if (vis[i][j] && dis[j] == -1) {
        dis[j] = dis[i] + 1;
        if (j == n) return dis[j];
        que[q++] = j;
      }
  }
  return -1;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) vis[i][j] = false;
  while (m--) {
    int x, y;
    scanf("%d%d", &x, &y);
    vis[x][y] = vis[y][x] = true;
  }
  if (vis[1][n] == true) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) vis[i][j] = !vis[i][j];
  }
  for (int i = 1; i <= n; i++) vis[i][i] = true;
  printf("%d\n", bfs());
  return 0;
}
