#include <bits/stdc++.h>
using namespace std;
stack<char> sk;
vector<string> &split(string &s, char delim, vector<string> &elems) {
  stringstream ss(s);
  string item;
  while (getline(ss, item, delim)) {
    elems.push_back(item);
  }
  return elems;
}
int bfs(vector<vector<int> > &graph, int t) {
  bool visited[401] = {false};
  int distance[401] = {0};
  int source = 1;
  visited[1] = true;
  distance[1] = 0;
  queue<int> s;
  s.push(source);
  int l;
  if (graph[1][t] == 1) {
    l = 0;
  } else {
    l = 1;
  }
  while (!s.empty()) {
    int u = s.front();
    s.pop();
    for (int i = 1; i <= t; i++) {
      if (graph[u][i] == l && visited[i] == false) {
        s.push(i);
        visited[i] = true;
        distance[i] = distance[u] + 1;
      }
    }
  }
  if (distance[t] == 0) return -1;
  if (distance[t] < 1) return 1;
  return distance[t];
}
int main() {
  int t, r;
  cin >> t >> r;
  vector<vector<int> > graph;
  graph.resize(t + 1);
  for (int i = 1; i <= t; i++) {
    graph[i].resize(t + 1);
  }
  for (int i = 1; i <= r; i++) {
    int t1, t2;
    cin >> t1 >> t2;
    graph[t1][t2] = 1;
    graph[t2][t1] = 1;
  }
  cout << bfs(graph, t);
  return 0;
}
