#include <bits/stdc++.h>
void dj(int n);
const int V = 99999999;
typedef struct pp {
  int x;
  int y;
} ss;
using namespace std;
int flag[500];
int jj[500][500];
int d[500];
int main(void) {
  int n, i, j, k, p, q, N, M;
  while (scanf("%d %d", &N, &M) != EOF) {
    memset(flag, 0, sizeof(flag));
    for (i = 0; i < 450; i++) {
      for (j = 0; j < 450; j++) {
        jj[i][j] = V;
      }
    }
    for (i = 0; i < M; i++) {
      scanf("%d %d", &p, &q);
      jj[p][q] = 1;
      jj[q][p] = 1;
    }
    dj(N);
    int ss = d[N];
    for (i = 0; i < 450; i++) {
      for (j = 0; j < 450; j++) {
        if (jj[i][j] == V) {
          jj[i][j] = 1;
        } else
          jj[i][j] = V;
      }
    }
    dj(N);
    int vv = d[N];
    int uu = max(vv, ss);
    if (uu >= V) {
      printf("-1\n");
    } else
      printf("%d\n", uu);
  }
  return 0;
}
void dj(int n) {
  fill(d, d + n + 1, V);
  fill(flag, flag + n + 1, 0);
  d[1] = 0;
  int i, j, k, p, q;
  while (true) {
    int l = -1;
    for (i = 1; i <= n; i++) {
      if (flag[i] == 0 && (l == -1 || d[i] < d[l])) {
        l = i;
      }
    }
    if (l == -1) {
      break;
    }
    flag[l] = 1;
    for (i = 1; i <= n; i++) {
      d[i] = min(d[i], d[l] + jj[l][i]);
    }
  }
}
