#include <bits/stdc++.h>
using namespace std;
int a[500][500], b[500][500], n, m, u, v;
int main() {
  cin >> n >> m;
  memset(a, 50, sizeof a);
  memset(b, 50, sizeof b);
  for (int i = 1; i <= n; i++) a[i][i] = b[i][i] = 0;
  while (m--) cin >> u >> v, a[u][v] = a[v][u] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++)
      if (a[i][j] > 10000) b[i][j] = b[j][i] = 1;
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        a[i][j] = min(a[i][j], a[i][k] + a[k][j]),
        b[i][j] = min(b[i][j], b[i][k] + b[k][j]);
  if (max(a[1][n], b[1][n]) > 10000)
    cout << -1;
  else
    cout << max(a[1][n], b[1][n]);
  return 0;
}
