#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[500][500] = {0};
int re1[500] = {0};
bool used[500];
int d[500], d1[500];
int s, s1;
int ss = 0, ss1 = 0;
int z[1000];
int min(int i, int j) {
  if (i < j)
    return i;
  else
    return j;
}
void dijkstra(int s) {
  fill(d, d + n, 0x3f3f3f3f);
  fill(used, used + n, false);
  d[s] = 0;
  while (true) {
    int v = -1;
    for (int u = 0; u < n; u++) {
      if (!used[u] && (v == -1 || d[u] < d[v])) v = u;
    }
    if (v == -1) break;
    used[v] = true;
    for (int u = 0; u < n; u++) d[u] = min(d[u], d[v] + a[v][u]);
  }
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf(" %d %d", &x, &y);
    a[x - 1][y - 1] = 1;
    a[y - 1][x - 1] = 1;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (a[i][j] == 0) a[i][j] = 0x3f3f3f3f;
    }
  }
  dijkstra(0);
  s = d[n - 1];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (a[i][j] == 0x3f3f3f3f)
        a[i][j] = 1;
      else if (a[i][j] == 1)
        a[i][j] = 0x3f3f3f3f;
    }
  }
  dijkstra(0);
  s1 = d[n - 1];
  if (s == 0x3f3f3f3f || s1 == 0x3f3f3f3f)
    puts("-1");
  else {
    if (s > s1)
      printf("%d\n", s);
    else
      printf("%d\n", s1);
  }
  return 0;
}
