#include <bits/stdc++.h>
using namespace std;
const int MAX_N = (int)4e2;
int shortestPath(vector<vector<int> > g, int n) {
  vector<bool> used(n, false);
  vector<int> depth(n, -1);
  queue<int> q;
  used[0] = true;
  depth[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (int i = 0; i < g[cur].size(); i++) {
      int next = g[cur][i];
      if (!used[next]) {
        used[next] = true;
        depth[next] = depth[cur] + 1;
        q.push(next);
      }
    }
  }
  return depth[n - 1];
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  bool mp[MAX_N][MAX_N] = {};
  vector<vector<int> > g1(n), g2(n);
  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--, v--;
    mp[u][v] = true;
    mp[v][u] = true;
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      if (i != j) {
        if (mp[i][j])
          g1[i].push_back(j);
        else
          g2[i].push_back(j);
      }
    }
  if (mp[0][n - 1])
    printf("%d", shortestPath(g2, n));
  else
    printf("%d", shortestPath(g1, n));
  return 0;
}
