#include <bits/stdc++.h>
using namespace std;
const int N = 3e5;
const int M = 405;
const int oo = 1e9 + 777;
vector<int> g[N];
int n, m;
int d[N];
int r[M][M];
bool kiemtra;
void enter() {
  kiemtra = false;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    if ((u == 1 && v == n) || (u == n && v == 1)) kiemtra = true;
    r[u][v] = 1;
    r[v][u] = 1;
  }
  if (kiemtra == true) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (r[i][j] == 0) g[i].push_back(j);
  } else {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (r[i][j] == 1) g[i].push_back(j);
  }
  for (int i = 1; i <= n; i++) g[i].push_back(0);
}
void dijkstra() {
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      pq;
  for (int i = 1; i <= n; i++) d[i] = oo;
  d[1] = 0;
  pq.push(pair<int, int>(0, 1));
  while (!pq.empty()) {
    int u = pq.top().second;
    int du = pq.top().first;
    pq.pop();
    if (d[u] != du) continue;
    for (int i = 0; int v = g[u][i]; i++)
      if (du + 1 < d[v]) {
        d[v] = du + 1;
        pq.push(pair<int, int>(d[v], v));
      }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  enter();
  dijkstra();
  if (d[n] == oo)
    cout << -1;
  else
    cout << d[n];
  return 0;
}
