#include <bits/stdc++.h>
using namespace std;
int adjmat[444][444];
const int INF = (int)1e9;
int n, m;
int findDist(int cmp) {
  vector<int> dist(n, INF);
  dist[0] = 0;
  queue<pair<int, int> > q;
  q.push(pair<int, int>(0, 0));
  while (!q.empty()) {
    pair<int, int> v = q.front();
    q.pop();
    for (int i = 0; i < n; i++) {
      if (adjmat[v.first][i] == cmp && dist[i] == INF) {
        dist[i] = v.second + 1;
        q.push(pair<int, int>(i, dist[i]));
      }
    }
  }
  vector<int> dist2(n, INF);
  dist2[0] = 0;
  q.push(pair<int, int>(0, 0));
  while (!q.empty()) {
    pair<int, int> v = q.front();
    q.pop();
    for (int i = 0; i < n; i++) {
      if (adjmat[v.first][i] != cmp && dist2[i] == INF) {
        if (v.second + 1 == dist[i]) {
          continue;
        }
        dist2[i] = v.second + 1;
        q.push(pair<int, int>(i, dist2[i]));
      }
    }
  }
  return max(dist[n - 1], dist2[n - 1]);
}
int main() {
  std::ios_base::sync_with_stdio(false);
  cin >> n >> m;
  memset(adjmat, 0, sizeof adjmat);
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    adjmat[u][v] = adjmat[v][u] = 1;
  }
  int r1 = findDist(0), r2 = findDist(1);
  int res = min(r1, r2);
  if (res == INF)
    cout << "-1\n";
  else
    cout << res << "\n";
  return 0;
}
