#include using namespace std; #define ll long long const ll INF = 1ll << 60; void bfs(int start, vector> &graph, vector &dists) { dists[start] = 0; queue> q; q.push(pair(start, 0)); while (!q.empty()) { int now_v = q.front().first; ll d = q.front().second; q.pop(); for (int next_v : graph[now_v]) { if (dists[next_v] < INF) continue; dists[next_v] = d + 1; q.push(pair(next_v, d + 1)); } } } int main() { int N, M; cin >> N >> M; vector> graph(N, vector()); for (int i = 0; i < M; i++) { int u, v; cin >> u >> v; u -= 1, v -= 1; graph[u].push_back(v); } vector dist_0(N, INF); bfs(0, graph, dist_0); vector dist_N_2(N, INF); bfs(N-2, graph, dist_N_2); vector dist_N_1(N, INF); bfs(N-1, graph, dist_N_1); ll ans = min( dist_0[N-2] + dist_N_2[N-1] + dist_N_1[0], dist_0[N-1] + dist_N_1[N-2] + dist_N_2[0] ); if (ans < INF) cout << ans << endl; else cout << -1 << endl; }