#include <bits/stdc++.h>
using namespace std;
long long int n, m, x, y, flag = 0, found = 0;
queue<long long int> q;
long long int a[401][401] = {0};
long long int vis[401] = {0}, dis[401] = {0};
long long int bfs() {
  dis[1] = 0;
  q.push(1);
  while (!q.empty()) {
    long long int v = q.front();
    vis[v] = 1;
    q.pop();
    for (long long int i = 1; i <= n; i++) {
      if (vis[i] == 0 && a[v][i] == flag) {
        dis[i] = dis[v] + 1;
        vis[i] = 1;
        q.push(i);
        if (i == n) {
          found = 1;
          return dis[n];
        }
      }
    }
  }
  if (found == 0) return -1;
}
int main() {
  cin >> n >> m;
  flag = 1;
  for (long long int i = 0; i < m; i++) {
    cin >> x >> y;
    if ((x == 1 && y == n) || (x == n && y == 1)) flag = 0;
    a[x][y] = 1;
    a[y][x] = 1;
  }
  cout << bfs() << endl;
  return 0;
}
