#include <bits/stdc++.h>
using namespace std;
const int N = 1001;
vector<int> railway[N], roadways[N];
int visited[N];
bool noRailway(int i, int j) {
  for (int x : railway[i]) {
    if (x == j) return false;
  }
  return true;
}
void dfs(int flag) {
  visited[1] = 0;
  queue<int> q;
  q.push(1);
  if (flag == 1) {
    while (!q.empty()) {
      int node = q.front();
      q.pop();
      for (int x : railway[node]) {
        if (visited[x] == 0) {
          q.push(x);
          visited[x] = visited[node] + 1;
        }
      }
    }
  } else {
    while (!q.empty()) {
      int node = q.front();
      q.pop();
      for (int x : roadways[node]) {
        if (visited[x] == 0) {
          q.push(x);
          visited[x] = visited[node] + 1;
        }
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  int m;
  cin >> n >> m;
  int directRoute = 0;
  for (long long i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    if (min(u, v) == 1 && max(u, v) == n) directRoute = 1;
    railway[u].push_back(v);
    railway[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (noRailway(i, j) && i != j) {
        roadways[i].push_back(j);
        roadways[j].push_back(i);
      }
    }
  }
  if (directRoute == 1) {
    dfs(0);
  } else {
    dfs(1);
  }
  int timeTaken = visited[n];
  if (timeTaken == 0) {
    cout << -1 << "\n";
  } else {
    cout << timeTaken << "\n";
  }
  return 0;
}
