#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
int n, m;
int x, y;
int m1[maxn][maxn], m2[maxn][maxn];
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      m1[i][j] = m1[j][i] = 0x3f3f3f3f;
      m2[i][j] = m2[j][i] = 1;
    }
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    m1[x][y] = m1[y][x] = 1;
    m2[x][y] = m2[y][x] = 0x3f3f3f3f;
  }
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        m1[i][j] = min(m1[i][j], m1[i][k] + m1[k][j]);
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        m2[i][j] = min(m2[i][j], m2[i][k] + m2[k][j]);
  if (m1[1][n] >= 0x3f3f3f3f || m2[1][n] >= 0x3f3f3f3f)
    cout << -1 << endl;
  else {
    int ans = max(m1[1][n], m2[1][n]);
    cout << ans << endl;
  }
  return 0;
}
