#include <bits/stdc++.h>
using namespace std;
int a, b, n, m;
int M[405][405];
int T[405];
queue<pair<int, int> > Q;
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    M[a][b] = 1;
    M[b][a] = 1;
  }
  Q.push(pair<int, int>(1, 0));
  int kek = M[1][n];
  while (!Q.empty()) {
    pair<int, int> yeni;
    yeni = Q.front();
    Q.pop();
    if (T[yeni.first]) continue;
    T[yeni.first] = 1;
    if (yeni.first == n) {
      printf("%d", yeni.second);
      return 0;
    }
    for (int i = 1; i <= n; i++)
      if (M[yeni.first][i] != kek) Q.push(pair<int, int>(i, yeni.second + 1));
  }
  printf("-1");
}
