#include <bits/stdc++.h>
using namespace std;
int n, m;
bool camino[402][402];
int vis[402];
int color;
int path(int nodo) {
  color++;
  queue<int> cola;
  cola.push(nodo);
  int t = 0;
  while (!cola.empty()) {
    for (int a = cola.size(); a >= 1; a--) {
      nodo = cola.front();
      cola.pop();
      if (nodo == n) return t;
      if (vis[nodo] == color) continue;
      vis[nodo] = color;
      for (int i = 1; i <= n; i++)
        if (camino[i][nodo]) cola.push(i);
    }
    t++;
  }
  return -1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  int a, b;
  for (int i = 1; i <= m; i++) {
    cin >> a >> b;
    camino[a][b] = camino[b][a] = true;
  }
  int menor = -1;
  menor = max(menor, path(1));
  if (menor == -1) {
    cout << menor << "\n";
    return 0;
  }
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++)
      camino[i][j] = camino[j][i] = !camino[i][j];
  int aux = path(1);
  if (aux == -1) {
    cout << aux << "\n";
    return 0;
  }
  menor = max(menor, aux);
  cout << menor << "\n";
  return 0;
}
