#include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX;
const long long mod = 1e9 + 7;
int a[410][410];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  int n, m, ans = 1;
  cin >> n >> m;
  vector<vector<int> > g(n), t(n);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    t[x].push_back(y);
    t[y].push_back(x);
    a[x][y] = a[y][x] = 1;
  }
  if (m == n * (n - 1) / 2) {
    cout << "-1\n";
    return 0;
  }
  if (a[0][n - 1]) {
    for (int i = 0; i < n; i++)
      for (int j = i + 1; j < n; j++)
        if (!a[i][j])
          a[i][j] = a[j][i] = 1, g[i].push_back(j), g[j].push_back(i);
  } else
    g = t;
  vector<int> u(n, 0), d(n, INF);
  u[0] = 1;
  d[0] = 0;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < g[v].size(); i++)
      if (!u[g[v][i]]) {
        q.push(g[v][i]);
        u[g[v][i]] = 1;
        d[g[v][i]] = d[v] + 1;
      }
  }
  ans = max(ans, d[n - 1]);
  if (ans == INF)
    cout << "-1\n";
  else
    cout << ans << "\n";
  return 0;
}
