#include <bits/stdc++.h>
using namespace std;
const int MAXN = 450;
const int ziad = 10000 * 10000 * 5;
int n, m;
bool adj[MAXN][MAXN];
int dis[MAXN];
void dfs(int u) {
  for (int i = 0; i < n; i++) {
    if (adj[i][u]) {
      if (dis[i] > dis[u] + 1) {
        dis[i] = dis[u] + 1;
        dfs(i);
      }
    }
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    adj[a][b] = true;
    adj[b][a] = true;
  }
  for (int i = 0; i < n; i++) {
    dis[i] = ziad;
  }
  dis[0] = 0;
  if (adj[0][n - 1]) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        adj[i][j] = !adj[i][j];
      }
    }
  }
  dfs(0);
  if (dis[n - 1] == ziad) {
    cout << -1 << endl;
  } else {
    cout << dis[n - 1] << endl;
  }
  return 0;
}
