#include <bits/stdc++.h>
using namespace std;
set<pair<int, int>> rail[100005], bus[100005];
vector<int> raildist(100005, INT_MAX), busdist(100005, INT_MAX);
void shortestPath(int src, vector<int>& dist, int flag) {
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      pq;
  fill(dist.begin(), dist.end(), INT_MAX);
  pq.push(make_pair(0, src));
  dist[src] = 0;
  while (!pq.empty()) {
    int u = pq.top().second;
    pq.pop();
    set<pair<int, int>>::iterator i;
    if (flag == 1)
      for (i = rail[u].begin(); i != rail[u].end(); ++i) {
        int v = (*i).first;
        int weight = (*i).second;
        if (dist[v] > dist[u] + weight) {
          dist[v] = dist[u] + weight;
          pq.push(make_pair(dist[v], v));
        }
      }
    else
      for (i = bus[u].begin(); i != bus[u].end(); ++i) {
        int v = (*i).first;
        int weight = (*i).second;
        if (dist[v] > dist[u] + weight) {
          dist[v] = dist[u] + weight;
          pq.push(make_pair(dist[v], v));
        }
      }
  }
}
int main() {
  long long n, m, a, b;
  cin >> n >> m;
  while (m--) {
    cin >> a >> b;
    rail[a].insert(make_pair(b, 1));
    rail[b].insert(make_pair(a, 1));
  }
  vector<int> visited(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    fill(visited.begin(), visited.end(), 0);
    visited[i] = 1;
    for (auto a : rail[i]) visited[a.first] = 1;
    for (int j = 1; j <= n; j++)
      if (!visited[j]) {
        bus[i].insert(make_pair(j, 1));
        bus[j].insert(make_pair(i, 1));
      }
  }
  shortestPath(1, raildist, 1);
  shortestPath(1, busdist, 0);
  for (int i = 2; i < n; i++)
    if ((raildist[i] == busdist[i])) {
      cout << "-1\n";
      return 0;
    }
  if (raildist[n] == INT_MAX || busdist[n] == INT_MAX) {
    cout << "-1\n";
    return 0;
  }
  cout << max(raildist[n], busdist[n]) << "\n";
}
