#include using namespace std; bool dfs(int n,vector>&road,vector&use1,vector&use2) { if(use2[n]) return true; use1[n] = use2[n] = true; bool f = false; for(int i:road[n]) { if(use2[i] || (!use1[n] && dfs(i,road,use1,use2))) { f = true; break; } } use2[n] = false; return f; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N,Q; cin >> N >> Q; vectorA(Q),B(Q); for(int i = 0; i < Q; i++) { cin >> A[i] >> B[i]; A[i]--; B[i]--; } int l = -1,r = Q; while (l+1 < r) { int mid = (l+r)/2; vector>road(N); for(int i = 0; i <= mid; i++) { road[A[i]].push_back(B[i]); } vectoruse1(N),use2(N); bool f = false; for(int i = 0; i < N; i++) { if(!use1[i] && dfs(i,road,use1,use2)) { f = true; break; } } if(f) { r = mid; } else { l = mid; } } cout << ((r == Q)?-1:r+1) << endl; }