#include <bits/stdc++.h>
using namespace std;
vector<int> edge[410];
int vis[410];
queue<int> q;
vector<int> path, dis;
void bfs(int s) {
  q.push(s);
  path[s] = -1;
  vis[s] = 1;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (auto y : edge[x]) {
      if (!vis[y]) {
        vis[y] = 1;
        q.push(y);
        path[y] = x;
        dis[y] = dis[x] + 1;
      }
    }
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  set<pair<int, int> > s;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    edge[x].push_back(y);
    edge[y].push_back(x);
    if (x > y) swap(x, y);
    s.insert({x, y});
  }
  path.resize(n + 1), dis.resize(n + 1);
  memset(vis, 0, sizeof(vis));
  bfs(1);
  int ans = dis[n];
  if (!vis[n]) {
    cout << "-1"
         << "\n";
    return;
  }
  path.clear(), dis.clear();
  for (int i = 1; i <= n; i++) edge[i].clear();
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (s.find({i, j}) != s.end()) continue;
      edge[i].push_back(j);
      edge[j].push_back(i);
    }
  }
  bfs(1);
  if (!vis[n]) {
    cout << "-1"
         << "\n";
    return;
  }
  ans = max(dis[n], ans);
  cout << ans << "\n";
}
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
}
