#include <bits/stdc++.h>
using namespace std;
const long long N = 1234567;
const long long MOD = 998244353;
long long adj[500][500], vis[500], n, cost[500];
queue<long long> q;
void solve() {
  long long m, i, j, k, l = 0, r = 0, o = 0, p = 0, ans = 0, x, y, node;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> x >> y;
    adj[x][y] = 1;
    adj[y][x] = 1;
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      if (adj[i][j] == 0) adj[i][j] = 2;
    }
  }
  for (i = 1; i <= n; i++) cost[i] = 1e8;
  cost[1] = 0;
  if (adj[1][n] == 1) {
    q.push(1);
    while (!q.empty()) {
      node = q.front();
      q.pop();
      for (i = 1; i <= n; i++) {
        if (adj[node][i] == 2) {
          if (cost[node] + 1 < cost[i]) {
            cost[i] = 1 + cost[node];
            q.push(i);
          }
        }
      }
    }
    ans = max(1ll, cost[n]);
    if (ans >= n * n) ans = -1;
  } else {
    q.push(1);
    while (!q.empty()) {
      node = q.front();
      q.pop();
      for (i = 1; i <= n; i++) {
        if (adj[node][i] == 1) {
          if (cost[node] + 1 < cost[i]) {
            cost[i] = 1 + cost[node];
            q.push(i);
          }
        }
      }
    }
    ans = max(1ll, cost[n]);
    if (ans >= n * n) ans = -1;
  }
  cout << ans;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  t = 1;
  while (t--) solve();
  return 0;
}
