#include <bits/stdc++.h>
using namespace std;
int dis[401], used[401][401];
vector<int> ady[401];
int n, m;
void reset() {
  for (int i = 1; i <= 401; i++) ady[i].clear();
}
int bfs(int inicio) {
  memset(dis, 10000, sizeof(dis));
  queue<int> q;
  q.push(inicio);
  dis[inicio] = 0;
  while (!q.empty()) {
    int actual = q.front();
    q.pop();
    if (actual == n) return dis[actual];
    for (int i = 0; i < ady[actual].size(); i++) {
      int v = ady[actual][i];
      if (dis[actual] + 1 < dis[v]) {
        dis[v] = dis[actual] + 1;
        q.push(v);
      }
    }
  }
  return 0;
}
int main() {
  cin >> n >> m;
  for (int i = 0, x, y; i < m; i++) {
    cin >> x >> y;
    ady[x].push_back(y);
    ady[y].push_back(x);
    used[x][y] = used[y][x] = 1;
  }
  int train = bfs(1);
  reset();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (!used[i][j]) {
        used[i][j] = 1;
        ady[i].push_back(j);
      }
    }
  }
  int car = bfs(1);
  if (car && train)
    cout << train + car - 1;
  else
    cout << -1;
}
