#include <bits/stdc++.h>
int MAP[405][405], vis[405], dis[405];
int main() {
  int n, m, k, j, i, x, y, now;
  while (scanf("%d%d", &n, &m) != EOF) {
    memset(MAP, 0, sizeof(MAP));
    while (m--) {
      scanf("%d%d", &x, &y);
      MAP[x][y] = MAP[y][x] = 1;
    }
    now = MAP[1][n];
    for (i = 1; i < n; i++) {
      for (j = i + 1; j <= n; j++) {
        if (MAP[i][j] == now) {
          MAP[i][j] = MAP[j][i] = 1000;
        } else {
          MAP[i][j] = MAP[j][i] = 1;
        }
      }
    }
    memset(vis, 0, sizeof(vis));
    memset(dis, 0, sizeof(dis));
    for (i = 2; i <= n; i++) dis[i] = MAP[1][i];
    for (i = 2; i <= n; i++) {
      m = 10001;
      for (j = 2; j <= n; j++) {
        if (!vis[j] && m > dis[j]) {
          k = j;
          m = dis[j];
        }
      }
      vis[k] = 1;
      for (j = 2; j <= n; j++) {
        if (!vis[j] && dis[j] > dis[k] + MAP[k][j]) dis[j] = MAP[k][j] + dis[k];
      }
    }
    if (dis[n] == 1000)
      puts("-1");
    else
      printf("%d\n", dis[n]);
  }
  return 0;
}
