#include <bits/stdc++.h>
using namespace std;
struct cmp {
  bool operator()(pair<long, long> a, pair<long, long> b) {
    return a.second > b.second;
  }
};
long long modpow(long long base, long long exp, long long mod) {
  base %= mod;
  long long result = 1;
  while (exp > 0) {
    if (exp & 1) result = (result * base) % mod;
    base = (base * base) % mod;
    exp >>= 1;
  }
  return result;
}
long long rail[405][405], road[405][405], a[405], b[405];
priority_queue<pair<long, long>, vector<pair<long, long> >, cmp> pq;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m, u, v;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      rail[i][j] = road[i][j] = 1000000007;
    }
  }
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    rail[u][v] = 1;
    rail[v][u] = 1;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (j == i) continue;
      if (rail[i][j] == 1000000007) {
        road[i][j] = 1;
      }
    }
  }
  for (int i = 0; i <= n; i++) {
    a[i] = 1000000007;
    b[i] = 1000000007;
  }
  pq.push(make_pair(1, 0));
  a[1] = 0;
  while (!pq.empty()) {
    int i = pq.top().first;
    pq.pop();
    for (int j = 1; j <= n; j++) {
      if (a[i] + rail[i][j] < a[j]) {
        a[j] = a[i] + rail[i][j];
        pq.push(make_pair(j, a[j]));
      }
    }
  }
  pq.push(make_pair(1, 0));
  b[1] = 0;
  while (!pq.empty()) {
    int i = pq.top().first;
    pq.pop();
    for (int j = 1; j <= n; j++) {
      if (b[i] + road[i][j] < b[j]) {
        b[j] = b[i] + road[i][j];
        pq.push(make_pair(j, b[j]));
      }
    }
  }
  long long ans = max(a[n], b[n]);
  if (ans >= 1000000007) ans = -1;
  cout << ans << endl;
  return 0;
}
