#include <bits/stdc++.h>
using namespace std;
int f[405];
int d[405][405];
vector<int> a[405];
int n, m;
void DFS() {
  for (int i = 1; i <= n; i++) f[i] = 1000000;
  priority_queue<pair<int, int> > q;
  q.push(make_pair(0, 1));
  f[1] = 0;
  while (!q.empty()) {
    int v = q.top().second;
    int w = -q.top().first;
    q.pop();
    if (f[v] < w) continue;
    for (int i = 0; i < a[v].size(); i++) {
      int u = a[v][i];
      if (f[u] > f[v] + 1) {
        f[u] = f[v] + 1;
        q.push(make_pair(-f[u], u));
      }
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    d[x][y] = d[y][x] = 1;
  }
  if (d[1][n]) {
    for (int i = 1; i <= n; i++)
      for (int j = i + 1; j <= n; j++) {
        if (d[i][j] == 0) {
          a[i].push_back(j);
          a[j].push_back(i);
        }
      }
  } else {
    for (int i = 1; i <= n; i++)
      for (int j = i + 1; j <= n; j++) {
        if (d[i][j] == 1) {
          a[i].push_back(j);
          a[j].push_back(i);
        }
      }
  }
  DFS();
  if (f[n] == 1000000)
    cout << -1;
  else
    cout << f[n];
}
