#include <bits/stdc++.h>
using namespace std;
void bfs(vector<int> *G, int *dist, int n) {
  bool vis[n];
  fill(vis, vis + n, false);
  deque<int> q;
  q.push_back(0);
  dist[0] = 0;
  while (!q.empty()) {
    int v = q.front();
    q.pop_front();
    vis[v] = true;
    for (auto u : G[v]) {
      if (!vis[u]) {
        q.push_back(u);
        dist[u] = dist[v] + 1;
        vis[u] = true;
      }
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> railG[n], roadG[n];
  bool is_railroad = false;
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    railG[u - 1].push_back(v - 1);
    railG[v - 1].push_back(u - 1);
    if ((u == 1 && v == n) || (u == n && v == 1)) {
      is_railroad = true;
    }
  }
  for (int i = 0; i < n; ++i) {
    bool used[n];
    fill(used, used + n, true);
    used[i] = false;
    for (auto u : railG[i]) {
      used[u] = false;
    }
    for (int j = 0; j < n; ++j) {
      if (used[j]) {
        roadG[i].push_back(j);
      }
    }
  }
  int dist[n];
  fill(dist, dist + n, -1);
  if (is_railroad) {
    bfs(roadG, dist, n);
  } else {
    bfs(railG, dist, n);
  }
  cout << dist[n - 1] << endl;
  return 0;
}
