#include <bits/stdc++.h>
using namespace std;
int n, m, x, y;
bool a[401][401];
bool viz[401];
int dist[401];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cerr.tie(0);
  cout.tie(0);
  cin >> n >> m;
  while (m--) {
    cin >> x >> y;
    a[x][y] = a[y][x] = 1;
  }
  queue<int> q;
  q.push(1);
  viz[1] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 1; i <= n; i++)
      if (!viz[i] && (a[u][i] ^ a[1][n])) {
        q.push(i);
        viz[i] = 1;
        dist[i] = dist[u] + 1;
      }
  }
  return cout << ((!viz[n]) ? -1 : max(1, dist[n])), 0;
}
