#include <bits/stdc++.h>
using namespace std;
const int maxn = 4004;
const int inf = 0x3f3f3f3f;
int g[maxn][maxn];
int r[maxn][maxn];
int dis[maxn];
int vis[maxn];
int cnt = 0;
int n, m;
void dij() {
  memset(vis, 0, sizeof(vis));
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      q;
  for (int i = 1; i <= n; i++) dis[i] = inf;
  dis[1] = 0;
  q.push(pair<int, int>{0, 1});
  while (!q.empty()) {
    pair<int, int> f = q.top();
    q.pop();
    int u = f.second;
    if (vis[u]) continue;
    vis[u] = 1;
    for (int i = 1; i <= n; i++) {
      if (g[u][i]) {
        if (dis[u] + g[u][i] < dis[i]) {
          dis[i] = dis[u] + g[u][i];
          q.push({dis[i], i});
        }
      }
    }
  }
}
int main() {
  cin >> n >> m;
  int u, v;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (i == j) g[i][j] = 0;
  for (int i = 1; i <= m; i++) {
    cin >> u >> v;
    g[u][v] = 1;
    g[v][u] = 1;
  }
  dij();
  int ans1 = dis[n];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i != j) {
        if (g[i][j] == 1)
          g[i][j] = 0;
        else if (g[i][j] == 0)
          g[i][j] = 1;
      }
    }
  }
  dij();
  int ans2 = dis[n];
  if (ans1 == inf || ans2 == inf)
    cout << -1 << endl;
  else
    cout << max(ans1, ans2) << endl;
  return 0;
}
