#include <bits/stdc++.h>
using namespace std;
int n, m, a[2][409], q, w, as;
bool v[2][409], b[409][409];
void dfs(bool x) {
  int s;
  queue<int> qq;
  v[x][1] = 1;
  qq.push(1);
  while (!qq.empty()) {
    s = qq.front();
    qq.pop();
    for (int i = 2; i <= n; i++) {
      if (b[s][i] == x && v[x][i] == 0) {
        a[x][i] = a[x][s] + 1;
        v[x][i] = 1;
        qq.push(i);
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &q, &w);
    b[q][w] = b[w][q] = 1;
  }
  dfs(0);
  dfs(1);
  if (a[0][n] == 0 || a[1][n] == 0)
    as = -1;
  else
    as = max(a[0][n], a[1][n]);
  printf("%d\n", as);
  return 0;
}
