#include <bits/stdc++.h>
#pragma warning(disable : 4996 6031 6385 6386 6297 26451)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
long long n, m, a[401][401], opt, dis[401];
class A {
 public:
  long long dis, id;
  bool operator<(const A& tmp) const { return dis > tmp.dis; }
};
inline void bfs() {
  priority_queue<A> q;
  q.push({dis[1] = 0, 1});
  while (!q.empty()) {
    A tmp = q.top();
    q.pop();
    for (long long i = 1; i <= n; i += 1)
      if (dis[i] > tmp.dis + 1 && a[tmp.id][i] == opt)
        q.push({dis[i] = tmp.dis + 1, i});
  }
  cout << (dis[n] == 0x7f7f7f7f7f7f7f7f ? -1 : dis[n]);
}
signed main() {
  cin >> n >> m;
  memset(dis, 0x7f, sizeof dis);
  for (long long i = 1; i <= m; i += 1) {
    long long u, v;
    cin >> u >> v;
    a[u][v] = a[v][u] = 1;
  }
  for (long long i = 1; i <= n; i += 1) a[i][i] = -1;
  opt = 1 ^ a[1][n];
  bfs();
  return 0;
}
