#include <bits/stdc++.h>
using namespace std;
vector<long long int> b(200005, 1e9), p[200005], g[200005], c(200005, 1e9);
long long int a, d, e;
void dfs(long long int i, long long int n) {
  if (i != n) {
    for (long long int j = 0; j < p[i].size(); j++) {
      if (b[p[i][j]] > b[i] + 1) {
        b[p[i][j]] = min(b[i] + 1, b[p[i][j]]);
        dfs(p[i][j], n);
      }
    }
  }
}
void dfr(long long int i, long long int n) {
  if (i != n) {
    for (long long int j = 0; j < g[i].size(); j++) {
      if (c[g[i][j]] > c[i] + 1) {
        c[g[i][j]] = min(c[i] + 1, c[g[i][j]]);
        dfr(g[i][j], n);
      }
    }
  }
}
int main() {
  long long int n, m;
  cin >> n >> m;
  vector<vector<long long int>> d(n + 1, vector<long long int>(n + 1, 0));
  for (long long int i = 0; i < m; i++) {
    long long int a, b;
    cin >> a >> b;
    d[a][b] = 1;
    d[b][a] = 1;
    p[a].push_back(b);
    p[b].push_back(a);
  }
  long long int f = 0;
  for (long long int i = 1; i <= n; i++) {
    for (long long int j = 1; j <= n; j++) {
      if (d[i][j] == 0) {
        g[i].push_back(j);
      }
    }
  }
  b[1] = 0;
  c[1] = 0;
  dfs(1, n);
  dfr(1, n);
  f = max(b[n], c[n]);
  if (f == 1e9) {
    cout << -1 << endl;
    exit(0);
  }
  cout << f << endl;
  return 0;
}
