#include <bits/stdc++.h>
using namespace std;
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n >> b) & 1; }
inline void set_bit(int& n, int b) { n |= two(b); }
inline void unset_bit(int& n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
int dist[403][403];
void print(int a[][403], int n) {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) cout << a[i][j] << " ";
    cout << "\n";
  }
}
int floyd_warshall(int a[][403], int n) {
  for (int via = 1; via <= n; via++) {
    for (int from = 1; from <= n; from++) {
      for (int to = 1; to <= n; to++) {
        a[from][to] = min(a[from][to], a[from][via] + a[via][to]);
      }
    }
  }
  return a[1][n];
}
int dist_dij[403];
bool visited[403];
int vertex_min_path_len(int n) {
  int min_dist = INT_MAX, min_vertex = 0;
  for (int i = 1; i <= n; i++) {
    if (!visited[i] && dist_dij[i] <= min_dist)
      min_dist = dist_dij[i], min_vertex = i;
  }
  return min_vertex;
}
int dijkstra(int src, int n) {
  memset(visited, 0, sizeof visited);
  for (int i = 1; i <= n; i++) dist_dij[i] = 403;
  dist_dij[src] = 0;
  int cnt = 1;
  while (cnt <= n) {
    int u = vertex_min_path_len(n);
    visited[u] = true;
    for (int v = 1; v <= n; v++) {
      if (!visited[v] && dist[u][v] != 403 &&
          dist_dij[u] + dist[u][v] <= dist_dij[v])
        dist_dij[v] = dist_dij[u] + dist[u][v];
    }
    cnt++;
  }
  return dist_dij[n];
}
int main() {
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) dist[i][j] = 403;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    dist[u][v] = dist[v][u] = 1;
  }
  if (dist[1][n] == 1) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (dist[i][j] == 403)
          dist[i][j] = 1;
        else
          dist[i][j] = 403;
        if (i == j) dist[i][j] = 403;
      }
    }
  }
  int ans = -2;
  ans = dijkstra(1, n);
  if (ans == 403)
    cout << "-1\n";
  else
    cout << ans << "\n";
  return 0;
}
