#include <bits/stdc++.h>
using namespace std;
int G[401][401], vis[401], d[401], n, m;
queue<int> q;
int BFS(int N) {
  q.push(1);
  vis[1] = 1;
  while (!q.empty() && !vis[n]) {
    int t = q.front();
    q.pop();
    for (int i = 1; i <= n; i++) {
      if (G[t][i] == N && vis[i] == 0) {
        d[i] = d[t] + 1;
        vis[i] = 1;
        q.push(i);
      }
    }
  }
  return d[n];
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    G[a][b] = 1;
    G[b][a] = 1;
  }
  printf("%d", BFS(!G[1][n]) == 0 ? -1 : d[n]);
  return 0;
}
