#include <bits/stdc++.h>
using namespace std;
int n, m, used[2000], a[500][500], x, y;
void bfs(int x) {
  queue<int> q;
  q.push(1);
  int d[2000];
  for (int i = 2; i <= n; i++) d[i] = 10000;
  used[1] = 1;
  d[1] = 0;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 1; i <= n; i++)
      if (!used[i] && a[v][i] != x) {
        q.push(i);
        used[i] = 1;
        d[i] = d[v] + 1;
      }
  }
  if (d[n] == 10000)
    cout << "-1";
  else
    cout << d[n];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    a[x][y] = 1;
    a[y][x] = 1;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (a[i][j] != 1 && i != j) a[i][j] = 2;
  bfs(a[1][n]);
}
