#include <bits/stdc++.h>
using namespace std;
vector<int> G[405];
vector<int> R[405];
int a[405][405];
int bfs(int n, int src) {
  int val;
  queue<int> q;
  int taken[405] = {0}, distance[405] = {0};
  q.push(src);
  taken[src] = 1;
  distance[src] = 0;
  while (!q.empty()) {
    int u = q.front();
    for (int i = 0; i < G[u].size(); i++) {
      int v = G[u][i];
      if (!taken[v]) {
        distance[v] = distance[u] + 1;
        taken[v] = 1;
        q.push(v);
      }
    }
    q.pop();
  }
  return distance[n];
}
int bfs1(int n, int src) {
  int val;
  queue<int> q;
  int taken[405] = {0}, distance[405] = {0};
  q.push(src);
  taken[src] = 1;
  distance[src] = 0;
  while (!q.empty()) {
    int u = q.front();
    for (int i = 0; i < R[u].size(); i++) {
      int v = R[u][i];
      if (!taken[v]) {
        distance[v] = distance[u] + 1;
        taken[v] = 1;
        q.push(v);
      }
    }
    q.pop();
  }
  return distance[n];
}
int main() {
  int x, y, node, src, edge, r, rr, p, u, v, j, i, cnt;
  cin >> node >> edge;
  cnt = 0;
  if (edge == 0) {
    cout << -1;
    return 0;
  }
  for (int i = 0; i < edge; i++) {
    cin >> x >> y;
    a[x][y] = 1;
    a[y][x] = 1;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  for (i = 1; i <= node; i++) {
    for (j = 1; j <= node; j++) {
      if (a[i][j] == 0) {
        R[i].push_back(j);
        R[j].push_back(i);
      }
    }
  }
  src = 1;
  u = bfs(node, src);
  v = bfs1(node, src);
  if (u == 0 || v == 0)
    cout << -1;
  else
    cout << max(u, v);
}
