#include <bits/stdc++.h>
using namespace std;
int G[500][500], vis[500], dis[500], maps[500][500], dis1[500];
;
int n, m;
void Inn() {
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      G[i][j] = 0xfffffff;
      maps[i][j] = 0xfffffff;
    }
    dis[i] = 0xfffffff;
    dis1[i] = 0xfffffff;
    vis[i] = 0;
  }
}
int Dij1(int s, int e) {
  dis[s] = 0;
  for (int i = 1; i <= n; i++) {
    int Min = 0xfffffff, dist;
    for (int j = 1; j <= n; j++) {
      if (Min > dis[j] && vis[j] == 0) {
        Min = dis[j];
        dist = j;
      }
    }
    vis[dist] = 1;
    for (int j = 1; j <= n; j++) {
      dis[j] = min(dis[j], dis[dist] + G[dist][j]);
    }
  }
  return dis[e];
}
int Dij2(int s, int e) {
  memset(vis, 0, sizeof(vis));
  dis1[s] = 0;
  for (int i = 1; i <= n; i++) {
    int Min = 0xfffffff, dist;
    for (int j = 1; j <= n; j++) {
      if (Min > dis1[j] && vis[j] == 0) {
        Min = dis1[j];
        dist = j;
      }
    }
    vis[dist] = 1;
    for (int j = 1; j <= n; j++) {
      dis1[j] = min(dis1[j], dis1[dist] + maps[dist][j]);
    }
  }
  return dis1[e];
}
int main() {
  while (scanf("%d %d", &n, &m) != EOF) {
    Inn();
    int u, v;
    for (int i = 0; i < m; i++) {
      scanf("%d %d", &u, &v);
      G[u][v] = G[v][u] = 1;
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (i == j) continue;
        if (G[i][j] == 0xfffffff) maps[i][j] = 1;
      }
    }
    int k1 = Dij1(1, n);
    int k2 = Dij2(1, n);
    if (k1 == 0xfffffff || k2 == 0xfffffff)
      printf("-1\n");
    else
      printf("%d\n", max(k1, k2));
  }
  return 0;
}
