#include <bits/stdc++.h>
using namespace std;
queue<int> q;
int v[1123][1123];
int chek[1123];
int ans[1123];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    v[x][y] = 1;
    v[y][x] = 1;
  }
  if (v[1][n]) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (i != j) {
          if (v[i][j] == 0) {
            v[i][j] = 1;
          } else {
            v[i][j] = 0;
          }
        }
      }
    }
  }
  q.push(1);
  while (!q.empty()) {
    int j = q.front();
    for (int i = 1; i <= n; i++) {
      if (v[i][j]) {
        if (!chek[i]) {
          q.push(i);
          ans[i] = ans[j] + 1;
          chek[i] = 1;
        }
      }
    }
    q.pop();
  }
  if (chek[n]) {
    cout << ans[n];
    return 0;
  }
  cout << "-1";
  return 0;
}
