#include <bits/stdc++.h>
using namespace std;
int infinity = 10000000;
int railway[401][401];
int calculatedistance(int start, int end, bool bus) {
  bool visitedtown[401];
  int distance[401];
  queue<int> myQueue;
  for (int i = 1; i <= 400; i++) visitedtown[i] = false;
  for (int i = 1; i <= 400; i++) distance[i] = infinity;
  distance[start] = 0;
  visitedtown[start] = true;
  myQueue.push(start);
  while (!myQueue.empty()) {
    int town = myQueue.front();
    myQueue.pop();
    for (int i = 1; i <= end; i++)
      if (i != town) {
        if (bus) {
          if (!railway[town][i])
            if (!visitedtown[i])
              if (distance[town] + 1 < distance[i]) {
                distance[i] = distance[town] + 1;
                visitedtown[i] = true;
                myQueue.push(i);
              }
        } else {
          if (railway[town][i])
            if (!visitedtown[i])
              if (distance[town] + 1 < distance[i]) {
                distance[i] = distance[town] + 1;
                visitedtown[i] = true;
                myQueue.push(i);
              }
        }
      }
  }
  return distance[end];
}
int main() {
  int n, m, u, v;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) railway[i][j] = 0;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    railway[u][v] = 1;
    railway[v][u] = 1;
  }
  int bdis = calculatedistance(1, n, true);
  int tdis = calculatedistance(1, n, false);
  if (bdis == infinity || tdis == infinity) {
    cout << "-1";
    return 0;
  }
  if (bdis != tdis) {
    cout << max(bdis, tdis);
    return 0;
  }
  int disb = infinity;
  for (int i = 2; i < n; i++)
    disb = min(disb,
               calculatedistance(1, i, false) + calculatedistance(i, n, false));
  int dist;
  for (int i = 2; i < n; i++)
    dist = min(dist,
               calculatedistance(1, i, true) + calculatedistance(i, n, true));
  cout << min(disb, dist);
  return 0;
}
