func_code_string: #include <bits/stdc++.h>   using namespace std;   const int maxn = 5e3 + 10;   int n, m, c = 0, ans = 0;   vector<int> adj[maxn], bkadj[maxn], components, nodes[maxn], sink;   bool mark[maxn];   int cmp[maxn], d[maxn], o[maxn];   stack<int> st;   queue<int> q;   void input() {     cin >> n >> m;     for (int i = 0; i < m; i++) {       int fi, se;       cin >> fi >> se;       adj[fi].push_back(se);       bkadj[se].push_back(fi);     }   }   void dfs1(int x) {     mark[x]++;     for (int i = 0; i < adj[x].size(); i++) {       int child = adj[x][i];       if (!mark[child]) dfs1(child);     }     st.push(x);   }   void dfs2(int x) {     mark[x]++;     nodes[cmp[x]].push_back(x);     for (int i = 0; i < bkadj[x].size(); i++) {       int child = bkadj[x][i];       if (!mark[child]) {         cmp[child] = cmp[x];         dfs2(child);       } else if (cmp[child] != cmp[x])         o[cmp[child]]++;     }   }   int bfs(int x) {     memset(mark, 0, sizeof mark);     q.push(x);     d[x] = 1;     mark[x]++;     int mi = 1e8;     while (!q.empty()) {       int cur = q.front();       q.pop();       for (int i = 0; i < adj[cur].size(); i++) {         int child = adj[cur][i];         if (!mark[child]) {           mark[child]++;           d[child] = d[cur] + 1;           q.push(child);         } else if (child == x)           mi = min(mi, d[cur]);       }     }     return mi;   }   void task() {     for (int i = 1; i <= n; i++)       if (!mark[i]) dfs1(i);     memset(mark, 0, sizeof mark);     while (!st.empty()) {       int x = st.top();       st.pop();       if (!mark[x]) cmp[x] = x, components.push_back(x), dfs2(x);     }     for (int i = 0; i < components.size(); i++)       if (o[components[i]] == 0) sink.push_back(components[i]);     for (int i = 0; i < sink.size(); i++) {       int cm = sink[i];       if (nodes[cm].size() != 1) {         c++;         int mi = 1e8;         for (int i = 0; i < nodes[cm].size(); i++) {           int cur = nodes[cm][i];           mi = min(mi, bfs(cur));         }         ans += mi;       }     }   }   void debug() {     for (int i = 1; i <= n; i++) cerr << i <<  :  << cmp[i] << endl;   }   int main() {     input();     task();     cout << n + (ans * 998) + c << endl;     return 0;   }   