#include <bits/stdc++.h>
using namespace std;
const int maxn = 400 + 228;
bool vis1[maxn], vis2[maxn];
int dis1[maxn], dis2[maxn], pr1[maxn], pr2[maxn];
vector<int> g1[maxn], g2[maxn];
queue<int> q1, q2;
void bfs1(int v) {
  q1.push(v);
  dis1[v] = 0;
  vis1[v] = 1;
  while (!q1.empty()) {
    int x = q1.front();
    q1.pop();
    for (int i = 0; i < g1[x].size(); i++) {
      int to = g1[x][i];
      if (vis1[to] == 0) {
        q1.push(to);
        vis1[to] = 1;
        dis1[to] = dis1[x] + 1;
        pr1[to] = x;
      }
    }
  }
}
void bfs2(int v) {
  q2.push(v);
  dis2[v] = 0;
  vis2[v] = 1;
  while (!q2.empty()) {
    int x = q2.front();
    q2.pop();
    for (int i = 0; i < g2[x].size(); i++) {
      int to = g2[x][i];
      if (vis2[to] == 0) {
        q2.push(to);
        vis2[to] = 1;
        dis2[to] = dis2[x] + 1;
        pr2[to] = x;
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int n, m;
  cin >> n >> m;
  map<pair<int, int>, int> q;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    if (x > y) {
      swap(x, y);
    }
    x--;
    y--;
    q[{x, y}]++;
    g1[x].push_back(y);
    g1[y].push_back(x);
  }
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      if (q[{i, j}] == 0 && i < j && i != j) {
        g2[i].push_back(j);
        g2[j].push_back(i);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    dis1[i] = 1e9;
    dis2[i] = 1e9;
  }
  bfs1(0);
  bfs2(0);
  if (dis1[n - 1] == 1e9 || dis2[n - 1] == 1e9) {
    cout << -1;
    return 0;
  }
  vector<long long> ans1;
  long long x = n - 1;
  while (x != 0) {
    x = pr1[x];
    ans1.push_back(x);
  }
  ans1.pop_back();
  reverse(ans1.begin(), ans1.end());
  vector<long long> ans2;
  long long y = n - 1;
  while (y != 0) {
    y = pr2[y];
    ans2.push_back(y);
  }
  ans2.pop_back();
  reverse(ans2.begin(), ans2.end());
  if (ans1.size() == 0 || ans2.size() == 0) {
    cout << max(ans1.size(), ans2.size()) + 1;
    return 0;
  }
  map<pair<int, int>, int> w;
  for (int i = 0; i < ans1.size(); i++) {
    w[{dis1[ans1[i]], ans1[i]}]++;
  }
  bool e = 0;
  for (int i = 0; i < ans2.size(); i++) {
    if (w[{dis2[ans2[i]], ans2[i]}] > 0) {
      e = 1;
    }
  }
  cout << max(ans1.size(), ans2.size()) + 1 + e;
  return 0;
}
