#include <bits/stdc++.h>
using namespace std;
int i, j, l, m, n, p, q, r, a[405][405], dis[405], x, y;
vector<int> T[405], R[405];
void dfs(vector<int> v[], int node, int d) {
  dis[node] = d;
  for (int i = 0; i < v[node].size(); i++) {
    int t = v[node][i];
    if (dis[t] == -1 || dis[t] > (d + 1)) dfs(v, t, d + 1);
  }
}
int main() {
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> x >> y;
    T[x].push_back(y);
    T[y].push_back(x);
    a[x][y] = 1;
    a[y][x] = 1;
  }
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      if (a[i][j] == 0) {
        R[i].push_back(j);
        R[j].push_back(i);
      }
  memset(dis, -1, sizeof(dis));
  dfs(T, 1, 0);
  p = dis[n];
  memset(dis, -1, sizeof(dis));
  dfs(R, 1, 0);
  q = dis[n];
  if (p == -1 || q == -1)
    cout << -1;
  else
    cout << max(p, q);
}
