#include <bits/stdc++.h>
using namespace std;
int n, m, x, y, cnt, d[405];
queue<int> a;
bool vis[405][405];
void bfs(int val) {
  d[1] = 0;
  a.push(1);
  for (int i = 2; i <= n; i++) d[i] = -1;
  while (!a.empty()) {
    int u = a.front();
    a.pop();
    for (int v = 1; v <= n; v++) {
      if (u != v && vis[u][v] == val && d[v] == -1) {
        d[v] = d[u] + 1;
        vis[u][v] = true;
        a.push(v);
      }
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    vis[x][y] = vis[y][x] = true;
  }
  (vis[1][n]) ? bfs(0) : bfs(1);
  (d[n] != -1) ? printf("%d\n", d[n]) : printf("-1\n");
  return 0;
}
