#include <bits/stdc++.h>
using namespace std;
const int MAX = 400 + 7;
const int INF = 1e9;
int n, m, k, p, q, a, b, c, d, cnt = 0;
int ar[MAX][MAX];
int dist[MAX], vis[MAX];
int bfs(int type) {
  for (int i = 1; i <= n; ++i) dist[i] = INF;
  memset(vis, 0, sizeof(vis));
  dist[1] = 0;
  vis[1] = 1;
  queue<int> Q;
  Q.push(1);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int i = 1; i <= n; ++i)
      if (u != i && ar[u][i] == type && vis[i] == 0) {
        vis[i] = 1;
        dist[i] = dist[u] + 1;
        Q.push(i);
      }
  }
  return dist[n];
}
int MAIN() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> a >> b;
    ar[a][b] = ar[b][a] = 1;
  }
  int bus = bfs(0);
  int train = bfs(1);
  if (bus == INF || train == INF)
    cout << "-1";
  else
    cout << max(bus, train);
  return 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  int start = clock();
  cout << fixed << setprecision(16);
  int ret = MAIN();
  return ret;
}
