#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-9;
const int mod = 1e9 + 7;
const long long INF = 1e18 + 1;
const long long inf = 1e12 + 1;
const int N = 1e4 + 9;
int n, m, L, s, t;
long long dp[1000][1000], pd[1000][1000];
bool b[1000][1000];
int main() {
  ios_base ::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dp[i][j] = INF;
      pd[i][j] = INF;
    }
  }
  for (int i = 1; i <= n; i++) {
    dp[i][i] = 0;
    pd[i][i] = 0;
  }
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    dp[u][v] = dp[v][u] = 1;
    b[u][v] = 1;
    b[v][u] = 1;
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
      if (!b[i][j] && !b[j][i]) pd[i][j] = pd[j][i] = 1;
    }
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
        pd[i][j] = min(pd[i][j], pd[i][k] + pd[k][j]);
      }
    }
  }
  if (max(pd[1][n], dp[1][n]) == INF)
    cout << -1;
  else
    cout << max(pd[1][n], dp[1][n]);
  exit(0);
}
