#include <bits/stdc++.h>
using namespace std;
bool mark[444][444], flag[444];
int dis[444], n, m, ans;
void bfs(int root, bool edge) {
  flag[root] = true;
  dis[root] = 0;
  queue<int> q;
  q.push(root);
  while (!q.empty()) {
    int top = q.front();
    q.pop();
    for (int i = 1; i <= n; i++)
      if (mark[top][i] == edge && !flag[i]) {
        flag[i] = true;
        dis[i] = dis[top] + 1;
        q.push(i);
      }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    mark[x][y] = mark[y][x] = true;
  }
  memset(dis, 127, sizeof dis);
  bfs(1, true);
  ans = max(ans, dis[n]);
  memset(dis, 127, sizeof dis);
  memset(flag, false, sizeof flag);
  bfs(1, false);
  ans = max(ans, dis[n]);
  if (ans > 1e7)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
