#include <bits/stdc++.h>
long long int maxx(long long int a, long long int b) { return a > b ? a : b; }
long long int minn(long long int a, long long int b) { return a > b ? b : a; }
using namespace std;
long long int gcd(long long int a, long long int b) {
  return b == 0 ? a : gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
  return (a * b) / gcd(a, b);
}
long long int poww(long long int base, long long int exp) {
  base %= 1000000007;
  long long int result = 1;
  while (exp > 0) {
    if (exp & 1) result = (result * base) % 1000000007;
    base = (base * base) % 1000000007;
    exp >>= 1;
  }
  return result;
}
vector<int> a[2][500];
bool rail[500][500];
int dist[2][500];
void bfs(int second, int t, int pos) {
  queue<int> q;
  q.push(second);
  dist[pos][second] = 0;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < a[pos][v].size(); i++)
      if (!dist[pos][a[pos][v][i]]) {
        q.push(a[pos][v][i]);
        dist[pos][a[pos][v][i]] = dist[pos][v] + 1;
      }
  }
  return;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    a[0][u].push_back(v);
    a[0][v].push_back(u);
    rail[u][v] = 1;
    rail[v][u] = 1;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (i != j && !rail[i][j] && !rail[j][i]) a[1][i].push_back(j);
  bfs(1, n, 0);
  bfs(1, n, 1);
  if (!dist[0][n] || !dist[1][n]) return cout << -1, 0;
  cout << max(dist[0][n], dist[1][n]);
}
