#include <bits/stdc++.h>
using namespace std;
const int mxn = 1e5 + 10;
int n, m;
bool jest[410][410];
bool vis_sam[mxn], vis_poc[mxn];
int sam[mxn], poc[mxn];
void wczytaj() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    jest[a][b] = true;
    jest[b][a] = true;
  }
}
void bfs_sam() {
  queue<int> q;
  q.push(1);
  vis_sam[1] = true;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 1; i <= n; i++) {
      if (i == v) continue;
      if (vis_sam[i]) continue;
      if (!jest[i][v]) continue;
      sam[i] = sam[v] + 1;
      q.push(i);
      vis_sam[i] = true;
    }
  }
}
void bfs_poc() {
  queue<int> q;
  q.push(1);
  vis_poc[1] = true;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 1; i <= n; i++) {
      if (i == v) continue;
      if (vis_poc[i]) continue;
      if (jest[i][v]) continue;
      poc[i] = poc[v] + 1;
      q.push(i);
      vis_poc[i] = true;
    }
  }
}
int main() {
  wczytaj();
  bfs_poc();
  bfs_sam();
  if (vis_poc[n] == false || vis_sam[n] == false) {
    printf("-1\n");
    return 0;
  }
  printf("%d\n", max(poc[n], sam[n]));
}
