#include <bits/stdc++.h>
using namespace std;
int bfs(int node, int visited[], vector<int> *graph, int n) {
  int x;
  int level[n + 1];
  memset(level, 0, sizeof(level));
  level[node] = 0;
  visited[node] = 1;
  queue<int> q;
  q.push(node);
  while (!q.empty()) {
    x = q.front();
    q.pop();
    for (int i = 0; i < (int)graph[x].size(); i++) {
      if (visited[graph[x][i]] != 1) {
        q.push(graph[x][i]);
        visited[graph[x][i]] = 1;
        level[graph[x][i]] = level[x] + 1;
      }
    }
  }
  if (level[n] == 0) level[n] = -1;
  return level[n];
}
int main() {
  int n, m, u, v;
  cin >> n >> m;
  vector<int> graphr[n + 1];
  vector<int> graphrd[n + 1];
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    graphr[u].push_back(v);
    graphr[v].push_back(u);
  }
  multiset<int> ms;
  multiset<int>::iterator it;
  for (int i = 1; i <= n; i++) {
    ms.clear();
    for (int k = 1; k <= n; k++) {
      if (k != i) ms.insert(k);
    }
    for (int j = 0; j < (int)graphr[i].size(); j++) ms.erase(graphr[i][j]);
    for (it = ms.begin(); it != ms.end(); it++) graphrd[i].push_back(*it);
  }
  int visitedr[n + 1], visitedrd[n + 1];
  memset(visitedr, 0, sizeof(visitedr));
  memset(visitedrd, 0, sizeof(visitedrd));
  int p = bfs(1, visitedr, graphr, n);
  int q = bfs(1, visitedrd, graphrd, n);
  if (p == -1 || q == -1)
    cout << -1 << endl;
  else
    cout << max(p, q) << endl;
  return 0;
}
