#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const int MAX_N = 400;
int n, m;
bool g[MAX_N][MAX_N];
int bfs(bool b) {
  vector<int> depth(n, INT_MAX);
  vector<bool> processed(n);
  depth[0] = 0;
  queue<int> qu;
  qu.push(0);
  while (!qu.empty()) {
    int u = qu.front();
    qu.pop();
    if (processed[u]) continue;
    processed[u] = true;
    for (int v = 0; v < n; ++v) {
      if (g[u][v] ^ b || depth[v] != INT_MAX) continue;
      depth[v] = depth[u] + 1;
      qu.push(v);
    }
  }
  return depth[n - 1];
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    g[u][v] = true;
    g[v][u] = true;
  }
  int ans = max(bfs(0), bfs(1));
  if (ans == INT_MAX) ans = -1;
  cout << ans << '\n';
  return 0;
}
