#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int inf = 1e9 + 5;
const double pi = acos(-1);
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
const int mod = 998244353;
int n, m, d[405], us[405], r[405][405];
void bfs(int c) {
  queue<int> q;
  q.push(1);
  d[1] = 0;
  us[1] = 1;
  while (q.size()) {
    int v = q.front();
    q.pop();
    for (int i = 1; i <= n; i++) {
      if (v == i || r[v][i] != c) continue;
      if (!us[i]) {
        d[i] = d[v] + 1;
        us[i] = 1;
        q.push(i);
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    r[u][v] = 1;
    r[v][u] = 1;
  }
  for (int i = 1; i <= n; i++) {
    d[i] = inf;
  }
  if (r[1][n] == 0) {
    bfs(1);
  } else {
    bfs(0);
  }
  if (d[n] == inf) {
    cout << "-1";
  } else {
    cout << d[n];
  }
  return 0;
}
