#include <bits/stdc++.h>
using namespace std;
const int MAXN = 405;
int N, M, A, B, vis[MAXN];
bool adj[MAXN][MAXN];
int solve(bool b) {
  queue<int> q, d;
  q.push(0);
  d.push(0);
  while (q.size()) {
    int v = q.front();
    int x = d.front();
    q.pop();
    d.pop();
    if (v == N - 1) return x;
    for (int i = 0; i < N; i++) {
      if (!vis[i] && adj[v][i] == b) {
        vis[i] = 1;
        q.push(i);
        d.push(x + 1);
      }
    }
  }
  return -1;
}
int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> A >> B;
    A -= 1, B -= 1;
    adj[A][B] = 1;
    adj[B][A] = 1;
  }
  cout << solve(!adj[0][N - 1]) << '\n';
}
