#include <bits/stdc++.h>
using namespace std;
bool path[401][401];
int n, m, u, v, ans1;
void dfs(int s, bool st, int *H, int h) {
  H[s] = H[s] == -1 ? h : min(H[s], h);
  if (s == n - 1) return;
  for (int i = 0; i < n; i++) {
    if (i != s && path[s][i] == st) {
      if (H[i] == -1)
        dfs(i, st, H, h + 1);
      else if (H[i] > h + 1)
        dfs(i, st, H, h + 1);
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    u--, v--;
    path[u][v] = true;
    path[v][u] = true;
  }
  int H[n];
  memset(H, -1, sizeof(H));
  dfs(0, true, H, 0);
  if (H[n - 1] == -1) {
    cout << -1 << endl;
    return 0;
  } else
    ans1 = H[n - 1];
  memset(H, -1, sizeof(H));
  dfs(0, false, H, 0);
  if (H[n - 1] == -1)
    cout << -1 << endl;
  else
    cout << max(H[n - 1], ans1) << endl;
  return 0;
}
