#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, m;
  cin >> n >> m;
  bool v[n + 1][n + 1];
  for (long long int i = 1; i <= n; i++)
    for (long long int j = 1; j <= n; j++) v[i][j] = 0;
  bool rails = false;
  for (long long int i = 0; i < m; i++) {
    long long int a, b;
    cin >> a >> b;
    v[a][b] = 1;
    v[b][a] = 1;
    if ((a == 1 && b == n) || (a == n && b == 1)) rails = true;
  }
  if (rails) {
    for (long long int i = 1; i <= n; i++)
      for (long long int j = 1; j <= n; j++) v[i][j] = !v[i][j];
  }
  vector<long long int> seen(n + 1, 0), dist(n + 1, -1);
  queue<long long int> q;
  q.push(1);
  dist[1] = 0;
  seen[1] = 1;
  while (!q.empty()) {
    long long int x = q.front();
    q.pop();
    for (long long int i = 1; i <= n; i++) {
      if (!v[x][i]) continue;
      if (seen[i]) continue;
      seen[i] = 1;
      dist[i] = dist[x] + 1;
      q.push(i);
    }
  }
  cout << dist[n] << endl;
  return 0;
}
