func_code_string: #include <bits/stdc++.h>   const int inf = 2 * 1e9;   const int mod = 1e9 + 7;   const long long infll = (long long)1e18;   const long double eps = 1e-9;   const int dx[] = {1, 0, -1, 0};   const int dy[] = {0, -1, 0, 1};   const int MAXY = 1005000;   using namespace std;   int n, m;   vector<vector<pair<int, int>>> g;   vector<bool> used;   vector<int> topsort;   void dfs(int v, int time) {     used[v] = true;     for (int i = 0; i < g[v].size(); i++) {       int to = g[v][i].first;       int edgeTime = g[v][i].second;       if (edgeTime <= time) {         if (!used[to]) dfs(to, time);       }     }     topsort.push_back(v);   }   bool check(int time) {     used.assign(n, false);     topsort.clear();     for (int i = 0; i < n; i++) {       if (!used[i]) dfs(i, time);     }     reverse(topsort.begin(), topsort.end());     for (int i = 0; i < (n - 1); i++) {       int cur = topsort[i];       int nxt = topsort[i + 1];       bool ok = false;       for (int j = 0; j < g[cur].size(); j++) {         if (g[cur][j].second <= time) ok = ok || g[cur][j].first == nxt;       }       if (!ok) return false;     }     return true;   }   void solve() {     cin >> n >> m;     g.resize(n);     used.resize(n);     for (int i = 0; i < m; i++) {       int x, y;       cin >> x >> y;       x--;       y--;       g[x].push_back(make_pair(y, i));     }     if (!check(m - 1)) {       cout << -1;       return;     }     int tl = 0;     int tr = m - 1;     while (tr - tl > 1) {       int tm = (tl + tr) / 2;       if (check(tm))         tr = tm;       else         tl = tm;     }     cout << tr + 1;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     solve();     return 0;   }   