#include <bits/stdc++.h>
using namespace std;
const int maxNode = 405;
int n, m, u, v;
int ch[maxNode];
int dist[maxNode];
int g[405][405];
int shortestPath(int type) {
  memset(ch, 0, sizeof(ch));
  ch[1] = 1;
  dist[1] = 0;
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (int i = 1; i <= n; i++) {
      if (g[u][i] == type && !ch[i]) {
        ch[i] = 1;
        dist[i] = dist[u] + 1;
        if (i == n) {
          return dist[i];
        }
        q.push(i);
      }
    }
  }
  return -1;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &u, &v);
    g[u][v] = g[v][u] = 1;
  }
  int bus = shortestPath(0);
  int train = shortestPath(1);
  if (bus == -1 || train == -1)
    printf("-1\n");
  else
    printf("%d\n", max(bus, train));
  return 0;
}
