#include <bits/stdc++.h>
using namespace std;
int n, m, t[401][401], c_rail, c_road, visit[401], level[401];
vector<int> rail[401];
vector<int> road[401];
queue<int> q;
int rail_func() {
  q.push(1);
  level[1] = 0;
  visit[1] = 1;
  while (!q.empty()) {
    int top = q.front();
    q.pop();
    for (int i = 0; i < rail[top].size(); i++) {
      if (rail[top][i] == n) {
        return level[top] + 1;
      }
      if (visit[rail[top][i]] == 0) {
        level[rail[top][i]] = level[top] + 1;
        q.push(rail[top][i]);
        visit[rail[top][i]] = 1;
      }
    }
  }
  return -1;
}
int road_func() {
  q.push(1);
  level[1] = 0;
  visit[1] = 1;
  while (!q.empty()) {
    int top = q.front();
    q.pop();
    for (int i = 0; i < road[top].size(); i++) {
      if (road[top][i] == n) {
        return level[top] + 1;
      }
      if (visit[road[top][i]] == 0) {
        level[road[top][i]] = level[top] + 1;
        q.push(road[top][i]);
        visit[road[top][i]] = 1;
      }
    }
  }
  return -1;
}
void clear() {
  for (int i = 0; i <= 401; i++) {
    level[i] = 0;
    visit[i] = 0;
  }
  while (!q.empty()) q.pop();
}
int main() {
  int a, b;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> a >> b;
    rail[a].push_back(b);
    rail[b].push_back(a);
    t[a][b] = 1;
    t[b][a] = 1;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (t[i][j] == 0) {
        road[i].push_back(j);
        road[j].push_back(i);
      }
    }
  }
  c_rail = rail_func();
  clear();
  c_road = road_func();
  if ((c_road == -1) || (c_rail == -1))
    cout << -1;
  else
    cout << max(c_road, c_rail);
  return 0;
}
