#include <bits/stdc++.h>
using namespace std;
const int N = 401;
int g[N][N], n, m, d[N], x, y;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    g[x][y] = g[y][x] = 1;
  }
  for (int i = 2; i <= n; i++) d[i] = -1;
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    if (u == n) {
      printf("%d\n", d[n]);
      return 0;
    }
    for (int i = 1; i <= n; i++) {
      if (g[u][i] ^ g[1][n]) {
        if (d[i] == -1) {
          d[i] = d[u] + 1;
          q.push(i);
        }
      }
    }
  }
  printf("-1\n");
}
