#include <bits/stdc++.h>
using namespace std;
int traverse(vector<vector<bool>>& v, bool flag) {
  int n = v.size() - 1;
  queue<pair<int, int>> st;
  vector<int> vis(n + 1, 0);
  st.push({1, 0});
  vis[1] = 1;
  while (!st.empty()) {
    int k = st.front().first;
    int dis = st.front().second;
    if (k == n) return dis;
    st.pop();
    for (int j = 1; j <= n; j++)
      if (v[k][j] == flag) {
        if (vis[j] == 0) {
          st.push({j, dis + 1});
          vis[j] = 1;
        }
      }
  }
  return -1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  t = 1;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    vector<vector<bool>> v(n + 1, vector<bool>(n + 1, 0));
    bool flag = 0;
    long long x, y;
    for (int i = 0; i < m; i++) {
      cin >> x >> y;
      v[x][y] = 1;
      v[y][x] = 1;
      if ((x == 1 && y == n) || (x == n && y == 1)) flag = 1;
    }
    int ans = traverse(v, 1 - flag);
    cout << ans;
  }
}
