#include <bits/stdc++.h>
using namespace std;
const int MAXN = 405;
const int SX[] = {-1, 0, 1, 0};
const int SY[] = {0, -1, 0, 1};
int vrijeme(vector<vector<int> > veze, int n) {
  int dist[MAXN];
  memset(dist, -1, sizeof dist);
  dist[0] = 0;
  queue<int> sljedeci;
  sljedeci.push(0);
  int x;
  while (!sljedeci.empty()) {
    x = sljedeci.front();
    sljedeci.pop();
    for (auto nx : veze[x]) {
      if (dist[nx] != -1) continue;
      dist[nx] = dist[x] + 1;
      sljedeci.push(nx);
    }
  }
  return dist[n - 1];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  bool postoji[MAXN][MAXN];
  vector<vector<int> > veze1(n);
  vector<vector<int> > veze2(n);
  int u, v;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    --u;
    --v;
    postoji[u][v] = true;
    postoji[v][u] = true;
    veze1[u].push_back(v);
    veze1[v].push_back(u);
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (postoji[i][j]) continue;
      veze2[i].push_back(j);
      veze2[j].push_back(i);
    }
  }
  int t1 = vrijeme(veze1, n);
  int t2 = vrijeme(veze2, n);
  if (t1 == -1 || t2 == -1)
    cout << -1 << '\n';
  else
    cout << max(t1, t2) << '\n';
  return 0;
}
