#include <bits/stdc++.h>
using namespace std;
const int sz = 100005;
vector<int> v[sz];
vector<int> vx[sz];
long long dis[sz];
bool vis[sz];
set<int> s;
set<int>::iterator it;
pair<int, int> pi;
void bfs(int root) {
  int v1 = root;
  dis[v1] = 0;
  vis[v1] = 1;
  queue<int> q;
  q.push(v1);
  while (!q.empty()) {
    int x1 = q.front();
    q.pop();
    for (int i = 0; i < v[x1].size(); i++) {
      if (!vis[v[x1][i]]) {
        dis[v[x1][i]] = dis[x1] + 1;
        vis[v[x1][i]] = 1;
        q.push(v[x1][i]);
      }
    }
  }
}
void bfs1(int root) {
  int v1 = root;
  dis[v1] = 0;
  vis[v1] = 1;
  queue<int> q;
  q.push(v1);
  while (!q.empty()) {
    int x1 = q.front();
    q.pop();
    for (int i = 0; i < vx[x1].size(); i++) {
      if (!vis[vx[x1][i]]) {
        dis[vx[x1][i]] = dis[x1] + 1;
        vis[vx[x1][i]] = 1;
        q.push(vx[x1][i]);
      }
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  if (m == n * (n - 1) / 2 || m == 0)
    cout << "-1";
  else {
    bfs(1);
    int x = dis[n];
    for (int i = 0; i <= n; i++) vis[i] = 0, dis[i] = 0;
    if (x > 1)
      cout << x;
    else if (x == 0)
      cout << "-1";
    else {
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
          if (!(find(v[i].begin(), v[i].end(), j) != v[i].end()) && i != j) {
            vx[i].push_back(j);
            vx[j].push_back(i);
          }
        }
      }
      bfs1(1);
      if (dis[n] == 0)
        cout << "-1";
      else
        cout << dis[n];
    }
  }
}
