#include <bits/stdc++.h>
using namespace std;
int n, m;
const int maxn = 405, inf = 0x3f3f3f3f;
int dis1[maxn][maxn], dis2[maxn][maxn];
int main() {
  memset(dis1, inf, sizeof dis1);
  memset(dis2, inf, sizeof dis2);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    dis1[x][y] = 1;
    dis1[y][x] = 1;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (dis1[i][j] == inf) {
        dis2[i][j] = dis2[j][i] = 1;
      }
    }
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        dis1[i][j] = min(dis1[i][j], dis1[i][k] + dis1[k][j]);
      }
    }
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        dis2[i][j] = min(dis2[i][j], dis2[i][k] + dis2[k][j]);
      }
    }
  }
  if (dis1[1][n] == inf || dis2[1][n] == inf)
    printf("-1\n");
  else {
    printf("%d\n", max(dis1[1][n], dis2[1][n]));
  }
  return 0;
}
