#include <bits/stdc++.h>
using namespace std;
const int maxn = 400 + 10;
vector<int> vec[maxn];
vector<int> car[maxn];
int dist[maxn];
bool check[maxn];
bool way[maxn][maxn];
void BFS(int v) {
  int y;
  queue<int> q;
  q.push(v);
  while (!q.empty()) {
    y = q.front();
    check[y] = true;
    for (int i = 0; i < vec[y].size(); i++) {
      if (check[vec[y][i]] == false) {
        q.push(vec[y][i]);
        dist[vec[y][i]] = dist[y] + 1;
        check[vec[y][i]] = true;
      }
    }
    q.pop();
  }
}
void BFSs(int v) {
  int y;
  queue<int> q;
  q.push(v);
  while (!q.empty()) {
    y = q.front();
    check[y] = true;
    for (int i = 0; i < car[y].size(); i++) {
      if (check[car[y][i]] == false) {
        q.push(car[y][i]);
        dist[car[y][i]] = dist[y] + 1;
        check[car[y][i]] = true;
      }
    }
    q.pop();
  }
}
int main() {
  int n, m, a, b;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--;
    b--;
    way[a][b] = true;
    way[b][a] = true;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (way[i][j] && i != j) {
        vec[i].push_back(j);
        vec[j].push_back(i);
      } else if (!way[i][j] && i != j) {
        car[i].push_back(j);
        car[j].push_back(i);
      }
    }
  }
  int maax;
  BFS(0);
  if (!check[n - 1]) {
    cout << "-1";
    return 0;
  }
  maax = dist[n - 1];
  for (int i = 0; i < n; i++) {
    dist[i] = 0;
    check[i] = false;
  }
  BFSs(0);
  if (!check[n - 1]) {
    cout << "-1";
    return 0;
  }
  cout << max(maax, dist[n - 1]);
}
