#include <bits/stdc++.h>
using namespace std;
int n, m;
int cnt = 0;
int ans;
int a[404][404];
int u[404];
int BFS(int x) {
  int i, l;
  int fr;
  queue<int> q;
  q.push(1);
  u[1] = true;
  while (!q.empty()) {
    fr = q.front();
    cnt = u[fr];
    q.pop();
    if (a[fr][n] == x) return cnt;
    for (i = 1; i < n; i++) {
      l = 0;
      if (a[fr][i] == x && u[i] == false) {
        u[i] = cnt + 1;
        q.push(i);
        l = 1;
      }
    }
    if (l == 1) cnt++;
  }
  return -1;
}
int main() {
  int x, y, i;
  scanf("%d%d", &n, &m);
  for (i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    a[x][y] = a[y][x] = true;
  }
  ans = BFS(a[1][n] ^ 1);
  printf("%d\n", ans);
  return 0;
}
