#include <bits/stdc++.h>
using namespace std;
class comp {
 public:
  bool operator()(pair<int, int> p1, pair<int, int> p2) {
    return p2.second < p1.second;
  }
};
priority_queue<pair<int, int>, vector<pair<int, int> >, comp> pq;
int dist[200005];
vector<int> adj[200005];
int graph[405][405];
int main() {
  int n, m, i, j, x, y;
  scanf("%d %d", &n, &m);
  for (i = 0; i < m; i++) {
    scanf("%d %d", &x, &y);
    graph[x][y] = 1;
    graph[y][x] = 1;
  }
  if (graph[1][n] == 1) {
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= n; j++) {
        graph[i][j] ^= 1;
      }
    }
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      if (i != j && graph[i][j] == 1) {
        adj[i].push_back(j);
      }
    }
  }
  for (i = 1; i <= n; i++) {
    dist[i] = 2000000200;
  }
  pq.push(make_pair(1, 0));
  dist[1] = 0;
  while (!pq.empty()) {
    pair<int, int> p1 = pq.top();
    pq.pop();
    if (p1.second > dist[p1.first]) {
      continue;
    }
    for (i = 0; i < adj[p1.first].size(); i++) {
      if (dist[adj[p1.first][i]] > (p1.second + 1)) {
        dist[adj[p1.first][i]] = p1.second + 1;
        pq.push(make_pair(adj[p1.first][i], p1.second + 1));
      }
    }
  }
  if (dist[n] == 2000000200) {
    printf("-1\n");
  } else {
    printf("%d\n", dist[n]);
  }
  return 0;
}
