#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1050;
int a[maxn][maxn], b[maxn][maxn], c[maxn][maxn];
int n, m;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  while (cin >> n >> m) {
    memset(a, 0, sizeof(a));
    memset(b, inf, sizeof(b));
    int x, y;
    for (int i = 0; i < m; i++) {
      cin >> x >> y;
      a[x][y] = 1;
      a[y][x] = 1;
      b[x][y] = 1;
      b[y][x] = 1;
    }
    if (a[1][n] == 0) {
      for (int k = 1; k <= n; k++) {
        for (int i = 1; i <= n; i++) {
          for (int j = i + 1; j <= n; j++) {
            if (k != i && k != j && b[i][k] != inf && b[k][j] != inf) {
              b[i][j] = b[j][i] = min(b[i][j], b[i][k] + b[k][j]);
            }
          }
        }
      }
      if (b[1][n] != inf) {
        cout << b[1][n] << endl;
      } else {
        cout << "-1" << endl;
      }
    } else {
      for (int i = 0; i < 500; i++) {
        for (int j = 0; j < 500; j++) {
          if (b[i][j] == 1) {
            c[i][j] = inf;
          } else if (b[i][j] = inf) {
            c[i][j] = 1;
          }
        }
      }
      for (int k = 1; k <= n; k++) {
        for (int i = 1; i <= n; i++) {
          for (int j = i + 1; j <= n; j++) {
            if (k != j && k != i && c[i][k] != inf && c[k][j] != inf) {
              c[i][j] = c[j][i] = min(c[i][j], c[i][k] + c[k][j]);
            }
          }
        }
      }
      if (c[1][n] != inf) {
        cout << c[1][n] << endl;
      } else {
        cout << "-1" << endl;
      }
    }
  }
  return 0;
}
