#include <bits/stdc++.h>
using namespace std;
const int maxn = 400 + 5;
int ar[maxn][maxn];
int par[maxn];
bool mark[maxn];
int n, m, ans = 0;
void chap(int a) {
  if (a != 0) {
    ans++;
    chap(par[a]);
  }
}
void bfs(int a, int x) {
  queue<int> saf;
  saf.push(a);
  mark[a] = true;
  while (!saf.empty()) {
    int next = saf.front();
    mark[next] = true;
    for (int i = 0; i < n; i++) {
      if (ar[next][i] == x && !mark[i] && i != next) {
        saf.push(i);
        par[i] = next;
        mark[i] = true;
      }
    }
    saf.pop();
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    ar[a][b] = 1;
    ar[b][a] = 1;
  }
  int x = 0;
  if (ar[0][n - 1] == 1)
    x = 0;
  else
    x = 1;
  bfs(0, x);
  if (!mark[n - 1]) {
    cout << -1 << endl;
    return 0;
  }
  chap(n - 1);
  cout << ans << endl;
  return 0;
}
