#include <bits/stdc++.h>
using namespace std;
const long long INFF = 0x3f3f3f3f3f3f3f3fLL;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
const int MOD = int(1e9) + 9;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
const double EPS = 1e-9;
const double OO = 1e20;
const int MAXN = 500;
int a[MAXN][MAXN];
int main() {
  int n, m, x, y;
  cin >> n >> m;
  memset(a, INF, sizeof(a));
  for (int i = 0; i < m; ++i) {
    cin >> x >> y;
    a[x][y] = a[y][x] = 1;
  }
  if (a[1][n] == 1)
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j)
        if (i != j && a[i][j] == INF)
          a[i][j] = 1;
        else
          a[i][j] = INF;
  for (int i = 1; i <= n; ++i)
    for (int j = 0; j <= n; ++j)
      if (i != j)
        for (int k = 0; k <= n; ++k)
          if (i != k && j != k && a[j][i] + a[i][k] < a[j][k])
            a[j][k] = a[j][i] + a[i][k];
  if (a[1][n] == INF)
    cout << -1 << endl;
  else
    cout << a[1][n] << endl;
  return 0;
}
