#include <bits/stdc++.h>
using namespace std;
int m, n, mapp[402][402], vis[402], d[402];
queue<int> q;
int main() {
  int i, j, u, v, a, g = 0;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> u >> v;
    mapp[u][v] = 1;
    mapp[v][u] = 1;
    if ((u == 1 && v == n) || (u == n && v == 1)) {
      g = 1;
    }
  }
  if (g) {
    for (i = 0; i <= n; i++)
      for (j = 0; j <= n; j++) {
        if (mapp[i][j])
          mapp[i][j] = 0;
        else
          mapp[i][j] = 1;
      }
  }
  g = 0;
  q.push(1);
  vis[1] = 1;
  while (!q.empty()) {
    a = q.front();
    q.pop();
    if (d[a] >= 160000) break;
    if (a == n) {
      g = 1;
      break;
    }
    for (i = 1; i <= n; i++) {
      if (mapp[a][i] && !vis[i]) {
        q.push(i);
        vis[i] = 1;
        d[i] = d[a] + 1;
      }
    }
  }
  if (g)
    cout << d[n] << endl;
  else
    cout << -1 << endl;
  return 0;
}
