#include <bits/stdc++.h>
using namespace std;
vector<int> g1[405], g2[405];
int n;
bool visit[405], gg[405][405];
int bfs(vector<int> g[405]) {
  priority_queue<pair<int, int> > q;
  memset(visit, 0, sizeof visit);
  q.push(make_pair(0, 1));
  while (!q.empty()) {
    int u = q.top().second;
    int w = q.top().first;
    q.pop();
    if (u == n) {
      return -w;
    }
    if (!visit[u]) {
      visit[u] = true;
      for (int i = 0; i < g[u].size(); i++) {
        int v = g[u][i];
        q.push(make_pair(w - 1, v));
      }
    }
  }
  return -1;
}
int main() {
  int m;
  scanf("%d %d", &n, &m);
  int x, y;
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &x, &y);
    g1[x].push_back(y);
    g1[y].push_back(x);
    gg[x][y] = true;
    gg[y][x] = true;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (!gg[i][j]) {
        g2[i].push_back(j);
        g2[j].push_back(i);
      }
    }
  }
  int a = bfs(g1);
  int b = bfs(g2);
  if (a != b && a != -1 && b != -1) {
    cout << max(a, b) << endl;
    return 0;
  } else if (a == -1 || b == -1) {
    cout << -1 << endl;
    return 0;
  }
  return 0;
}
