#include <bits/stdc++.h>
using namespace std;
int n, m, graf[1000][1000], met[1000], kolejka[10000], x, y, v, k, p;
void dfs(int w) {
  for (int a = 1; a <= n; a++) {
    if (graf[w][a] == v && !met[a]) {
      kolejka[++p] = a;
      met[a] = met[w] + 1;
    }
  }
  while (k < p) {
    dfs(kolejka[++k]);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (int a = 1; a <= m; a++) {
    cin >> x >> y;
    graf[x][y] = 1;
    graf[y][x] = 1;
  }
  if (n == 2) {
    cout << "-1";
    return 0;
  }
  if (graf[1][n] == 1)
    v = 0;
  else
    v = 1;
  met[1] = 1;
  dfs(1);
  if (met[n] > 0)
    cout << met[n] - 1;
  else
    cout << "-1";
  return 0;
}
