#include <bits/stdc++.h>
using namespace std;
const long double PI =
    3.14159265358979323846264338327950288419716939937510582097494459230;
const long long MOD = 1000000007LL;
const long long INF = 1e9;
const long long LINF = 1e18;
const long double EPS = 1e-9;
const long double GOLD = ((1 + sqrt(5)) / 2);
template <class T>
int getbit(T s, int i) {
  return (s >> 1) & 1;
}
template <class T>
T onbit(T s, int i) {
  return s | (T(1) << i);
}
template <class T>
T offbit(T s, int i) {
  return s & (~(T(1) << i));
}
template <class T>
int cntbit(T s) {
  return __builtin_popcount(s);
}
long long n, m;
vector<vector<long long>> rail;
vector<long long> visited, dp;
void VarInput() {
  cin >> n >> m;
  rail.resize(n, vector<long long>(n, 0));
  visited.resize(n, false);
  dp.resize(n, 0LL);
  while (m--) {
    long long a, b;
    cin >> a >> b;
    rail[a - 1][b - 1] = 1;
    rail[b - 1][a - 1] = 1;
  }
}
void ProSolve() {
  visited[0] = true;
  queue<long long> Q;
  Q.push(0);
  long long token = 1 - rail[0][n - 1];
  while (!Q.empty()) {
    long long z = Q.front();
    Q.pop();
    for (long long i = 0; i < n; i++) {
      if (!visited[i] && rail[z][i] == token) {
        visited[i] = true;
        Q.push(i);
        dp[i] = dp[z] + 1;
      }
    }
  }
  if (dp[n - 1] == 0)
    cout << "-1";
  else
    cout << dp[n - 1];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  VarInput();
  ProSolve();
  return 0;
}
