#include <bits/stdc++.h>

using namespace std;

# define pb push_back
# define mp make_pair
# define FORN( a , b , c ) for ( int a = b ; a <= c ; ++ a )
# define FORNBACK( a , b , c ) for ( int a = b ; a >= c ; -- a )

map < pair < int , int > , int > H ;
const int MAX = 3e5 + 14 ;

bitset < MAX > sol ;

vector < int > gr [ MAX ] ;
vector < pair < int , int > > G [ MAX ] ;
vector < int > inv [ MAX ] ;
vector < int > ctc [ MAX ] ;

vector < int > descopera ;

bitset < MAX > viz ;

int cate ;

inline void dfs ( int nod )
{
    viz [ nod ] = 1 ;
    for ( auto x : gr [ nod ] )
        if ( viz [ x ] == 0 )
            dfs ( x ) ;
    descopera.pb ( nod ) ;
}

inline void dfs2 ( int nod )
{
    ctc [ cate ].pb ( nod ) ;
    viz [ nod ] = 0 ;
    for ( auto x : inv [ nod ] )
        if ( viz [ x ] == 1 )
            dfs2 ( x ) ;
}

int main()
{
    ios :: sync_with_stdio ( false ) ;

    //freopen( "input" , "r" , stdin ) ;
    //freopen( "output" , "w" , stdout ) ;
    int n ;
    int m ;
    cin >> n >> m ;
    FORN ( i , 1 , m )
    {
        int x , y , cost ;
        cin >> x >> y >> cost ;
        if ( x == y ) {
            if ( cost < 0 ) {
                sol [ x ] = 1 ;
                continue ;
            }
            continue ;
        }
        if ( H [ mp ( x , y ) ] > cost ) {
            H [ mp ( x , y ) ] = cost ;
        }
    }
    for ( auto x : H ) {
        gr [ x.first.first ].pb ( x.first.second ) ;
        inv [ x.first.second ].pb ( x.first.first ) ;
        G [ x.first.second ].pb ( mp ( x.first.first , x.second ) ) ;
    }
    FORN ( i , 1 , n )
        if ( !viz [ i ] ){
            dfs ( i ) ;
        }
    reverse ( descopera.begin() , descopera.end() ) ;
    for ( auto x : descopera )
        if ( viz [ x ] ) {
            ++ cate ;
            dfs2 ( x ) ;
        }
    FORN ( i , 1 , cate )
    {
        int ok = 0 ;
        for ( auto x : ctc [ i ] ) {
            for ( auto y : G [ x ] ) {
                if ( y.second < 0 ) {
                    ok = 1 ;
                }
            }
        }
        if ( ok )
        for ( auto x : ctc [ i ] ){
            sol [ x ] = 1 ;
        }
    }
    FORN ( i , 1 , n )
        for ( auto x : G [ i ] )
            if ( sol [ x.first ] ) sol [ i ] = 1 ;
    int S = 0 ;
    FORN ( i , 1 , n )
        S = S + sol [ i ] ;
    cout << S ;
    return 0 ;
}