#include <bits/stdc++.h>
using namespace std;
const int MAXN = 450;
int cities[MAXN][MAXN], dist[MAXN];
int main() {
  int n, m, u, v, comp, temp;
  cin >> n >> m;
  memset(dist, -1, sizeof(dist));
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    cities[u][v] = 1;
    cities[v][u] = 1;
  }
  comp = 1 - cities[1][n];
  queue<int> q;
  q.push(1);
  dist[1] = 0;
  while (!q.empty()) {
    temp = q.front();
    q.pop();
    for (int i = 1; i <= n; i++) {
      if (cities[temp][i] == comp && dist[i] == -1) {
        dist[i] = dist[temp] + 1;
        q.push(i);
      }
    }
  }
  cout << dist[n] << endl;
  return 0;
}
