#include <bits/stdc++.h>
long long int add(long long int a, long long int b) {
  return a + b - (a + b >= 1000000007) * 1000000007;
}
long long int sub(long long int a, long long int b) {
  return a - b + (a - b < 0) * 1000000007;
}
long long int mult(long long int a, long long int b) {
  return (1LL * a * b) % 1000000007;
}
inline long long int gcd(long long int a, long long int b) {
  long long int t;
  while (b) {
    a = a % b;
    t = a;
    a = b;
    b = t;
  }
  return a;
}
using namespace std;
const int MAXN = 4E2 + 10;
int E[MAXN][MAXN];
int dis[MAXN];
int main() {
  int n, m;
  cin >> n >> m;
  for (int u, v, i = 0; i < m; ++i) {
    cin >> u >> v;
    E[u][v] = E[v][u] = true;
  }
  int type = !E[1][n];
  queue<int> Q1;
  dis[1] = 1;
  Q1.push(1);
  while (!Q1.empty() && !dis[n]) {
    int u = Q1.front();
    Q1.pop();
    for (int i = 1; i <= n; ++i)
      if (E[u][i] == type && !dis[i]) {
        dis[i] = dis[u] + 1;
        Q1.push(i);
      }
  }
  cout << dis[n] - 1;
  return 0;
}
