#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  int dp[n + 1][n + 1];
  int i, j;
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= n; j++) dp[i][j] = -1;
  }
  for (i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    dp[a][b] = 1;
    dp[b][a] = 1;
  }
  int l1 = 0;
  int l2 = 0;
  if (dp[1][n] == 1) {
    l1 = 1;
    queue<pair<int, int>> q;
    q.push({1, 0});
    l2 = -1;
    bool vis[n + 1];
    for (i = 0; i <= n; i++) vis[i] = false;
    vis[1] = true;
    while (!q.empty()) {
      pair<int, int> p = q.front();
      q.pop();
      int x = p.first;
      if (x == n) {
        l2 = p.second;
        break;
      }
      for (i = 1; i <= n; i++) {
        if (dp[x][i] == -1 and vis[i] == false) {
          vis[i] = true;
          q.push({i, p.second + 1});
        }
      }
    }
    if (l2 == -1)
      cout << -1;
    else
      cout << max(l1, l2);
  } else {
    l1 = 2;
    queue<pair<int, int>> q;
    q.push({1, 0});
    l1 = -1;
    bool vis[n + 1];
    for (i = 0; i <= n; i++) vis[i] = false;
    vis[1] = true;
    while (!q.empty()) {
      pair<int, int> p = q.front();
      q.pop();
      int x = p.first;
      if (x == n) {
        l1 = p.second;
        break;
      }
      for (i = 1; i <= n; i++) {
        if (dp[x][i] == 1 and vis[i] == false) {
          vis[i] = true;
          q.push({i, p.second + 1});
        }
      }
    }
    if (l1 == -1)
      cout << -1;
    else
      cout << max(l1, l2);
  }
}
