#include <bits/stdc++.h>
using namespace std;
const int OO = 0x3f3f3f3f;
int n, m, u, v;
int mat[402][402];
vector<int> tadj[402];
vector<int> badj[402];
int tdis[402];
int bdis[402];
bool canBeARoute = 1;
void tbfs(int src) {
  memset(tdis, OO, sizeof tdis);
  tdis[src] = 0;
  queue<int> q;
  q.push(src);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int v : tadj[u]) {
      if (tdis[v] == OO) {
        tdis[v] = tdis[u] + 1;
        q.push(v);
      }
    }
  }
}
void bbfs(int src) {
  memset(bdis, OO, sizeof bdis);
  bdis[src] = 0;
  queue<int> q;
  q.push(src);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int v : badj[u]) {
      if (bdis[v] == OO) {
        if (bdis[u] + 1 == tdis[v]) continue;
        bdis[v] = bdis[u] + 1;
        q.push(v);
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d%d", &u, &v);
    tadj[u].push_back(v);
    tadj[v].push_back(u);
    mat[u][v] = 1;
    mat[v][u] = 1;
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (mat[i][j] == 0) {
        badj[i].push_back(j);
        badj[j].push_back(i);
        mat[i][j] = 1;
        mat[j][i] = 1;
      }
    }
  }
  tbfs(1);
  int train = tdis[n];
  bbfs(1);
  int bus = bdis[n];
  int mx = max(train, bus);
  if (mx == OO)
    cout << "-1" << endl;
  else
    cout << mx << endl;
  return 0;
}
