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