#include <bits/stdc++.h>
using namespace std;
int lnk[401][401], f[401][401];
int n;
int ans[401];
struct inf {
  int poi, dist;
  inf(int a = 0, int b = 0) {
    poi = a;
    dist = b;
  }
};
bool operator<(const inf& t1, const inf& t2) { return t1.dist > t2.dist; }
priority_queue<inf> heap;
void solve() {
  for (int i = 1; i <= n; i++) ans[i] = -1;
  heap.push(inf(1, 0));
  while (!heap.empty()) {
    inf tmp = heap.top();
    heap.pop();
    if (ans[tmp.poi] != -1) continue;
    ans[tmp.poi] = tmp.dist;
    for (int i = 1; i <= n; i++)
      if (lnk[tmp.poi][i]) heap.push(inf(i, tmp.dist + 1));
  }
}
int main() {
  int m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    f[a][b] = f[b][a] = 1;
  }
  if (f[1][n]) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) lnk[i][j] = !f[i][j];
  } else {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) lnk[i][j] = f[i][j];
  }
  solve();
  printf("%d\n", ans[n]);
}
