#include <bits/stdc++.h>
using namespace std;
int a[405][405];
int q[405 * 405], d[405], t, h, ans;
bool vis[405];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  while (m--) {
    int u, v;
    scanf("%d %d", &u, &v);
    a[u][v] = a[v][u] = 1;
  }
  t = h = 0;
  q[h++] = 1;
  vis[1] = 1;
  memset(d, -1, sizeof d);
  d[1] = 0;
  while (t < h) {
    int u = q[t++];
    for (int i = 1; i <= n; i++) {
      if (vis[i] || a[u][i] == 0) continue;
      d[i] = d[u] + 1;
      vis[i] = 1;
      q[h++] = i;
    }
  }
  if (d[n] == -1) {
    puts("-1");
    return 0;
  }
  ans = d[n];
  t = h = 0;
  q[h++] = 1;
  memset(vis, 0, sizeof vis);
  vis[1] = 1;
  memset(d, -1, sizeof d);
  d[1] = 0;
  while (t < h) {
    int u = q[t++];
    for (int i = 1; i <= n; i++) {
      if (vis[i] || a[u][i]) continue;
      d[i] = d[u] + 1;
      vis[i] = 1;
      q[h++] = i;
    }
  }
  if (d[n] == -1) {
    puts("-1");
    return 0;
  }
  ans = max(ans, d[n]);
  cout << ans;
}
