#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int N = 500;
set<char> vowel = {'A', 'O', 'Y', 'E', 'U', 'I', 'a', 'o', 'y', 'e', 'u', 'i'};
long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int n, m, k, l, ans = INT_MAX, res = INT_MAX, posmin, posmax, x, y, z, xmax = 0,
                ymax = 0;
int pred[N];
int dist[N];
bool vis[N];
char c, c1;
string ch, ch1, ch2;
deque<int> dq, dq1;
queue<int> q;
map<long long, long long> maa;
map<int, set<int> > adj;
map<pair<int, int>, vector<pair<int, int> > > mapai;
map<char, set<char> > edje;
set<char> setchar;
set<long long> sx, sy;
vector<int> v, vv;
vector<pair<int, int> > vvv;
bool check = false, check2 = false;
int main() {
  ios_base::sync_with_stdio(false);
  memset(vis, 0, sizeof(vis));
  memset(pred, -1, sizeof(pred));
  memset(dist, 1e6, sizeof(dist));
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    x--;
    y--;
    adj[x].insert(y);
    adj[y].insert(x);
  }
  q.push(0);
  dist[0] = 0;
  while (!q.empty()) {
    x = q.front();
    q.pop();
    if (vis[x]) continue;
    vis[x] = 1;
    for (auto t : adj[x]) {
      pred[t] = x;
      dist[t] = min(dist[x] + 1, dist[t]);
      if (t == n - 1) {
        ans = min(ans, dist[t]);
        check = true;
      }
      if (vis[t] == 0) q.push(t);
    }
  }
  memset(vis, 0, sizeof(vis));
  memset(pred, -1, sizeof(pred));
  memset(dist, 1e6, sizeof(dist));
  q.push(0);
  dist[0] = 0;
  while (!q.empty()) {
    x = q.front();
    q.pop();
    if (vis[x]) continue;
    vis[x] = 1;
    for (int t = 0; t < n; t++) {
      if (adj[x].count(t) == 0 && t != x) {
        pred[t] = x;
        dist[t] = min(dist[x] + 1, dist[t]);
        if (t == n - 1) {
          res = min(res, dist[t]);
          check2 = true;
        }
        if (vis[t] == 0) q.push(t);
      }
    }
  }
  if (check == false || check2 == false)
    cout << -1;
  else
    cout << max(res, ans);
  return 0;
}
