#include <bits/stdc++.h>
using namespace std;
int graph[401][401];
int dist[401];
const int INF = 1 << 30;
int n;
int minDist(int type) {
  for (int i = 0; i < n; i++) dist[i] = INF;
  dist[0] = 0;
  priority_queue<pair<int, int> > q;
  q.push(pair<int, int>(0, 0));
  while (!q.empty()) {
    pair<int, int> x = q.top();
    q.pop();
    if (dist[x.second] < x.first) continue;
    dist[x.second] = x.first;
    for (int i = 0; i < n; i++)
      if (graph[x.second][i] == type && dist[i] > dist[x.second] + 1)
        q.push(pair<int, int>(dist[x.second] + 1, i));
  }
  return (dist[n - 1] == INF) ? -1 : dist[n - 1];
}
int main() {
  int m;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) graph[i][j] = 1;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    graph[u][v] = 2;
    graph[v][u] = 2;
  }
  if (graph[0][n - 1] == 1)
    cout << minDist(2) << endl;
  else
    cout << minDist(1) << endl;
}
