#include <bits/stdc++.h>
using namespace std;
vector<int> v[500];
int a[500][500], vs[500];
queue<int> q;
int f(int aa, int n) {
  int i, k;
  q.push(aa);
  vs[aa] = 1;
  while (q.size() != 0) {
    aa = q.front();
    q.pop();
    for (i = 0; i < v[aa].size(); i++) {
      if (vs[v[aa][i]] == 0) {
        vs[v[aa][i]] = vs[aa] + 1;
        q.push(v[aa][i]);
      }
    }
  }
  return vs[n] - 1;
}
int main() {
  int i, j, j1 = 1, k, l, m, n;
  cin >> n >> m;
  for (i = 1; i <= m; i++) {
    cin >> k >> l;
    a[k][l] = 1;
    a[l][k] = 1;
    if ((k == 1 && l == n) || (k == n && l == 1)) j1 = 0;
  }
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      if (i != j && (a[i][j] == j1)) v[i].push_back(j);
  cout << f(1, n) << endl;
  return 0;
}
