#include <bits/stdc++.h>
const long long int MOD = 1E9 + 7;
using namespace std;
int n, m;
int a[412][412];
int b[412][412];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) a[i][j] = 4000;
  int d, c;
  while (m--) {
    cin >> d >> c;
    d--;
    c--;
    a[d][c] = 1;
    a[c][d] = 1;
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (a[i][j] == 4000) {
        b[i][j] = 1;
      } else
        b[i][j] = 4000;
    }
  }
  for (int k = 0; k < n; ++k) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
      }
    }
  }
  int ans1 = a[0][n - 1];
  for (int k = 0; k < n; ++k) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        b[i][j] = min(b[i][j], b[i][k] + b[k][j]);
      }
    }
  }
  int ans2 = b[0][n - 1];
  if (ans1 < 4000 && ans2 < 4000) {
    printf("%d\n", max(ans1, ans2));
  } else
    printf("-1\n");
  return 0;
}
