#include <bits/stdc++.h>
const long long oo = 1e9;
const long long mod = 95542721;
const long long MAX = 1e9;
const long long ARR = 410;
using namespace std;
vector<vector<int> > g(410), G(410);
vector<int> dist(410, MAX);
bool found(int n, vector<int> v) {
  for (long long i = (0); i < (long long)(v.size()); ++i) {
    if (v[i] == n) return 1;
  }
  return 0;
}
void BFS(vector<vector<int> > graph) {
  dist[0] = 0;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (long long i = (0); i < (long long)(graph[u].size()); ++i) {
      int v = graph[u][i];
      if (dist[v] == MAX) {
        dist[v] = dist[u] + 1;
        q.push(v);
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (long long i = (0); i < (long long)(m); ++i) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  if (found(n - 1, g[0])) {
    for (long long i = (0); i < (long long)(n); ++i) {
      for (long long j = (0); j < (long long)(n); ++j) {
        if (!found(i, g[j]) && !found(j, g[i])) {
          G[i].push_back(j);
          G[j].push_back(i);
        }
      }
    }
    BFS(G);
    if (dist[n - 1] == MAX)
      cout << -1;
    else
      cout << dist[n - 1];
  } else {
    BFS(g);
    if (dist[n - 1] == MAX)
      cout << -1;
    else
      cout << dist[n - 1];
  }
  return 0;
}
