#include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; int N, Q; int A[100010]; int B[100010]; map> E; map> RE; bool has_cycle(int x) { vector cost(N + 1, 9999); for (int i = 0; i < N; ++i) { for (int j = 0; j < x; ++j) { int a = A[j]; int b = B[j]; if (cost[b] > cost[a] - 1) { cost[b] = cost[a] - 1; if (i == N - 1) { return true; } } } } return false; } int main() { cin >> N >> Q; for (int i = 0; i < Q; ++i) { cin >> A[i] >> B[i]; } if (not has_cycle(Q)) { cout << -1 << endl; return 0; } int ok = Q; int ng = 0; while (abs(ok - ng) >= 2) { int x = (ok + ng) / 2; if (has_cycle(x)) { ok = x; } else { ng = x; } } cout << ok << endl; return 0; }