#include <bits/stdc++.h>
using namespace std;
void bfs(vector<int> adjList[], int n) {
  vector<bool> visited(n + 1, false);
  queue<int> q;
  q.push(1);
  visited[1] = true;
  int distance = 0;
  while (!q.empty()) {
    distance++;
    int queueSize = q.size();
    for (int i = 0; i < queueSize; i++) {
      int v = q.front();
      q.pop();
      for (int neighbour : adjList[v]) {
        if (!visited[neighbour]) {
          q.push(neighbour);
          visited[neighbour] = true;
          if (neighbour == n) {
            cout << distance << endl;
            return;
          }
        }
      }
    }
  }
  cout << -1 << endl;
}
void solve(set<pair<int, int>>& s, int n) {
  vector<int> adjList[n + 1];
  if (s.find({1, n}) == s.end()) {
    for (auto& p : s) {
      int u = p.first, v = p.second;
      adjList[u].push_back(v);
      adjList[v].push_back(u);
    }
  } else {
    for (int u = 1; u <= n; u++) {
      for (int v = u + 1; v <= n; v++) {
        if (s.find({u, v}) == s.end()) {
          adjList[u].push_back(v);
          adjList[v].push_back(u);
        }
      }
    }
  }
  bfs(adjList, n);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t = 1;
  while (t--) {
    int n, m;
    cin >> n >> m;
    set<pair<int, int>> s;
    for (int i = 0; i < m; i++) {
      int u, v;
      cin >> u >> v;
      if (u > v) swap(u, v);
      s.insert({u, v});
    }
    solve(s, n);
  }
}
