#include <bits/stdc++.h>
using namespace std;
int BFS(vector<int> G[], int n) {
  queue<int> q;
  vector<bool> visited(n + 1, false);
  vector<int> distance(n + 1, -1);
  visited[1] = true;
  distance[1] = 0;
  q.push(1);
  while (!q.empty()) {
    int s = q.front();
    q.pop();
    for (auto u : G[s]) {
      if (visited[u]) continue;
      visited[u] = true;
      distance[u] = distance[s] + 1;
      q.push(u);
    }
  }
  return distance[n];
}
int main(int argc, char const *argv[]) {
  int n, m, x, y;
  bool g = false;
  cin >> n >> m;
  vector<int> G1[n + 1];
  vector<int> G2[n + 1];
  vector<vector<bool> > G(n + 1, vector<bool>(n + 1, false));
  while (m--) {
    cin >> x >> y;
    G[x][y] = true;
    G[y][x] = true;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (G[i][j]) {
        G1[i].push_back(j);
      } else {
        G2[i].push_back(j);
      }
  if (G[1][n])
    printf("%d\n", BFS(G2, n));
  else
    printf("%d\n", BFS(G1, n));
  return 0;
}
