#include <bits/stdc++.h>
using namespace std;
const long long IINF = 9223372036854775807;
const int INF = 0x7fffffff;
const int inf = 0x3f3f3f3f;
const int maxn = 200005;
const int N = 105;
inline void read(int &x) {
  int f = 1;
  x = 0;
  char s = getchar();
  while (s < '0' || s > '9') {
    if (s == '-') f = -1;
    s = getchar();
  }
  while (s >= '0' && s <= '9') {
    x = x * 10 + s - '0';
    s = getchar();
  }
  x *= f;
}
int n, m, d[405][405], f[405][405];
int floyd() {
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
    }
  }
  if (d[1][n] == inf) d[1][n] = -1;
  return d[1][n];
}
int main() {
  memset(d, inf, sizeof(d));
  read(n), read(m);
  for (int i = 0, u, v; i < m; i++) {
    read(u), read(v);
    d[u][v] = d[v][u] = 1;
  }
  if (d[1][n] == 1) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++)
        if (d[i][j] == 1)
          d[i][j] = inf;
        else
          d[i][j] = 1;
    }
  }
  cout << floyd();
  return 0;
}
