#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:100000000")
int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return (((a * b) / gcd(a, b))); }
int phi(int n) {
  int result = n;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      while (n % i == 0) n /= i;
      result -= result / i;
    }
  }
  if (n > 1) result -= result / n;
  return result;
}
using namespace std;
const int nmax = 401;
int n, m;
int g[nmax][nmax];
int main() {
  scanf("%d %d", &n, &m);
  int P = 1;
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    if (a > b) swap(a, b);
    if (a == 1 && b == n) P = 2;
    g[a][b] = g[b][a] = 1;
  }
  if (P == 2) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (i != j) g[i][j] ^= 1;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (g[i][j] == 0 && i != j) g[i][j] = 300000000;
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) {
        if (i != j && j != k && i != k)
          g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
      }
  if (g[1][n] == 300000000) g[1][n] = -1;
  printf("%d\n", g[1][n]);
  return 0;
}
