#include <bits/stdc++.h>
const long long int MOD = 1E9 + 7;
using namespace std;
int n, m;
int a[412][412];
bool bl[412];
int bfs(int k) {
  if (k == n - 1) return 0;
  bl[k] = true;
  list<pair<int, int> > q;
  q.push_back(make_pair(k, 0));
  int temp = 1;
  while (!q.empty()) {
    k = q.front().first;
    for (int i = 0; i < n; ++i) {
      if (a[i][k] == 1 && !bl[i]) {
        if (i == n - 1) return (q.front().second + 1);
        bl[i] = true;
        q.push_back(make_pair(i, q.front().second + 1));
      }
    }
    q.pop_front();
  }
  return -1;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) a[i][j] = 0;
  int b, c;
  while (m--) {
    cin >> b >> c;
    b--;
    c--;
    a[b][c] = 1;
    a[c][b] = 1;
  }
  int ans1 = bfs(0);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) a[i][j] = !a[i][j];
  for (int i = 0; i < 412; ++i) {
    bl[i] = false;
  }
  int ans2 = bfs(0);
  if (ans1 == -1 || ans2 == -1) {
    cout << -1 << endl;
    return 0;
  }
  printf("%d\n", max(ans1, ans2));
  return 0;
}
