#include <bits/stdc++.h>
using namespace std;
long long g[405][405];
long long level[405];
bool vis[405];
int main() {
  long long n, m;
  cin >> n >> m;
  level[n] = -1;
  for (long long i = 0; i < m; i++) {
    long long x, y;
    cin >> x >> y;
    g[x][y] = 1;
    g[y][x] = 1;
  }
  queue<long long> q;
  q.push(1);
  level[1] = 0;
  vis[1] = true;
  while (!q.empty()) {
    long long x = q.front();
    q.pop();
    for (long long i = 1; i <= n; i++) {
      if ((!g[1][n] && g[x][i]) || (g[1][n] && !g[x][i])) {
        if (!vis[i]) {
          q.push(i);
          level[i] = level[x] + 1;
          vis[i] = true;
        }
      }
    }
  }
  cout << level[n] << "\n";
  return 0;
}
