#include <bits/stdc++.h>
using namespace std;
vector<vector<bool> > adjMat;
vector<vector<int> > adjList;
int count(const int x, const vector<int> &par) {
  if (par[x] == -1) return 0;
  return 1 + count(par[x], par);
}
int bfs1(const int s) {
  vector<bool> used(adjMat.size());
  vector<int> par(adjMat.size(), -1);
  used[s] = 1;
  queue<int> q;
  q.emplace(s);
  while (q.size()) {
    int u = q.front();
    q.pop();
    for (int v = 0; v < adjMat.size(); ++v) {
      if (v != u && adjMat[u][v] && !used[v]) {
        q.emplace(v);
        used[v] = 1;
        par[v] = u;
      }
    }
  }
  if (par.back() == -1) return -1;
  return count(par.size() - 1, par);
}
int bfs2(const int s) {
  vector<bool> used(adjList.size());
  vector<int> par(used.size(), -1);
  used[s] = 1;
  queue<int> q;
  q.emplace(s);
  while (q.size()) {
    int u = q.front();
    q.pop();
    for (const int &v : adjList[u]) {
      if (!used[v]) {
        used[v] = 1;
        q.emplace(v);
        par[v] = u;
      }
    }
  }
  if (par.back() == -1) return -1;
  return count(par.size() - 1, par);
}
void buildAdjList() {
  adjList = vector<vector<int> >(adjMat.size());
  for (int u = 0; u < adjMat.size(); ++u) {
    for (int v = 0; v < adjMat.size(); ++v) {
      if (u == v) continue;
      if (!adjMat[u][v]) {
        adjList[u].emplace_back(v);
        adjList[v].emplace_back(u);
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, e;
  cin >> n >> e;
  adjMat = vector<vector<bool> >(n, vector<bool>(n));
  for (int i = 0; i < e; ++i) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    adjMat[u][v] = 1;
    adjMat[v][u] = 1;
  }
  buildAdjList();
  int train = bfs1(0), bus = bfs2(0);
  if (bus == -1 || train == -1)
    cout << -1 << '\n';
  else
    cout << max(train, bus) << '\n';
  return 0;
}
