#include <bits/stdc++.h>
using namespace std;
int n, m, dist[401], A[401][401], x, y, tmp;
queue<int> q;
int main() {
  memset(dist, -1, sizeof(dist));
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &x, &y);
    A[x][y] = 1;
    A[y][x] = 1;
  }
  if (A[1][n]) {
    q.push(1);
    dist[1] = 0;
    while (q.size()) {
      tmp = q.front();
      q.pop();
      if (A[tmp][n] == 0) {
        printf("%d", dist[tmp] + 1);
        return (0);
      }
      for (int i = 1; i <= n; i++) {
        if (A[tmp][i] == 0 && dist[i] == -1) {
          dist[i] = dist[tmp] + 1;
          q.push(i);
        }
      }
    }
  } else if (A[1][n] == 0) {
    q.push(1);
    dist[1] = 0;
    while (q.size()) {
      tmp = q.front();
      q.pop();
      if (A[tmp][n] == 1) {
        printf("%d", dist[tmp] + 1);
        return (0);
      }
      for (int i = 1; i <= n; i++) {
        if (A[tmp][i] == 1 && dist[i] == -1) {
          dist[i] = dist[tmp] + 1;
          q.push(i);
        }
      }
    }
  }
  printf("%d", -1);
}
