#include <bits/stdc++.h>
using namespace std;
int bfs_pathLength(int sz, int **g, int source, int end, int flag) {
  int cost[sz];
  for (int i = 0; i < sz; i++) {
    cost[i] = -1;
  }
  cost[source] = 0;
  queue<int> q;
  q.push(source);
  while (!q.empty()) {
    int cur = q.front();
    for (int i = 0; i < sz; i++) {
      if (g[cur][i] == flag && cost[i] == -1) {
        cost[i] = cost[cur] + 1;
        if (i == end) {
          return cost[i];
        }
        q.push(i);
      }
    }
    q.pop();
  }
  return cost[end];
}
int main() {
  int n, e;
  cin >> n >> e;
  int **g;
  g = new int *[n];
  for (int i = 0; i < n; i++) {
    g[i] = new int[n];
    for (int j = 0; j < n; j++) {
      g[i][j] = 0;
    }
  }
  for (int i = 0; i < e; i++) {
    int a, b;
    cin >> a >> b;
    g[a - 1][b - 1] = 1;
    g[b - 1][a - 1] = 1;
  }
  int cost = g[0][n - 1] ? bfs_pathLength(n, g, 0, n - 1, 0)
                         : bfs_pathLength(n, g, 0, n - 1, 1);
  cout << cost << '\n';
  return 0;
}
