#include <bits/stdc++.h>
using namespace std;
const int maxn = 500;
bool vis[maxn][maxn], mark[maxn];
int h[maxn];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m, v, u;
  cin >> n >> m;
  for (long long int i = 0; i < m; i++) {
    cin >> v >> u;
    vis[v][u] = true;
    vis[u][v] = true;
  }
  memset(h, -1, sizeof h);
  h[1] = 0;
  mark[1] = true;
  queue<int> q;
  q.push(1);
  while (q.size()) {
    v = q.front();
    q.pop();
    for (int i = 1; i <= n; i++) {
      if (i != v && vis[v][i] != vis[1][n] && !mark[i]) {
        h[i] = h[v] + 1;
        mark[i] = true;
        q.push(i);
      }
    }
  }
  cout << h[n];
  return 0;
}
