#include <bits/stdc++.h>
int h[401][401], q[401][401];
int main() {
  int m, n;
  scanf("%d%d", &n, &m);
  int i, j, k;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      if (i == j) {
        h[i][j] = 0;
      } else
        h[i][j] = 99999999;
    }
  }
  int x, y;
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    h[x][y] = 1;
    h[y][x] = 1;
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      if (i == j) {
        q[i][j] = 0;
      } else if (h[i][j] == 1) {
        q[i][j] = 99999999;
      } else
        q[i][j] = 1;
    }
  }
  for (k = 1; k <= n; k++) {
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= n; j++) {
        if (h[i][j] > h[i][k] + h[k][j]) h[i][j] = h[i][k] + h[k][j];
        if (q[i][j] > q[i][k] + q[k][j]) q[i][j] = q[i][k] + q[k][j];
      }
    }
  }
  if (q[1][n] == 99999999 || h[1][n] == 99999999) {
    printf("-1\n");
  } else {
    if (q[1][n] > h[1][n])
      printf("%d\n", q[1][n]);
    else
      printf("%d\n", h[1][n]);
  }
  return 0;
}
