#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int TAM = 400 + 5;
int nodes;
int arestas;
int G[TAM][TAM];
int dist[TAM];
int proc[TAM];
int dij(int origem, int destino, int flag) {
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      pq;
  memset(dist, INF, sizeof dist);
  dist[origem] = 0;
  pq.emplace(dist[origem], origem);
  while (true) {
    pair<int, int> davez = make_pair(-1, -1);
    while (!pq.empty()) {
      pair<int, int> atual = pq.top();
      pq.pop();
      if (!proc[atual.second]) {
        davez = atual;
        break;
      }
    }
    if (davez.first == -1) break;
    if (davez.second == destino) return davez.first;
    int atual = davez.second;
    proc[atual] = 1;
    int dis = dist[atual];
    for (int i = 1; i <= nodes; i++) {
      if (G[atual][i] == flag) {
        if (dis + 1 < dist[i]) {
          dist[i] = dis + 1;
          pq.emplace(dist[i], i);
        }
      }
    }
  }
  return -1;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> nodes >> arestas;
  int u, v;
  for (int i = 0; i < arestas; i++) {
    cin >> u >> v;
    G[u][v] = 1;
    G[v][u] = 1;
  }
  int flag = 0;
  if (G[1][nodes] == 0) flag = 1;
  cout << dij(1, nodes, flag) << endl;
  return 0;
}
