#include <bits/stdc++.h>
using namespace std;
int nod, edge;
int a[401][401];
int dist[401];
int vis[401];
int bfs(int node) {
  queue<int> q;
  q.push(node);
  vis[node] = 1;
  while (q.size()) {
    int cur = q.front();
    q.pop();
    if (cur == nod) return dist[cur];
    for (int i = nod; i >= 1; i--) {
      if (!vis[i] and (a[cur][i] + a[i][cur])) {
        dist[i] += dist[cur] + 1, q.push(i);
        vis[i] = 1;
      }
    }
  }
  return 0;
}
int bfs1(int node) {
  memset(dist, 0, sizeof dist);
  memset(vis, 0, sizeof vis);
  queue<int> q;
  q.push(node);
  vis[node] = 1;
  while (q.size()) {
    int cur = q.front();
    q.pop();
    if (cur == nod) return dist[cur];
    for (int i = nod; i >= 1; i--) {
      if (!vis[i] and !(a[cur][i] + a[i][cur])) {
        dist[i] += dist[cur] + 1, q.push(i);
        vis[i] = 1;
      }
    }
  }
  return 0;
}
int main() {
  cin >> nod >> edge;
  int x, y;
  while (edge--) {
    cin >> x >> y;
    a[x][y] = 1;
    a[y][x] = 1;
  }
  int ans = bfs(1);
  int ans2 = bfs1(1);
  if (ans != 0 and ans2 != 0)
    cout << max(ans, ans2);
  else
    cout << -1;
}
