#include <bits/stdc++.h>
using namespace std;
long long int n, visited[401] = {0}, visited_c[401] = {0};
bool a[401][401] = {{false}}, b[401][401] = {{false}};
void bfs(int v) {
  int mark = 0;
  list<int> queue;
  visited[v] = mark;
  mark = 1;
  queue.push_back(v);
  queue.push_back(-1);
  while ((int)queue.size() != 1) {
    v = queue.front();
    queue.pop_front();
    if (v == -1) {
      mark++;
      queue.push_back(-1);
      continue;
    }
    for (int i = 2; i <= n; i++) {
      if (a[v][i] == true && visited[i] == 0 && i != v) {
        queue.push_back(i);
        visited[i] = mark;
      }
    }
  }
}
void bfs_c(int v) {
  int mark = 0;
  list<int> queue;
  visited_c[v] = mark;
  mark = 1;
  queue.push_back(v);
  queue.push_back(-1);
  while ((int)queue.size() != 1) {
    v = queue.front();
    queue.pop_front();
    if (v == -1) {
      mark++;
      queue.push_back(-1);
      continue;
    }
    for (int i = 2; i <= n; i++) {
      if (a[v][i] == false && visited_c[i] == 0 && i != v) {
        queue.push_back(i);
        visited_c[i] = mark;
      }
    }
  }
}
int main() {
  long long int m, l, k;
  cin >> n >> m;
  for (long long int i = 0; i < m; i++) {
    cin >> l >> k;
    a[l][k] = true;
    a[k][l] = true;
  }
  bfs(1);
  bfs_c(1);
  int vi = visited[n], vic = visited_c[n];
  if (vi == 0 || vic == 0) {
    cout << "-1" << endl;
  } else {
    cout << max(vi, vic) << endl;
  }
  return 0;
}
