#include <bits/stdc++.h>
using namespace std;
vector<vector<long long int>> v;
vector<vector<long long int>> v1;
long long int vis[401][401];
long long int dist[1000000];
long long int dist1[1000000];
priority_queue<pair<long long int, long long int>,
               vector<pair<long long int, long long int>>,
               greater<pair<long long int, long long int>>>
    q;
priority_queue<pair<long long int, long long int>,
               vector<pair<long long int, long long int>>,
               greater<pair<long long int, long long int>>>
    q1;
void dij1(long long int x, long long int n) {
  for (int i = 1; i <= n; i++) dist1[i] = 1e9;
  dist1[x] = 0;
  q1.push({0, x});
  while (!q1.empty()) {
    pair<long long int, long long int> h = q1.top();
    long long int x = h.second;
    q1.pop();
    for (long long int i = 0; i < v1[x].size(); i++) {
      if (dist1[x] + 1 < dist1[v1[x][i]]) {
        dist1[v1[x][i]] = dist1[x] + 1;
        q1.push({dist1[v1[x][i]], v1[x][i]});
      }
    }
  }
}
void dij(long long int x, long long int n) {
  for (int i = 1; i <= n; i++) dist[i] = 1e9;
  dist[x] = 0;
  q.push({0, x});
  while (!q.empty()) {
    pair<long long int, long long int> h = q.top();
    long long int x = h.second;
    q.pop();
    for (long long int i = 0; i < v[x].size(); i++) {
      if (dist[x] + 1 < dist[v[x][i]]) {
        dist[v[x][i]] = dist[x] + 1;
        q.push({dist[v[x][i]], v[x][i]});
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int n, m, a, b;
  cin >> n >> m;
  v.resize(n + 1);
  v1.resize(n + 1);
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
    vis[a][b] = 1;
    vis[a][a] = 1;
    vis[b][b] = 1;
    vis[b][a] = 1;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (vis[i][j] == 0) {
        v1[i].push_back(j);
        v1[j].push_back(i);
      }
    }
  }
  dij(1, n);
  dij1(1, n);
  if (dist[n] != 1e9 && dist1[n] != 1e9) {
    cout << max(dist[n], dist1[n]);
  } else
    cout << "-1";
  return 0;
}
