#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 450;
int mp[maxn][maxn], rmp[maxn][maxn];
int vis[maxn], d[maxn], n, m;
int dijkstra(int a[maxn][maxn]) {
  memset(vis, 0, sizeof(vis));
  memset(d, 0x3f, sizeof(d));
  d[1] = 0;
  for (int i = 1; i < n; i++) {
    int k, minn = inf;
    for (int j = 1; j <= n; j++) {
      if (!vis[j] && d[j] < minn) {
        k = j;
        minn = d[j];
      }
    }
    vis[k] = 1;
    for (int y = 1; y <= n; y++) d[y] = min(d[y], d[k] + a[k][y]);
  }
  return d[n];
}
int main() {
  cin >> n >> m;
  memset(mp, 0x3f, sizeof(mp));
  memset(rmp, 0x3f, sizeof(rmp));
  for (int i = 1; i <= n; i++) {
    mp[i][i] = 0;
    rmp[i][i] = 0;
  }
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    mp[x][y] = mp[y][x] = 1;
  }
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++)
      if (mp[i][j] == inf) rmp[i][j] = rmp[j][i] = 1;
  int ans1 = 0, ans2 = 0;
  ans1 = dijkstra(mp);
  ans2 = dijkstra(rmp);
  if (ans1 >= inf || ans2 >= inf)
    cout << "-1" << endl;
  else
    cout << max(ans1, ans2) << endl;
  return 0;
}
