#include <bits/stdc++.h>
using namespace std;
const int maxn = 405;
const int inf = 1e9;
int boxt[maxn][maxn];
int boxb[maxn][maxn];
int n, m;
int dijkstra(int box[][maxn]) {
  int vis[maxn];
  int dist[maxn];
  for (int i = 1; i <= n; i++) {
    vis[i] = 0;
    dist[i] = box[1][i];
  }
  vis[1] = 1;
  for (int i = 2; i <= n; i++) {
    int Min = inf;
    int k = -1;
    for (int j = 1; j <= n; j++)
      if (!vis[j] && Min > dist[j]) Min = dist[k = j];
    if (k == -1) break;
    vis[k] = 1;
    for (int j = 1; j <= n; j++)
      if (!vis[j] && dist[k] + box[k][j] < dist[j])
        dist[j] = dist[k] + box[k][j];
  }
  return dist[n];
}
int main() {
  cin >> n >> m;
  int u, v;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      boxt[i][j] = inf;
      boxb[i][j] = inf;
    }
  for (int i = 1; i <= m; i++) {
    cin >> u >> v;
    boxt[u][v] = boxt[v][u] = 1;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (i != j && boxt[i][j] == inf) boxb[i][j] = 1;
  int result = max(dijkstra(boxt), dijkstra(boxb));
  if (result == inf)
    cout << -1 << endl;
  else
    cout << result << endl;
  return 0;
}
