#include using namespace std; const int NMax = 1e5 + 5; const int INF = 2e9; int n, ans; int D[NMax], A[NMax]; bool Ciclu[NMax]; vector < pair < int, int > > G[NMax]; vector < int > Gi[NMax]; deque < int > Q; inline void Bellman(const int &start){ int node; Q.clear(); Q.push_back(start); for(int i = 1; i <= n; i++){ A[i] = INF; } A[start] = 0; while(!Q.empty()){ node = Q.front(); Q.pop_front(); for(auto it: G[node]){ if(A[it.first] > A[node] + it.second){ A[it.first] = A[node] + it.second; Q.push_back(it.first); D[it.first]++; if(D[it.first] > n){ Ciclu[it.first] = 1; return; } } } } } bool viz[NMax]; inline void BFS(){ int node; while(!Q.empty()){ node = Q.front(); Q.pop_front(); for(auto it: Gi[node]){ if(!viz[it]){ ans++; viz[it] = 1; Q.push_back(it); } } } } int main(){ /*#ifndef ONLINE_JUDGE freopen("debug.in", "r", stdin); #endif // ONLINE_JUDGE*/ freopen("a.in", "r", stdin); int m, a, b, c; cin >> n >> m; for(int i = 1; i <= m; i++){ cin >> a >> b >> c; G[a].push_back({b, c}); Gi[b].push_back(a); } for(int i = 1; i <= n; i++){ if(D[i] == 0){ Bellman(i); } } Q.clear(); for(int i = 1; i <= n; i++){ if(Ciclu[i]){ ans++; viz[i] = 1; Q.push_back(i); } } BFS(); cout << ans; return 0; }