#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<int> > tab;
vector<vector<int> > g1, g2;
vector<int> d1, d2;
void bfs1() {
  queue<int> q;
  q.push(0);
  d1.assign(n, 1000);
  d1[0] = 0;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < g1[v].size(); ++i) {
      int to = g1[v][i];
      if (d1[to] > d1[v] + 1) {
        d1[to] = d1[v] + 1;
        q.push(to);
      }
    }
  }
}
void bfs2() {
  queue<int> q;
  q.push(0);
  d2.assign(n, 1000);
  d2[0] = 0;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < g2[v].size(); ++i) {
      int to = g2[v][i];
      if (d2[to] > d2[v] + 1) {
        d2[to] = d2[v] + 1;
        q.push(to);
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  tab.assign(n, vector<int>(n, 0));
  g1.resize(n);
  g2.resize(n);
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    tab[a][b] = 1;
    tab[b][a] = 1;
    g1[a].push_back(b);
    g1[b].push_back(a);
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < i; ++j) {
      if (tab[i][j] == 0) {
        g2[i].push_back(j);
        g2[j].push_back(i);
      }
    }
  }
  bfs1();
  bfs2();
  if (d1[n - 1] == 1000 || d2[n - 1] == 1000) {
    cout << "-1" << endl;
    return 0;
  }
  cout << max(d1[n - 1], d2[n - 1]) << endl;
  return 0;
}
