#include <bits/stdc++.h>
using namespace std;
int n;
int f[500][500];
int g[500][500];
int main() {
  int m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      f[i][j] = g[i][j] = 10000;
    }
  }
  for (int i = 1; i <= n; i++) {
    f[i][i] = g[i][i] = 0;
  }
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    f[u][v] = f[v][u] = 1;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i != j && f[i][j] == 10000) {
        g[i][j] = 1;
      }
    }
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
        g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
      }
    }
  }
  int ans = max(f[1][n], g[1][n]);
  if (ans == 10000) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}
