#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e5 + 5, mod = 1e9 + 7;
long long n;
struct Edge {
  long long to;
};
vector<vector<Edge>> g1, g2;
long long BFS1() {
  queue<long long> q;
  vector<long long> d(n), used(n);
  auto add = [&](long long v, long long D) {
    if (!used[v]) {
      used[v] = 1;
      d[v] = D;
      q.push(v);
    }
  };
  add(1, 0);
  while (!q.empty()) {
    long long v = q.front();
    q.pop();
    for (auto e : g1[v]) {
      add(e.to, d[v] + 1);
    }
  }
  if (d[n - 1] == 0) {
    return -1;
  } else {
    return d[n - 1];
  }
}
long long BFS2() {
  queue<long long> q;
  vector<long long> d(n), used(n);
  auto add = [&](long long v, long long D) {
    if (!used[v]) {
      used[v] = 1;
      d[v] = D;
      q.push(v);
    }
  };
  add(1, 0);
  while (!q.empty()) {
    long long v = q.front();
    q.pop();
    for (auto e : g2[v]) {
      add(e.to, d[v] + 1);
    }
  }
  if (d[n - 1] == 0) {
    return -1;
  } else {
    return d[n - 1];
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long m;
  cin >> n >> m;
  n++;
  g1.resize(n);
  g2.resize(n);
  while (m--) {
    long long u, v;
    cin >> u >> v;
    Edge e;
    e.to = v;
    g1[u].push_back(e);
    e.to = u;
    g1[v].push_back(e);
  }
  bool flag = 0;
  for (long long i = 1; i < n; i++) {
    for (long long j = 1; j < n; j++) {
      for (auto e : g1[i]) {
        if (e.to == j) {
          flag = 1;
          break;
        }
      }
      if (flag || i == j) {
        flag = 0;
        continue;
      }
      Edge e;
      e.to = j;
      g2[i].push_back(e);
    }
  }
  auto a1 = BFS1();
  auto a2 = BFS2();
  if (a1 == -1 || a2 == -1) {
    printf("-1\n");
  } else {
    printf("%i\n", max(a1, a2));
  }
}
