#include <bits/stdc++.h>
int n, m, g[405][405] = {}, d[405], ans;
int main() {
  scanf("%d%d", &n, &m);
  while (m--) {
    int u, v;
    scanf("%d%d", &u, &v);
    g[u][v] = g[v][u] = 1;
  }
  std::fill(d, d + n + 5, 2e9);
  std::queue<int> q;
  q.push(1);
  d[1] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int v = 1; v <= n; v++)
      if (g[u][v] && d[v] == 2e9) {
        d[v] = d[u] + 1;
        q.push(v);
      }
  }
  if (d[n] == 2e9) {
    printf("-1\n");
    return 0;
  } else
    ans = d[n];
  std::fill(d, d + n + 5, 2e9);
  q.push(1);
  d[1] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int v = 1; v <= n; v++)
      if (!g[u][v] && d[v] == 2e9) {
        d[v] = d[u] + 1;
        q.push(v);
      }
  }
  if (d[n] == 2e9) {
    printf("-1\n");
    return 0;
  }
  printf("%d\n", (int)std::max(ans, d[n]));
}
