#include <bits/stdc++.h>
using namespace std;
const long long N = 405, M = 40005;
int n, m;
int dist[N][N];
int dist1[N][N];
int main() {
  memset(dist, -1, sizeof dist);
  memset(dist1, -1, sizeof dist1);
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    dist[a][b] = 1;
    dist[b][a] = 1;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (dist[i][j] == -1) dist1[i][j] = 1;
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (dist[i][k] != -1 && dist[k][j] != -1 &&
            (dist[i][j] == -1 || dist[i][j] > dist[i][k] + dist[k][j]))
          dist[i][j] = dist[i][k] + dist[k][j];
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (dist1[i][k] != -1 && dist1[k][j] != -1 &&
            (dist1[i][j] == -1 || dist1[i][j] > dist1[i][k] + dist1[k][j]))
          dist1[i][j] = dist1[i][k] + dist1[k][j];
  if (dist[1][n] == -1 || dist1[1][n] == -1)
    printf("-1");
  else
    printf("%d", max(dist[1][n], dist1[1][n]));
}
