#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e16;
vector<long long> b(410, inf);
vector<vector<long long> > bus(410, b), train(410, b);
long long n, m;
long long path(vector<vector<long long> > a) {
  vector<pair<long long, long long> > q(2004000);
  long long qs = 0, qf = 1;
  long long i, t;
  q[0] = make_pair(1, 0);
  vector<bool> used(1000, false);
  used[1] = true;
  while (qs < qf) {
    long long cur = q[qs].first, t = q[qs].second;
    used[cur] = true;
    for (i = 1; i <= n; i++) {
      if ((a[cur][i] == 1) && (!used[i])) {
        used[i] = true;
        q[qf] = make_pair(i, t + 1);
        if (i == n) {
          return (t + 1);
        }
        qf++;
        if (qf == 2000000) cout << "fatal" << endl;
      }
    }
    qs++;
  }
  if (!used[n]) {
    return inf;
  }
  return 0;
}
int main() {
  cin >> n >> m;
  long long i, j;
  for (i = 0; i < m; i++) {
    long long q, w;
    cin >> q >> w;
    train[q][w] = 1;
    train[w][q] = 1;
  }
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      if ((i != j) && (train[i][j] == inf)) {
        bus[i][j] = 1;
        bus[j][i] = 1;
      }
  long long ans;
  if (bus[1][n] == inf) {
    ans = path(bus);
  } else {
    ans = path(train);
  }
  if (ans == inf)
    cout << -1;
  else
    cout << ans;
  return 0;
}
