#include <bits/stdc++.h>
using namespace std;
int n, m;
bool mat[400 + 5][400 + 5];
int dist[400 + 5];
int bfs(int src, int des, int id) {
  queue<int> q;
  q.push(src);
  dist[src] = 0;
  while (!q.empty()) {
    int f = q.front();
    q.pop();
    for (int i = 1; i <= n; i++) {
      if (f == i) continue;
      if (mat[f][i] == id && dist[i] == -1) {
        dist[i] = dist[f] + 1;
        q.push(i);
      }
    }
  }
  return dist[n];
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1, u, v; i <= m; i++) {
    scanf("%d %d", &u, &v);
    mat[u][v] = 1;
    mat[v][u] = 1;
  }
  memset(dist, -1, sizeof(dist));
  if (mat[1][n] == 1)
    printf("%d\n", bfs(1, n, 0));
  else
    printf("%d\n", bfs(1, n, 1));
  return 0;
}
