#include <bits/stdc++.h>
using namespace std;
bool graph[405][405];
int dist[2][405];
int main() {
  int i, j, u, v, m, n, tt, type, ans[2];
  queue<pair<int, int> > q;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> u >> v;
    graph[u][v] = graph[v][u] = true;
  }
  tt = 1 - graph[1][n];
  q.push(pair<int, int>(tt, 1));
  dist[tt][1] = 1;
  while (!q.empty()) {
    type = q.front().first, u = q.front().second;
    q.pop();
    if (u == n) {
      ans[type] = dist[type][n];
      continue;
    }
    for (v = 1; v <= n; v++) {
      if (graph[u][v] == type && !dist[type][v]) {
        q.push(pair<int, int>(type, v));
        dist[type][v] = 1 + dist[type][u];
      }
    }
  }
  cout << dist[tt][n] - 1 << endl;
  return 0;
}
