#include <bits/stdc++.h>
using namespace std;
int graph[405][405];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    graph[u][v] = graph[v][u] = 1;
  }
  if (graph[1][n]) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (graph[i][j] == 1) {
          graph[i][j] = 0;
        } else if (graph[i][j] == 0) {
          graph[i][j] = 1;
        }
  }
  queue<int> q;
  q.push(1);
  int dist[405];
  for (int i = 1; i <= n; i++) dist[i] = -1;
  dist[1] = 0;
  while (!q.empty()) {
    int s = q.front();
    q.pop();
    for (int i = 1; i <= n; i++) {
      if (i != s && graph[s][i] && dist[i] == -1) {
        dist[i] = dist[s] + 1;
        q.push(i);
      }
    }
  }
  cout << dist[n] << endl;
}
