func_code_string: #include <bits/stdc++.h>   using namespace std;   pair<int, int> arr[100005];   vector<int> v1[100005];   int visited[100005];   stack<int> s;   int n, m;   set<int> se[100005];   void dfs(int node) {     int i;     visited[node] = 1;     int n = v1[node].size();     for (i = 0; i < n; i++) {       if (visited[v1[node][i]] == 0) {         dfs(v1[node][i]);       }     }     s.push(node);   }   bool check(int mid) {     int i;     bool ans = true;     for (i = 1; i <= mid; i++) {       v1[arr[i].first].push_back(arr[i].second);       se[arr[i].first].insert(arr[i].second);     }     for (i = 1; i <= n; i++) visited[i] = 0;     for (i = 1; i <= n; i++) {       if (visited[i] == 0) {         dfs(i);       }     }     int x;     x = s.top();     s.pop();     while (!s.empty()) {       if (se[x].find(s.top()) == se[x].end()) ans = false;       x = s.top();       s.pop();     }     for (i = 1; i <= n; i++) {       v1[i].clear();       se[i].clear();     }     return ans;   }   int main() {     int i, j, k;     scanf( %d , &n);     scanf( %d , &m);     for (i = 1; i <= m; i++) {       scanf( %d , &arr[i].first);       scanf( %d , &arr[i].second);     }     int l = 0, r = m, mid;     while (l <= r) {       mid = (l + r) / 2;       bool temp1 = check(mid);       if (mid == m) {         if (temp1 == false) {           printf( -1 n );           return 0;         } else {           r = mid - 1;           continue;         }       }       bool temp2 = check(mid + 1);       if (temp1 == false && temp2 == true) {         printf( %d n , mid + 1);         return 0;       } else if (temp2 == true) {         r = mid - 1;       } else {         l = mid + 1;       }     }     printf( -1 n );     return 0;   }   