#include<cstdio>
#include<bitset>
#include<vector>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<map>
#include<set>
#include<queue>
#include<algorithm>

using namespace std;

int sol = 0, curr_sz, nr, ctc, N, M, Q[100009], ap[100009], ap2[100009], comp[100009], sz[100009], dp[100009];
bool in_q[100009], ap3[100009], ap4[100009];
long long dist[100009];
vector < int > v[100009], h[100009];
vector < pair < int , int > > mu[100009];
queue < int > cc;

void dfs (int nod)
{
    ap[nod] = 1;
    for (vector < int > :: iterator it = v[nod].begin (); it != v[nod].end (); it ++)
        if (ap[*it] == 0)
            dfs (*it);
    Q[++nr] = nod;
}

void dfp (int nod)
{
    ap[nod] = ctc, comp[++curr_sz] = nod;
    for (vector < int > :: iterator it = h[nod].begin (); it != h[nod].end (); it ++)
        if (ap[*it] == 1)
            dfp (*it);
}

void Clear ()
{
    while (!cc.empty ()) cc.pop ();
}

bool fnd (int nod, int id)
{
    for (int i=1; i<=curr_sz; i++)
        dist[comp[i]] = 1LL << 60, in_q[comp[i]] = 0, ap2[comp[i]] = 0;
    Clear ();
    cc.push (nod), dist[nod] = 0, in_q[nod] = 1;
    while (!cc.empty ())
    {
        int nod = cc.front ();
        in_q[nod] = 0; cc.pop ();
        for (vector < pair < int , int > > :: iterator it = mu[nod].begin (); it != mu[nod].end (); it ++)
            if (ap[it->first] == id && dist[it->first] > dist[nod] + it->second)
            {
                dist[it->first] = dist[nod] + it->second;
                if (++ap2[it->first] > curr_sz) return 1;
                if (in_q[it->first] == 0) in_q[it->first] = 1, cc.push (it->first);
            }
    }
    return 0;
}

void df (int nod)
{
    ap4[nod] = 1, dp[nod] = ap3[nod];
    for (vector < int > :: iterator it = h[nod].begin (); it != h[nod].end (); it ++)
        if (ap4[*it] == 0)
        {
            df (*it);
            dp[nod] |= ap3[*it];
        }
}

int main()
{
//freopen ("input", "r", stdin);
//freopen ("output", "w", stdout);

scanf ("%d %d", &N, &M);
while (M --)
{
    int x, y, z;
    scanf ("%d %d %d", &x, &y, &z);
    v[x].push_back (y);
    h[y].push_back (x);
    mu[x].push_back (make_pair (y, z));
}
for (int i=1; i<=N; i++)
    if (ap[i] == 0) dfs (i);
for (int i=nr; i>=1; i--)
    if (ap[Q[i]] == 1)
    {
        ctc --, curr_sz = 0, dfp (Q[i]), sz[-ctc] = curr_sz;
        if (fnd (Q[i], ctc)) ap3[-ctc] = 1;
    }
ctc = -ctc;
for (int i=1; i<=N; i++)
    ap[i] = -ap[i], h[i].clear ();
for (int i=1; i<=N; i++)
    for (vector < int > :: iterator it = v[i].begin (); it != v[i].end (); it ++)
        if (ap[i] != ap[*it]) h[ap[i]].push_back (ap[*it]);
for (int i=1; i<=ctc; i++)
    if (ap4[i] == 0) df (i);
for (int i=1; i<=ctc; i++)
    if (dp[i]) sol += sz[i];
printf ("%d\n", sol);
return 0;
}