#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> gt[410];
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    gt[a].push_back(b);
    gt[b].push_back(a);
  }
  int dt[410] = {};
  queue<int> q;
  dt[0] = 1;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int to : gt[v]) {
      if (!dt[to]) {
        dt[to] = dt[v] + 1;
        q.push(to);
      }
    }
  }
  vector<int> ga[410];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (find(gt[i].begin(), gt[i].end(), j) == gt[i].end()) {
        ga[i].push_back(j);
      }
    }
  }
  int da[410] = {};
  da[0] = 1;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int to : ga[v]) {
      if (!da[to]) {
        da[to] = da[v] + 1;
        q.push(to);
      }
    }
  }
  if (!dt[n - 1] || !da[n - 1]) {
    cout << -1;
  } else {
    cout << max(dt[n - 1], da[n - 1]) - 1;
  }
}
