#include <bits/stdc++.h>
using namespace std;
string vow = "aeiou";
const int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2};
const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1};
const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};
const long double pie = 3.14159265358979;
const long long mod = 1e9 + 7;
const int N = 4e2 + 2;
int g1[N][N], g2[N][N];
int n, m;
void read() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g1[a][b] = 1;
    g1[b][a] = 1;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) g2[i][j] = g1[i][j] ^ 1;
  }
}
bool vis[N];
int bfs1() {
  queue<pair<int, int> > q;
  q.push({0, 0});
  while (q.empty() == false) {
    auto temp = q.front();
    q.pop();
    int node = temp.second;
    if (vis[node]) continue;
    ;
    vis[node] = 1;
    if (node == n - 1) return temp.first;
    for (int i = 0; i < n; i++) {
      if (g1[node][i] == 1 && vis[i] == false) {
        q.push({temp.first + 1, i});
      }
    }
  }
  if (vis[n - 1] == false) {
    cout << -1;
    exit(0);
  }
}
int bfs2() {
  for (int i = 0; i < N; i++) vis[i] = false;
  queue<pair<int, int> > q;
  q.push({0, 0});
  while (q.empty() == false) {
    auto temp = q.front();
    q.pop();
    int node = temp.second;
    if (vis[node]) continue;
    ;
    vis[node] = 1;
    if (node == n - 1) return temp.first;
    for (int i = 0; i < n; i++) {
      if (g2[node][i] == 1 && vis[i] == false) {
        q.push({temp.first + 1, i});
      }
    }
  }
  if (vis[n - 1] == false) {
    cout << -1;
    exit(0);
  }
}
void solve(int test_case) {
  read();
  int d1 = bfs1();
  int d2 = bfs2();
  cout << max(d1, d2);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  for (int i = 0; i < t; i++) solve(i);
  return 0;
}
