#include using namespace std; int main() { int N, M; cin >> N >> M; vector> G(N); for (int i = 0; i < M; i++) { int u, v; cin >> u >> v; u--, v--; G[u].push_back(v); // G[v].push_back(u); } const int inf = 10000000; queue que; vector dist1(N, inf), dist2(N, inf), dist3(N, inf); que.push(0); dist1[0] = 0; while (!que.empty()) { int cur = que.front(); que.pop(); for (auto nxt : G[cur]) { if (dist1[nxt] > dist1[cur] + 1) { que.push(nxt); dist1[nxt] = dist1[cur] + 1; } } } que.push(N - 2); dist2[N - 2] = 0; while (!que.empty()) { int cur = que.front(); que.pop(); for (auto nxt : G[cur]) { if (dist2[nxt] > dist2[cur] + 1) { que.push(nxt); dist2[nxt] = dist2[cur] + 1; } } } que.push(N - 1); dist3[N - 1] = 0; while (!que.empty()) { int cur = que.front(); que.pop(); for (auto nxt : G[cur]) { if (dist3[nxt] > dist3[cur] + 1) { que.push(nxt); dist3[nxt] = dist3[cur] + 1; } } } int ans = min(dist1[N - 2] + dist2[N - 1] + dist3[0], dist1[N - 1] + dist3[N - 2] + dist2[0]); if (ans >= inf) { ans = -1; } cout << ans << endl; return 0; } // iku junban // 1 -> N - 1 -> N -> 1 // 1 -> N -> N - 1 -> 1