#include <bits/stdc++.h>
using namespace std;
const int N = 444;
int n, m, a[N][N], dist[N];
int bfs(int edge) {
  memset(dist, -1, sizeof(dist));
  dist[1] = 0;
  queue<int> q;
  q.push(1);
  while (q.size()) {
    int u = q.front();
    q.pop();
    for (int v = 1; v <= n; ++v) {
      if (dist[v] == -1 && a[u][v] == edge) {
        dist[v] = dist[u] + 1;
        q.push(v);
      }
    }
  }
  return dist[n];
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    a[u][v] = a[v][u] = 1;
  }
  int v1 = bfs(0);
  int v2 = bfs(1);
  if (min(v1, v2) == -1) {
    cout << -1 << endl;
  } else {
    cout << max(v1, v2) << endl;
  }
  return 0;
}
