#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char* x) { cerr << '\"' << x << '\"'; }
void __print(const string& x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V>& x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T& x) {
  int f = 0;
  cerr << '{';
  for (auto& i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
  for (auto& i : v) is >> i;
  return is;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T>& v) {
  for (auto& i : v) os << i << ' ';
  return os;
}
template <typename T, typename U>
istream& operator>>(istream& is, pair<T, U>& p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, pair<T, U>& p) {
  os << p.first << ' ' << p.second;
  return os;
}
inline long long int ce(long long int a, long long int b) {
  return (a % b == 0 ? a / b : a / b + 1);
}
inline long long int powm(long long int b, long long int p, long long int m) {
  b %= m;
  if (b == 0) {
    return 0;
  }
  long long int ans = 1;
  while (p > 0) {
    if (p % 2 == 1) {
      ans = (ans * b) % m;
    }
    p /= 2;
    b = (b * b) % m;
  }
  return ans;
}
inline long long int gcd(long long int a, long long int b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
inline long long int lcm(long long int a, long long int b) {
  return ((a * b) / gcd(a, b));
}
inline bool ispow2(int x) { return (x != 0 && (x & (x - 1)) == 0); }
inline bool isprime(long long int n) {
  if (n == 0 || n == 1) {
    return 0;
  } else if (n == 2 || n == 3) {
    return 1;
  }
  if (n % 2 == 0) {
    return 0;
  }
  long long int t = floor(sqrt(n));
  for (long long int i = 3; i <= t; i += 2) {
    if (n % i == 0) {
      return 0;
    }
  }
  return 1;
}
inline long long int ncr(long long int n, long long int r) {
  if (n < r) {
    return 0;
  }
  long long int ans = 1;
  long long int k = min(n - r, r);
  for (long long int i = 1; i <= k; i++) {
    ans = ans * (n - i + 1) / i;
  }
  return ans;
}
inline long long int fib(long long int n) {
  const long double r5 = sqrt(5);
  const long double ra = (r5 + 1) / 2;
  return round(pow(ra, n) / r5);
}
long long int phi[1000001];
inline void totient() {
  for (int i = 1; i < 1000001; i++) phi[i] = (i & 1) ? i : i / 2;
  for (long long int i = 3; i < 1000001; i += 2)
    if (phi[i] == i)
      for (long long int j = i; j < 1000001; j += i) phi[j] -= phi[j] / i;
}
int spf[1000001];
inline void find_spf() {
  long long int n = 1000000;
  for (long long int i = 1; i <= n; i++) spf[i] = i;
  for (long long int i = 2; i <= n; i++) {
    for (long long int j = i * i; j <= n; j += i) {
      if (spf[j] == j) {
        spf[j] = i;
      }
    }
  }
}
unordered_map<int, int> find_pf(int n) {
  unordered_map<int, int> pf;
  while (n != 1) {
    pf[spf[n]]++;
    n /= spf[n];
  }
  return pf;
}
const int vertices = 401;
vector<int> road[vertices];
vector<int> rail[vertices];
int bfsRoad(int n) {
  vector<int> dist(n, INT_MAX);
  queue<pair<int, int>> q;
  q.push({0, 0});
  dist[0] = 0;
  vector<int> visited(n, 0);
  visited[0] = 1;
  while (!q.empty()) {
    pair<int, int> parent = q.front();
    q.pop();
    for (int child : road[parent.second]) {
      if (!visited[child]) {
        q.push({parent.first + 1, child});
        dist[child] = min(dist[child], parent.first + 1);
        visited[child] = 1;
      }
    }
  };
  return dist[n - 1] == INT_MAX ? -1 : dist[n - 1];
}
int bfsRail(int n) {
  vector<int> dist(n, INT_MAX);
  queue<pair<int, int>> q;
  q.push({0, 0});
  dist[0] = 0;
  vector<int> visited(n, 0);
  visited[0] = 1;
  while (!q.empty()) {
    pair<int, int> parent = q.front();
    q.pop();
    for (int child : rail[parent.second]) {
      if (!visited[child]) {
        q.push({parent.first + 1, child});
        dist[child] = min(dist[child], parent.first + 1);
        visited[child] = 1;
      }
    }
  };
  return dist[n - 1] == INT_MAX ? -1 : dist[n - 1];
}
void printPath(int n) {
  for (int i = 0; i < n; i++) {
    cout << i + 1 << "->";
    for (int j : road[i]) {
      cout << j + 1 << " ";
    }
    cout << '\n';
  }
  for (int i = 0; i < n; i++) {
    cout << i + 1 << "->";
    for (int j : rail[i]) {
      cout << j + 1 << " ";
    }
    cout << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  int adj[n][n];
  for (int i = 0; i < n; i++) {
    road[i].clear();
    rail[i].clear();
  }
  memset(adj, 0, sizeof(adj));
  while (m--) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    adj[u][v] = 1;
    adj[v][u] = 1;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j)
        continue;
      else if (adj[i][j]) {
        rail[i].push_back(j);
        rail[j].push_back(i);
      } else {
        road[i].push_back(j);
        road[j].push_back(i);
      }
    }
  }
  int timeTakenByRoad = bfsRoad(n);
  int timeTakenByRail = bfsRail(n);
  if (timeTakenByRoad == -1 || timeTakenByRail == -1)
    cout << -1 << '\n';
  else
    cout << max(timeTakenByRail, timeTakenByRoad);
  return 0;
}
