#include <bits/stdc++.h>
using namespace std;
int sum() { return 0; }
template <typename T, typename... Args>
T sum(T a, Args... args) {
  return a + sum(args...);
}
void solve() {
  long long int n, m;
  cin >> n >> m;
  long long int a[n + 1][n + 1];
  for (long long int i = 1; i <= n; i++) {
    for (long long int j = 1; j <= n; j++) a[i][j] = 0;
  }
  for (long long int i = 1; i <= m; i++) {
    long long int x, y;
    cin >> x >> y;
    a[x][y] = 1;
    a[y][x] = 1;
  }
  long long int t[n + 1][n + 1];
  for (long long int i = 1; i <= n; i++) {
    for (long long int j = 1; j <= n; j++) {
      if (i == j)
        t[i][j] = 0;
      else if (a[i][j])
        t[i][j] = 1;
      else
        t[i][j] = (long long int)1e9;
    }
  }
  for (long long int k = 1; k <= n; k++) {
    for (long long int i = 1; i <= n; i++) {
      for (long long int j = 1; j <= n; j++) {
        t[i][j] = min(t[i][j], t[i][k] + t[k][j]);
      }
    }
  }
  for (long long int i = 1; i <= n; i++) {
    for (long long int j = 1; j <= n; j++) {
      a[i][j] ^= 1;
    }
  }
  long long int b[n + 1][n + 1];
  for (long long int i = 1; i <= n; i++) {
    for (long long int j = 1; j <= n; j++) {
      if (i == j)
        b[i][j] = 0;
      else if (a[i][j])
        b[i][j] = 1;
      else
        b[i][j] = (long long int)1e9;
    }
  }
  for (long long int k = 1; k <= n; k++) {
    for (long long int i = 1; i <= n; i++) {
      for (long long int j = 1; j <= n; j++) {
        b[i][j] = min(b[i][j], b[i][k] + b[k][j]);
      }
    }
  }
  if (b[1][n] == (long long int)1e9 or t[1][n] == (long long int)1e9)
    cout << -1;
  else
    cout << max(t[1][n], b[1][n]);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int t = 1;
  while (t--) {
    solve();
  }
}
