#include using namespace std; const int INF = 100000000; 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); } queue q; q.push(0); vector dist1(N, INF); dist1[0] = 0; while (!q.empty()) { int now = q.front(); q.pop(); for (auto next : G[now]) { if (dist1[next] == INF) { q.push(next); dist1[next] = dist1[now] + 1; } } } q.push(N - 2); vector dist2(N, INF); dist2[N - 2] = 0; while (!q.empty()) { int now = q.front(); q.pop(); for (auto next : G[now]) { if (dist2[next] == INF) { q.push(next); dist2[next] = dist2[now] + 1; } } } q.push(N - 1); vector dist3(N, INF); dist3[N - 1] = 0; while (!q.empty()) { int now = q.front(); q.pop(); for (auto next : G[now]) { if (dist3[next] == INF) { q.push(next); dist3[next] = dist3[now] + 1; } } } int ans = dist1[N - 2] + dist2[N - 1] + dist3[0]; ans = min(ans, dist1[N - 1] + dist3[N - 2] + dist2[0]); if (ans < INF) { cout << ans << endl; } else { cout << -1 << endl; } }