#include <bits/stdc++.h>
using namespace std;
const int N = 410;
int mat[N][N];
bool inq[N][N][2];
int dis[N][N][2];
struct Re {
  int x, y, flag;
  Re() {}
  Re(int x, int y, int flag) : x(x), y(y), flag(flag) {}
};
int solve(int n) {
  queue<Re> que;
  que.push(Re(1, 1, 0));
  inq[1][1][0] = true;
  memset(dis, 63, sizeof(dis));
  int oo = dis[1][1][0];
  dis[1][1][0] = 0;
  while (!que.empty()) {
    Re now = que.front();
    que.pop();
    inq[now.x][now.y][now.flag] = 0;
    if (0 == now.flag) {
      if (now.x == n) {
        if (dis[n][now.y][1] > dis[n][now.y][0] + 1) {
          dis[n][now.y][1] = dis[n][now.y][0] + 1;
          if (inq[n][now.y][1]) continue;
          inq[n][now.y][1] = true;
          que.push(Re(n, now.y, 1));
        }
      }
      for (int i = 1; i <= n; i++) {
        if ((i != n && now.y == i) || i == now.x || mat[now.x][i] != 1)
          continue;
        if (dis[i][now.y][1] > dis[now.x][now.y][now.flag] + 1) {
          dis[i][now.y][1] = dis[now.x][now.y][now.flag] + 1;
          if (!inq[i][now.y][1]) {
            inq[i][now.y][1] = true;
            que.push(Re(i, now.y, 1));
          }
        }
      }
    } else {
      if (now.y == n) {
        if (dis[now.x][n][0] > dis[now.x][now.y][now.flag]) {
          dis[now.x][n][0] = dis[now.x][now.y][now.flag];
          if (inq[now.x][n][0]) continue;
          inq[now.x][n][0] = true;
          que.push(Re(now.x, n, 0));
        }
      }
      for (int i = 1; i <= n; i++) {
        if ((i != n && now.x == i) || i == now.y || mat[now.y][i] != 2)
          continue;
        if (dis[now.x][i][0] > dis[now.x][now.y][now.flag]) {
          dis[now.x][i][0] = dis[now.x][now.y][now.flag];
          if (inq[now.x][i][0]) continue;
          inq[now.x][i][0] = true;
          que.push(Re(now.x, i, 0));
        }
      }
    }
  }
  if (dis[n][n][0] == oo) dis[n][n][0] = -1;
  return dis[n][n][0];
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      mat[i][j] = 1;
    }
  }
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    mat[x][y] = mat[y][x] = 2;
  }
  printf("%d\n", solve(n));
  return 0;
}
