#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF_L = ll(1e18);
const int INF = 1e9;
const int mod = 1e9 + 7;
int main() {
  int n, m;
  cin >> n >> m;
  int adj[n + 1][n + 1];
  int dis1[n + 1];
  int dis2[n + 1];
  int dis[n + 1];
  int v[n + 1];
  memset(adj, 0, sizeof(adj));
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a][b] = 1;
    adj[b][a] = 1;
  }
  for (int i = 1; i <= n; i++) {
    dis1[i] = INF;
    dis2[i] = INF;
    dis[i] = -1;
    v[i] = 0;
  }
  if (!adj[1][n]) {
    queue<int> s;
    s.push(1);
    dis[1] = 0;
    v[1] = 1;
    while (!s.empty()) {
      int cur = s.front();
      s.pop();
      for (int i = 1; i <= n; i++) {
        if (adj[cur][i]) {
          if (!v[i]) {
            dis[i] = dis[cur] + 1;
            v[i] = 1;
            s.push(i);
          }
        }
      }
    }
  } else {
    queue<int> s;
    s.push(1);
    v[1] = 1;
    dis[1] = 0;
    while (!s.empty()) {
      int cur = s.front();
      s.pop();
      for (int i = 1; i <= n; i++) {
        if (!adj[cur][i]) {
          if (!v[i]) {
            dis[i] = dis[cur] + 1;
            v[i] = 1;
            s.push(i);
          }
        }
      }
    }
  }
  cout << dis[n] << endl;
  return 0;
}
