#include <bits/stdc++.h>
#pragma warning(disable : 4996)
const int INF = 1e8;
using namespace std;
int disiron[450][450], disroad[405][405];
int n, m;
void floyd1() {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        disiron[j][k] = min(disiron[j][k], disiron[j][i] + disiron[i][k]);
      }
    }
  }
}
void floyd2() {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        disroad[j][k] = min(disroad[j][k], disroad[j][i] + disroad[i][k]);
      }
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      disiron[i][j] = disroad[i][j] = INF;
    }
  }
  int u, v;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    disiron[u][v] = disiron[v][u] = 1;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      if (i == j) continue;
      if (disiron[i][j] == INF) disroad[i][j] = 1;
    }
  floyd1();
  floyd2();
  if (disiron[1][n] == INF || disroad[1][n] == INF) {
    cout << -1 << endl;
  } else {
    cout << max(disiron[1][n], disroad[1][n]) << endl;
  }
  return 0;
}
