#include <bits/stdc++.h>
using namespace std;
vector<int> v1[402], v2[402];
bool visited[402], vis[402][402];
int level[402], b, a;
int bfs(vector<int> (&v)[402]) {
  memset(level, -1, sizeof level);
  visited[1] = true;
  level[1] = 0;
  queue<int> qu;
  qu.push(1);
  while (!qu.empty()) {
    int x = qu.front();
    qu.pop();
    for (int i = 0; i < v[x].size(); i++) {
      int y = v[x][i];
      if (!visited[y]) {
        visited[y] = true;
        qu.push(y);
        level[y] = level[x] + 1;
      }
    }
  }
  memset(visited, 0, sizeof visited);
  return level[a];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int i, c, d, j;
  cin >> a >> b;
  for (i = 0; i < b; i++) {
    cin >> c >> d;
    v1[c].push_back(d);
    v1[d].push_back(c);
    vis[c][d] = vis[d][c] = 1;
  }
  for (i = 1; i <= a; i++) {
    for (j = 1; j <= a; j++) {
      if (i != j && vis[i][j] == 0) {
        v2[i].push_back(j);
        v2[j].push_back(i);
      }
    }
  }
  int x = bfs(v1);
  int y = bfs(v2);
  if (min(x, y) == -1) x = y = -1;
  cout << max(x, y) << endl;
  return 0;
}
