#include <bits/stdc++.h>
using namespace std;
const int N = 401;
vector<int> adj1[N], adj2[N];
int dis1[N], dis2[N];
bool mark[N][N];
int main() {
  ios::sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adj1[a].push_back(b);
    adj1[b].push_back(a);
    mark[a][b] = 1;
    mark[b][a] = 1;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (mark[i][j] == 0) adj2[i].push_back(j);
    }
  }
  memset(dis1, 0x3f, sizeof dis1);
  memset(dis2, 0x3f, sizeof dis2);
  queue<int> bfs;
  dis1[1] = 0;
  dis2[1] = 0;
  bfs.push(1);
  while (!bfs.empty()) {
    int v = bfs.front();
    bfs.pop();
    for (int u : adj1[v]) {
      if (dis1[u] > dis1[v] + 1) {
        dis1[u] = dis1[v] + 1;
        bfs.push(u);
      }
    }
  }
  bfs.push(1);
  while (!bfs.empty()) {
    int v = bfs.front();
    bfs.pop();
    for (int u : adj2[v]) {
      if (dis2[u] > dis2[v] + 1) {
        dis2[u] = dis2[v] + 1;
        bfs.push(u);
      }
    }
  }
  int ans = max(dis1[n], dis2[n]);
  if (ans >= 10000000) {
    cout << -1;
  } else
    cout << ans;
  return 0;
}
