#include <bits/stdc++.h>
using namespace std;
long long XX[8] = {+1, +1, +1, 0, 0, -1, -1, -1};
long long YY[8] = {+1, 0, -1, +1, -1, +1, 0, -1};
long long dx[4] = {-1, 1, 0, 0};
long long dy[4] = {0, 0, 1, -1};
vector<long long> adj[405];
long long a[405][405];
long long level[405], vis[405];
long long n, m, c = 0;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (long long i = 1; i <= m; i++) {
    long long u, v;
    cin >> u >> v;
    a[u][v] = 1;
    a[v][u] = 1;
  }
  memset(vis, 0, sizeof(0));
  queue<long long> q;
  q.push(1);
  vis[1] = 1;
  level[n] = -1;
  while (!q.empty()) {
    long long node = q.front();
    q.pop();
    for (long long i = 1; i <= n; i++) {
      if (!vis[i]) {
        if ((!a[1][n] && a[node][i]) || (a[1][n] && !a[node][i])) {
          level[i] = level[node] + 1;
          vis[i] = 1;
          q.push(i);
        }
      }
    }
  }
  cout << level[n] << '\n';
  return 0;
}
