#include <bits/stdc++.h>
const long long MOD = 1000000007;
using namespace std;
int mp1[(400 + 10)][(400 + 10)], mp2[(400 + 10)][(400 + 10)];
int dis[(400 + 10)], vis[(400 + 10)];
int n;
void dijkstra(int mp[][(400 + 10)]) {
  int i, j;
  for (i = 1; i <= n; i++) {
    dis[i] = 0x3f3f3f3f;
    vis[i] = 0;
  }
  dis[1] = 0;
  for (i = 1; i <= n; i++) {
    int tmp = 0x3f3f3f3f;
    int k;
    for (j = 1; j <= n; j++)
      if (!vis[j] && dis[j] < tmp) tmp = dis[k = j];
    vis[k] = 1;
    for (j = 1; j <= n; j++)
      if (!vis[j] && dis[j] > dis[k] + mp[k][j]) dis[j] = dis[k] + mp[k][j];
  }
}
int main() {
  int m;
  scanf("%d", &n), scanf("%d", &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) mp1[i][j] = mp2[i][j] = 0x3f3f3f3f;
  int u, v;
  while (m--) {
    scanf("%d", &u), scanf("%d", &v);
    mp1[u][v] = mp1[v][u] = 1;
  }
  if (mp1[1][n] == 1) {
    for (int i = 1; i <= n; i++)
      for (int j = i + 1; j <= n; j++)
        if (mp1[i][j] != 1) mp2[i][j] = mp2[j][i] = 1;
    dijkstra(mp2);
    if (dis[n] == 0x3f3f3f3f)
      printf("%d\n", (-1));
    else
      printf("%d\n", (dis[n]));
  } else {
    dijkstra(mp1);
    if (dis[n] == 0x3f3f3f3f)
      printf("%d\n", (-1));
    else
      printf("%d\n", (dis[n]));
  }
  return 0;
}
