#include <bits/stdc++.h>
using namespace std;
long long int power_mod(long long int a, long long int x) {
  if (x == 0) return 1;
  long long int y = power_mod(a, x / 2);
  long long int ans = (y * y) % 1000000007;
  if (x % 2) ans = (ans * a) % 1000000007;
  return ans;
}
long long int inv(long long int a) { return power_mod(a, 1000000007 - 2); }
long long int power(long long int a, long long int x) {
  if (x == 0) return 1;
  long long int y = power(a, x / 2);
  long long int ans = (y * y);
  if (x % 2) ans *= a;
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  vector<vector<int>> a(n + 1, vector<int>(n + 1));
  while (m--) {
    int x, y;
    cin >> x >> y;
    a[x][y] = 1;
    a[y][x] = 1;
  }
  queue<pair<int, int>> q;
  vector<bool> vis(n + 1);
  vis[1] = 1;
  q.emplace(make_pair(1, 0));
  int t1 = 0;
  bool nf1 = true, nf2 = true;
  while (!q.empty()) {
    pair<int, int> tpp = q.front();
    q.pop();
    int tp = tpp.first;
    if (tp == n) {
      t1 = tpp.second;
      nf1 = false;
      break;
    }
    for (int i = 1; i <= n; i++) {
      if (a[tp][i] && !vis[i]) {
        vis[i] = 1;
        q.emplace(make_pair(i, tpp.second + 1));
      }
    }
  }
  int t2 = 0;
  while (!q.empty()) q.pop();
  q.emplace(make_pair(1, 0));
  vis.assign(n + 1, false);
  vis[1] = 1;
  while (!q.empty()) {
    pair<int, int> tpp = q.front();
    q.pop();
    int tp = tpp.first;
    if (tp == n) {
      t2 = tpp.second;
      nf2 = false;
      break;
    }
    for (int i = 1; i <= n; i++) {
      if (!a[tp][i] && !vis[i]) {
        vis[i] = 1;
        q.emplace(make_pair(i, tpp.second + 1));
      }
    }
  }
  if (nf1 || nf2)
    cout << -1 << endl;
  else
    cout << max(t1, t2) << endl;
}
