#include <bits/stdc++.h>
using namespace std;
int m[404][404];
int n, a, b;
bool vis[404];
int bfs(int a, bool flag) {
  queue<pair<int, int> > q;
  q.push(pair<int, int>(0, a));
  memset(vis, 0, sizeof vis);
  while (!q.empty()) {
    int dist = q.front().first;
    int atual = q.front().second;
    q.pop();
    if (atual == n - 1) return dist;
    if (vis[atual]) continue;
    vis[atual] = true;
    for (int i = 0; i < n; i++) {
      if (i != atual and m[atual][i] == flag)
        q.push(pair<int, int>(dist + 1, i));
    }
  }
  return 0x3f3f3f3f;
}
int main(void) {
  int e;
  scanf("%d%d", &n, &e);
  memset(m, 0, sizeof m);
  for (int i = 0; i < e; i++) {
    scanf("%d%d", &a, &b);
    a--;
    b--;
    m[a][b] = true;
    m[b][a] = true;
  }
  int ans1 = bfs(0, true);
  int ans2 = bfs(0, false);
  if (ans1 == 0x3f3f3f3f || ans2 == 0x3f3f3f3f)
    puts("-1");
  else
    printf("%d\n", max(ans1, ans2));
  return 0;
}
