#include <bits/stdc++.h>
using namespace std;
signed main() {
  int n, m;
  cin >> n >> m;
  vector<vector<bool>> g(n, vector<bool>(n, 0));
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a][b] = 1;
    g[b][a] = 1;
  }
  deque<int> q = {0};
  vector<int> dp(n, 1e9);
  dp[0] = 0;
  while (!q.empty()) {
    int e = q.front();
    q.pop_front();
    for (int i = 0; i < n; ++i) {
      if (g[e][i] != g[0][n - 1] && dp[e] + 1 < dp[i]) {
        dp[i] = dp[e] + 1;
        q.push_back(i);
      }
    }
  }
  cout << (max(1, dp[n - 1]) != 1e9 ? max(1, dp[n - 1]) : -1);
}
