#include <bits/stdc++.h>
using namespace std;
int ans = -1, n;
int visited[500];
void bfs(int node, vector<int> v[500]) {
  queue<pair<int, int> > q;
  q.push(make_pair(0, node));
  visited[1] = 1;
  while (!q.empty()) {
    int l = q.front().first;
    int x = q.front().second;
    q.pop();
    if (x == n) {
      ans = l;
      break;
    }
    int i;
    for (i = 0; i < v[x].size(); i++) {
      if (visited[v[x][i]] == 0) {
        visited[v[x][i]] = 1;
        q.push(make_pair(l + 1, v[x][i]));
      }
    }
  }
}
int main() {
  int i, j, k, m;
  cin >> n >> m;
  int u, v, mat[500][500] = {0};
  vector<int> adj[500];
  vector<int> bdj[500];
  for (i = 0; i < m; i++) {
    cin >> u >> v;
    mat[u][v] = 1;
    mat[v][u] = 1;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (i = 1; i < n + 1; i++) {
    for (j = i; j < n + 1; j++) {
      if (mat[i][j] == 0) {
        bdj[i].push_back(j);
        bdj[j].push_back(i);
      }
    }
  }
  if (mat[1][n] == 0) {
    bfs(1, adj);
  } else
    bfs(1, bdj);
  if (ans == -1) {
    cout << "-1" << endl;
  } else {
    ans = max(1, ans);
    cout << ans << endl;
  }
  return 0;
}
