#include <bits/stdc++.h>
using namespace std;
vector<long long int> v[401];
vector<long long int> vr[401];
bool mark[401][401];
bool vis[401];
long long int level[401];
long long int n;
long long int bfs(long long int s) {
  vis[s] = 1;
  queue<long long int> q;
  q.push(s);
  level[s] = 0;
  memset(level, 0, n);
  while (!q.empty()) {
    long long int temp = q.front();
    q.pop();
    for (int i = 0; i < v[temp].size(); ++i) {
      if (vis[v[temp][i]] == 0) {
        vis[v[temp][i]] = 1;
        level[v[temp][i]] = level[temp] + 1;
        q.push(v[temp][i]);
      }
    }
  }
  if (level[n] != 0)
    return level[n];
  else
    return 0;
}
long long int bfs1(long long int s) {
  vis[s] = 1;
  queue<long long int> q;
  q.push(s);
  level[s] = 0;
  memset(level, 0, n);
  while (!q.empty()) {
    long long int temp = q.front();
    q.pop();
    for (int i = 0; i < vr[temp].size(); ++i) {
      if (vis[vr[temp][i]] == 0) {
        vis[vr[temp][i]] = 1;
        level[vr[temp][i]] = level[temp] + 1;
        q.push(vr[temp][i]);
      }
    }
  }
  if (level[n] != 0)
    return level[n];
  else
    return 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int m;
  cin >> n >> m;
  memset(mark, 0, sizeof(mark));
  for (int i = 0; i < m; ++i) {
    long long int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
    mark[x][y] = 1;
    mark[y][x] = 1;
  }
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (mark[i][j] == 0 && i != j) {
        vr[i].push_back(j);
      }
    }
  }
  if (m == 0 || m == (n * (n - 1)) / 2) {
    cout << "-1";
    exit(0);
  }
  if (mark[1][n] == 1) {
    if (bfs1(1))
      cout << max(1LL, bfs1(1));
    else
      cout << "-1";
  } else {
    if (bfs(1))
      cout << max(1LL, bfs(1));
    else
      cout << "-1";
  }
}
