#include <bits/stdc++.h>
using namespace std;
int data[500][500];
int m, n;
int bfs(int x) {
  queue<int> q;
  q.push(1);
  int i, j;
  int vis[500];
  memset(vis, 0, sizeof(vis));
  vis[1] = 1;
  int flag = 0;
  while (!q.empty()) {
    int k = q.front();
    if (k == n) {
      flag = 1;
      break;
    }
    q.pop();
    for (i = 1; i <= n; i++) {
      if (i != k && data[k][i] == x && (!vis[i])) {
        q.push(i);
        vis[i] = vis[k] + 1;
      }
    }
  }
  if (flag) return vis[n] - 1;
  return -1;
}
int main() {
  while (scanf("%d%d", &n, &m) != EOF) {
    memset(data, 0, sizeof(data));
    int i, j;
    for (i = 1; i <= m; i++) {
      int fx, fy;
      scanf("%d%d", &fx, &fy);
      data[fx][fy] = 1;
      data[fy][fx] = 1;
    }
    if (n == 2) {
      printf("-1\n");
      continue;
    }
    int ans = 0;
    if (data[1][n] == 1) {
      ans = bfs(0);
    } else {
      ans = bfs(1);
    }
    if (ans == -1)
      printf("-1\n");
    else {
      printf("%d\n", ans);
    }
  }
  return 0;
}
