#include <bits/stdc++.h>
using namespace std;
vector<long long> railways[1000];
vector<long long> roads[1000];
int check[1000][1000];
long long inbus[1000];
long long inrail[1000];
long long raildist[1000][1000];
long long roaddist[1000][1000];
queue<long long> q;
queue<long long> q1;
long long visited[1000];
int main() {
  long long i, j, k, n, m, u, v;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> u >> v;
    check[u][v] = 1;
    check[v][u] = 1;
    railways[u].push_back(v);
    railways[v].push_back(u);
    raildist[u][v] = 1;
    raildist[v][u] = 1;
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      if (i != j) {
        if (check[i][j] != 1) {
          roads[i].push_back(j);
          roaddist[i][j] = 1;
        }
      }
    }
  }
  q.push(1);
  visited[1] = 1;
  while (!q.empty()) {
    long long top = q.front();
    q.pop();
    for (i = 0; i < roads[top].size(); i++) {
      long long el = roads[top][i];
      if (!visited[el]) {
        inbus[el] = inbus[top] + 1;
        visited[el] = 1;
        q.push(el);
      }
    }
  }
  int flag1 = 0;
  if (!visited[n]) {
    flag1 = 1;
  }
  memset(visited, 0, sizeof(visited));
  q1.push(1);
  visited[1] = 1;
  while (!q1.empty()) {
    long long top = q1.front();
    q1.pop();
    for (i = 0; i < railways[top].size(); i++) {
      long long el = railways[top][i];
      if (!visited[el]) {
        inrail[el] = inrail[top] + 1;
        visited[el] = 1;
        q1.push(el);
      }
    }
  }
  int flag2 = 0;
  if (!visited[n]) {
    flag2 = 1;
  }
  for (k = 0; k < n; k++) {
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        if (raildist[i][k] + raildist[k][j] < raildist[i][j])
          raildist[i][j] = raildist[i][k] + raildist[k][j];
      }
    }
  }
  for (k = 0; k < n; k++) {
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        if (roaddist[i][k] + roaddist[k][j] < roaddist[i][j])
          roaddist[i][j] = roaddist[i][k] + roaddist[k][j];
      }
    }
  }
  if (flag1 || (flag2)) {
    cout << "-1" << endl;
  } else {
    cout << max(inbus[n], inrail[n]);
  }
  return 0;
}
