#include <bits/stdc++.h>
using namespace std;
const int maxn = 405;
typedef struct node {
  int dist, pos;
  friend bool operator<(const node &a, const node &b) {
    return a.dist > b.dist;
  }
} node;
int n, m;
int G1[maxn][maxn], G2[maxn][maxn];
int dist1[maxn], dist2[maxn];
int trans[maxn], vis[maxn], vis1[maxn], vis2[maxn];
priority_queue<node> q;
int main() {
  int u, v;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d %d", &u, &v);
    G1[u][v] = G1[v][u] = 1;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (i != j && !G1[i][j]) G2[i][j] = G2[j][i] = 1;
  for (int i = 1; i <= n; i++) dist1[i] = dist2[i] = 9999999;
  q.push(node{0, 1});
  dist1[1] = 0;
  while (!q.empty()) {
    int tmp = q.top().pos;
    q.pop();
    if (vis1[tmp]) continue;
    vis1[tmp] = true;
    for (int i = 1; i <= n; i++)
      if (G1[tmp][i]) {
        if (dist1[i] > dist1[tmp] + 1) {
          dist1[i] = dist1[tmp] + 1;
          trans[i] = tmp;
          q.push(node{dist1[i], i});
        }
      }
  }
  if (dist1[n] == 9999999) {
    puts("-1");
    return 0;
  }
  for (int i = n; i != 1; i = trans[i]) vis[i] = 1;
  q.push(node{0, 1});
  dist2[1] = 0;
  while (!q.empty()) {
    int tmp = q.top().pos;
    q.pop();
    if (vis2[tmp]) continue;
    vis2[tmp] = true;
    for (int i = 1; i <= n; i++)
      if (G2[tmp][i]) {
        if (dist2[i] > dist2[tmp] + 1 &&
            (i == n || !vis[i] ||
             (vis[i] && dist2[tmp] + 1 != dist1[tmp] + 1))) {
          dist2[i] = dist2[tmp] + 1;
          q.push(node{dist2[i], i});
        }
      }
  }
  int ans = max(dist1[n], dist2[n]);
  if (ans == 9999999) ans = -1;
  printf("%d", ans);
  return 0;
}
