#include <bits/stdc++.h>
const int N = 2350;
const int INF = 0x3f3f3f3f;
using namespace std;
const int M = 450;
int mp1[M][M], mp2[M][M];
int main() {
  int n, m, a, b;
  while (scanf("%d%d", &n, &m) == 2) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (i == j)
          mp1[i][j] = mp2[i][j] = 0;
        else
          mp1[i][j] = mp2[i][j] = INF;
      }
    }
    for (int i = 1; i <= m; i++) {
      scanf("%d%d", &a, &b);
      mp1[a][b] = mp1[b][a] = 1;
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (i == j) continue;
        if (mp1[i][j] == 1) {
          mp2[i][j] = INF;
        } else {
          mp2[i][j] = 1;
        }
      }
    }
    if (mp1[1][n] == 1) {
      for (int k = 1; k <= n; k++) {
        for (int i = 1; i <= n; i++) {
          for (int j = 1; j <= n; j++) {
            if (mp2[i][j] > mp2[i][k] + mp2[k][j])
              mp2[i][j] = mp2[i][k] + mp2[k][j];
          }
        }
      }
      if (mp2[1][n] == INF)
        printf("-1\n");
      else
        printf("%d\n", mp2[1][n]);
    } else {
      for (int k = 1; k <= n; k++) {
        for (int i = 1; i <= n; i++) {
          for (int j = 1; j <= n; j++) {
            if (mp1[i][j] > mp1[i][k] + mp1[k][j])
              mp1[i][j] = mp1[i][k] + mp1[k][j];
          }
        }
      }
      if (mp1[1][n] == INF)
        printf("-1\n");
      else
        printf("%d\n", mp1[1][n]);
    }
  }
}
