#include <bits/stdc++.h>
using namespace std;
int ma[408][408];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) ma[i][j] = 900000;
  }
  int x, y;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    ma[x][y] = ma[y][x] = 1;
  }
  if (ma[1][n] == 1) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (ma[i][j] == 1)
          ma[i][j] = 900000;
        else
          ma[i][j] = 1;
      }
    }
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (i == j) continue;
        if (ma[i][j] > ma[i][k] + ma[k][j]) ma[i][j] = ma[i][k] + ma[k][j];
      }
    }
  }
  if (ma[1][n] == 900000) ma[1][n] = -1;
  printf("%d\n", ma[1][n]);
}
