#include <bits/stdc++.h>
const double PI = 2 * asin(1);
using namespace std;
int mm[405][405];
int length[405];
bitset<405> isvisit;
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int c1, c2;
    cin >> c1 >> c2;
    mm[c1][c2] = mm[c2][c1] = 1;
  }
  int value = mm[1][n] ? 0 : 1;
  isvisit.reset();
  length[1] = 0;
  queue<int> que;
  isvisit.set(1);
  que.push(1);
  while (!que.empty()) {
    int root = que.front();
    que.pop();
    for (int i = 1; i <= n; i++) {
      if (!isvisit[i] && mm[i][root] == value) {
        que.push(i);
        isvisit.set(i);
        length[i] = length[root] + 1;
      }
    }
  }
  if (length[n] == 0)
    cout << -1;
  else
    cout << max(2, length[n]);
}
