#include <bits/stdc++.h>
using namespace std;
bool p[1001][1001], used[1001], used1[1010];
int n, a, l = 1, ax, an, f = 1, d[1001], d1[1001];
vector<int> g[1001], k[1001];
int main() {
  int m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> ax >> an;
    p[ax][an] = true;
    p[an][ax] = true;
    g[ax].push_back(an);
    g[an].push_back(ax);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (!p[i][j]) {
        k[i].push_back(j);
        k[j].push_back(i);
      }
    }
  }
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int v = q.front();
    used[v] = true;
    q.pop();
    for (int i = 0; i < g[v].size(); i++) {
      int to = g[v][i];
      if (!used[to]) {
        used[to] = true;
        q.push(to);
        d[to] = d[v] + 1;
      }
    }
  }
  q.push(1);
  while (!q.empty()) {
    int v = q.front();
    used1[v] = true;
    q.pop();
    for (int i = 0; i < k[v].size(); i++) {
      int to = k[v][i];
      if (!used1[to]) {
        used1[to] = true;
        q.push(to);
        d1[to] = d1[v] + 1;
      }
    }
  }
  if (d[n] == 0 || d1[n] == 0)
    cout << -1;
  else
    cout << max(d[n], d1[n]);
}
