#include <bits/stdc++.h>
const int maxn = 505;
const int inf = 2 * (1e9);
using namespace std;
int dp[maxn][maxn];
void floyd(int n) {
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (dp[i][k] < inf && dp[k][j] < inf && dp[i][j] > dp[i][k] + dp[k][j])
          dp[i][j] = dp[i][k] + dp[k][j];
}
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m) == 2) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) i == j ? dp[i][j] = 0 : dp[i][j] = inf;
    int u, v;
    for (int i = 0; i < m; i++) {
      scanf("%d%d", &u, &v);
      dp[u][v] = dp[v][u] = 1;
    }
    if (dp[1][n] == 1) {
      for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++) {
          if (i == j)
            continue;
          else if (dp[i][j] == 1)
            dp[i][j] = inf;
          else
            dp[i][j] = 1;
        }
    }
    floyd(n);
    if (dp[1][n] == inf)
      printf("-1\n");
    else
      printf("%d\n", dp[1][n]);
  }
}
