#include <bits/stdc++.h>
using namespace std;
vector<long long> V[401];
vector<long long> V1[401];
bool visited[401];
long long level[401];
long long level1[401];
void bfs(long long x) {
  long long i;
  queue<long long> Q;
  visited[x] = 1;
  Q.push(x);
  while (!Q.empty()) {
    long long x = Q.front();
    Q.pop();
    for (i = 0; i < V[x].size(); i++) {
      if (!visited[V[x][i]]) {
        visited[V[x][i]] = 1;
        Q.push(V[x][i]);
        level[V[x][i]] = level[x] + 1;
      }
    }
  }
}
void bfs1(long long x) {
  long long i;
  queue<long long> Q;
  visited[x] = 1;
  Q.push(x);
  while (!Q.empty()) {
    long long x = Q.front();
    Q.pop();
    for (i = 0; i < V1[x].size(); i++) {
      if (!visited[V1[x][i]]) {
        visited[V1[x][i]] = 1;
        Q.push(V1[x][i]);
        level1[V1[x][i]] = level1[x] + 1;
      }
    }
  }
}
long long Power(long long a, long long b) {
  long long result = 1;
  while (b) {
    if (b % 2) {
      result = (result * a) % 1000000007;
    }
    b = b >> 1;
    a = (a * a) % 1000000007;
  }
  return result;
}
int main() {
  std::ios_base::sync_with_stdio(false);
  long long N, M, i, j;
  cin >> N >> M;
  while (M--) {
    long long x, y;
    cin >> x >> y;
    V[x].push_back(y);
    V[y].push_back(x);
  }
  for (i = 1; i < N + 1; i++) {
    bool visited1[N + 1];
    memset(visited1, 0, sizeof(visited1));
    for (j = 0; j < V[i].size(); j++) {
      visited1[V[i][j]] = 1;
    }
    visited1[i] = 1;
    for (j = 1; j < N + 1; j++) {
      if (!visited1[j]) {
        V1[i].push_back(j);
      }
    }
  }
  bfs(1);
  memset(visited, 0, sizeof(visited));
  bfs1(1);
  if (!level[N] || !level1[N]) {
    cout << "-1";
  } else {
    cout << max(level1[N], level[N]);
  }
  return 0;
}
