#include <bits/stdc++.h>
using namespace std;
int adj[444][444];
int dist[444];
int n, m;
int calc(int p) {
  queue<int> q;
  for (int i = 2; i <= n; ++i) dist[i] = -1;
  dist[1] = 0;
  q.push(1);
  while (!q.empty() && dist[n] == -1) {
    int u = q.front();
    q.pop();
    for (int i = 1; i <= n; ++i) {
      if (adj[u][i] == p && dist[i] == -1) {
        dist[i] = dist[u] + 1;
        q.push(i);
      }
    }
  }
  return dist[n];
}
int main() {
  int a, b;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d %d", &a, &b);
    adj[a][b] = adj[b][a] = 1;
  }
  int x = calc(0);
  int y = calc(1);
  if (x == -1 || y == -1)
    printf("-1\n");
  else
    printf("%d\n", max(x, y));
  return 0;
}
