#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<bool>> adjM(N, vector<bool>(N, false));
  for (int m = 0; m < M; m++) {
    int u, v;
    cin >> u >> v;
    adjM[u - 1][v - 1] = true;
    adjM[v - 1][u - 1] = true;
  }
  vector<bool> vis(N, false);
  queue<pair<int, int>> q;
  q.push({0, 0});
  vis[0] = true;
  int time = 0;
  bool possible = true;
  while (!q.empty()) {
    int node, t;
    tie(node, t) = q.front();
    if (node == N - 1) {
      time = max(t, time);
      break;
    }
    for (int i = 0; i < N; i++) {
      if ((!vis[i]) && adjM[node][i] == true) {
        vis[i] = true;
        q.push({i, t + 1});
      }
    }
    q.pop();
  }
  if (q.empty()) possible = false;
  for (int i = 0; i < N; i++) vis[i] = false;
  while (!q.empty()) q.pop();
  q.push({0, 0});
  vis[0] = true;
  while (!q.empty()) {
    int node, t;
    tie(node, t) = q.front();
    if (node == N - 1) {
      time = max(t, time);
      break;
    }
    for (int i = 0; i < N; i++) {
      if ((!vis[i]) && adjM[node][i] == false) {
        vis[i] = true;
        q.push({i, t + 1});
      }
    }
    q.pop();
  }
  if (q.empty()) possible = false;
  if (possible)
    cout << time;
  else
    cout << -1;
  return 0;
}
