#include <bits/stdc++.h>
using namespace std;
const long long inf = 1000000000ll;
const long long inf64 = inf * inf;
const long long base = inf + 7;
const long double pi = acos(-1.0);
bool solve() {
  int n, m;
  scanf("%d %d", &n, &m);
  vector<vector<int> > g(n + 1, vector<int>(n + 1, 0));
  for (int u, v, i = 0; i < m; i++) {
    scanf("%d %d", &u, &v);
    g[u][v] = g[v][u] = 1;
  }
  if (g[1][n] == 1) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        g[i][j] ^= 1;
      }
    }
  }
  vector<int> dist(n + 1, inf);
  vector<char> in(n + 1, 0);
  dist[1] = 0;
  in[1] = 1;
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    in[v] = 0;
    for (int i = 1; i <= n; i++) {
      if (g[v][i] && i != v) {
        if (dist[i] > dist[v] + 1) {
          dist[i] = dist[v] + 1;
          if (!in[i]) {
            in[i] = 1;
            q.push(i);
          }
        }
      }
    }
  }
  cout << (dist[n] == inf ? -1 : dist[n]) << '\n';
  return true;
}
int main() {
  solve();
  return 0;
}
