#include <bits/stdc++.h>
using namespace std;
int n, m, ans;
const int maxn = 1e5 + 9;
const int Max = 400 + 9;
int dis[maxn], v[Max][Max];
bool mark[maxn];
deque<int> de;
void input() {
  cin >> n >> m;
  int x, y;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    v[x][y]++;
    v[y][x]++;
  }
}
void bfs() {
  int a = de.front();
  for (int i = 1; i <= n; i++)
    if (mark[i] == false && v[a][i] > 0) {
      if (i == n) {
        ans = dis[a] + 1;
        dis[i] = dis[a] + 1;
        mark[n] = true;
        return;
      }
      dis[i] = dis[a] + 1;
      mark[i] = true;
      de.push_back(i);
    }
  de.pop_front();
}
void bbfs() {
  int a = de.front();
  for (int i = 1; i <= n; i++)
    if (mark[i] == false && v[a][i] == 0) {
      if (i == n) {
        ans = max(ans, dis[a] + 1);
        dis[i] = dis[a] + 1;
        mark[n] = true;
        return;
      }
      dis[i] = dis[a] + 1;
      mark[i] = true;
      de.push_back(i);
    }
  de.pop_front();
}
int main() {
  int q = 0;
  input();
  mark[1] = true;
  de.push_back(1);
  while (de.size() > 0) bfs();
  if (dis[n] == 0) q++;
  for (int i = 0; i < maxn; i++) dis[i] = 0;
  for (int i = 2; i < maxn; i++) mark[i] = false;
  de.push_back(1);
  while (de.size() > 0) bbfs();
  if (dis[n] == 0) q++;
  if (q > 0)
    cout << -1;
  else
    cout << ans << endl;
}
