#include <bits/stdc++.h>
using namespace std;
const int MAXN = (int)500;
const int inf = (int)1e9 + 7;
bool a[MAXN][MAXN];
int used[MAXN];
int n, m;
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    a[x][y] = a[y][x] = true;
  }
  for (int i = 0; i < n + 10; ++i) used[i] = inf;
  if (a[0][n - 1] == true) {
    queue<int> q;
    q.push(0);
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      used[0] = 0;
      for (int i = 0; i < n; ++i) {
        if (a[v][i]) continue;
        if (used[i] <= used[v] + 1) continue;
        used[i] = used[v] + 1;
        q.push(i);
      }
    }
    if (used[n - 1] == inf) {
      cout << -1 << endl;
      return 0;
    }
    cout << used[n - 1] << endl;
  } else {
    queue<int> q;
    q.push(0);
    used[0] = 0;
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (int i = 0; i < n; ++i) {
        if (!a[v][i]) continue;
        if (used[i] <= used[v] + 1) continue;
        used[i] = used[v] + 1;
        q.push(i);
      }
    }
    if (used[n - 1] == inf) {
      cout << -1 << endl;
      return 0;
    }
    cout << used[n - 1] << endl;
  }
  return 0;
}
