#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100 * 4 + 10;
int dist[MAX_N];
vector<int> v[MAX_N], v2[MAX_N];
bool check[MAX_N];
queue<int> q, q2;
int n, m;
void bfs(int x) {
  check[x] = true;
  q.push(x);
  dist[x] = 0;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = 0; i < (int)v[x].size(); i++) {
      if (check[v[x][i]] == false) {
        check[v[x][i]] = true;
        q.push(v[x][i]);
        dist[v[x][i]] = dist[x] + 1;
      }
      if (v[x][i] == n - 1) break;
    }
  }
}
void bfs2(int x) {
  check[x] = true;
  q.push(x);
  dist[x] = 0;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = 0; i < (int)v2[x].size(); i++) {
      if (check[v2[x][i]] == false) {
        check[v2[x][i]] = true;
        q.push(v2[x][i]);
        dist[v2[x][i]] = dist[x] + 1;
      }
      if (v2[x][i] == n - 1) break;
    }
  }
}
int main() {
  int a, b, y = 0;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a = a - 1;
    b = b - 1;
    v[a].push_back(b);
    v[b].push_back(a);
    if ((a == 0 && b == n - 1) || (a == n - 1 && b == 0)) y = 1;
  }
  for (int i = 0; i < n; i++) dist[i] = -1;
  if (y == 0)
    bfs(0);
  else if (y == 1) {
    for (int t = 0; t < n; t++) {
      for (int i = 0; i < n; i++) {
        int te = 0;
        for (int j = 0; j < (int)v[t].size(); j++)
          if (v[t][j] != i) te = te + 1;
        if (te == (int)v[t].size() && i != t) {
          v2[t].push_back(i);
          v2[i].push_back(t);
        }
      }
    }
    bfs2(0);
  }
  if (dist[n - 1] == -1)
    cout << -1 << endl;
  else
    cout << dist[n - 1] << endl;
  return 0;
}
