#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-12;
double __t;
void quit();
int n, m, a[500][500], u[500];
int bfs(int p) {
  queue<int> q;
  u[0] = 1;
  q.push(0);
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (int i = 0; i < (int)n; i++) {
      if (!u[i] && a[cur][i] == p) {
        u[i] = u[cur] + 1;
        q.push(i);
      }
    }
  }
  return u[n - 1] - 1;
}
int main() {
  cin >> n >> m;
  int t1, t2;
  for (int i = 0; i < (int)m; i++) {
    cin >> t1 >> t2;
    t1--;
    t2--;
    a[t1][t2] = 1;
    a[t2][t1] = 1;
  }
  if (a[0][n - 1] == 0)
    cout << bfs(1);
  else
    cout << bfs(0);
  quit();
}
void quit() { exit(0); }
