#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:100000000")
#pragma GCC optimize("O500")
using namespace std;
const bool db = false;
int n;
int g[500][500];
bool used[500];
int d1, d2;
int ds[500];
void dfs1(int v) {
  deque<int> d;
  memset(ds, 0, sizeof(ds));
  used[v] = true;
  d.push_back(v);
  int temp;
  while (d.size()) {
    temp = d.front();
    d.pop_front();
    for (int i = 1; i <= n; i++)
      if (g[temp][i] && !used[i]) {
        used[i] = true;
        ds[i] = ds[temp] + 1;
        d.push_back(i);
      }
  }
  d1 = ds[n];
}
void dfs2(int v) {
  deque<int> d;
  memset(ds, 0, sizeof(ds));
  used[v] = true;
  d.push_back(v);
  int temp;
  while (d.size()) {
    temp = d.front();
    d.pop_front();
    for (int i = 1; i <= n; i++)
      if (!g[temp][i] && !used[i]) {
        used[i] = true;
        ds[i] = ds[temp] + 1;
        d.push_back(i);
      }
  }
  d2 = ds[n];
}
int main() {
  ios::sync_with_stdio(0);
  int m, a, b;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> a >> b;
    g[a][b] = g[b][a] = 1;
  }
  memset(used, 0, sizeof(used));
  bool can1, can2;
  can1 = can2 = 0;
  dfs1(1);
  if (used[n]) can1 = 1;
  memset(used, 0, sizeof(used));
  dfs2(1);
  if (used[n]) can2 = 1;
  if (can1 && can2)
    cout << max(d1, d2) << '\n';
  else
    cout << "-1\n";
  return 0;
}
