#include <bits/stdc++.h>
using namespace std;
int i, j, k, n, m;
int a[410][410];
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    a[x][y] = a[y][x] = 1;
  }
  if (a[1][n]) {
    for (i = 1; i <= n; i++)
      for (j = 1; j <= n; j++) a[i][j] = 1 - a[i][j];
  }
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      if (!a[i][j]) a[i][j] = 410;
  for (k = 1; k <= n; k++)
    for (i = 1; i <= n; i++)
      for (j = 1; j <= n; j++) a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
  printf("%d\n", a[1][n] == 410 ? -1 : a[1][n]);
  return 0;
}
