#include<bits/stdc++.h>
using namespace std;
#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long
#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
const int mod=1e9+7;
const int maxn=3e5+100;
const int maxm=1e5+100;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){
if(y==0) return x;
return gcd(y,x%y);
}
int n,m;
pair<int,pii> g[maxn];
int dp[2][maxn];
int main(){
ios::sync_with_stdio(false);
cin>>n>>m;
rep(i,0,m){
int x,y,z;
cin>>g[i].se.fi>>g[i].se.se>>g[i].fi;
}
sort(g,g+m);
g[m].fi=-1;
int pre=0,ans=1;
rep(i,0,m+1){
dp[0][i]=dp[1][g[i].se.fi]+1;
if(i+1<=m&&g[i+1].fi!=g[i].fi){
rep(j,pre,i+1){
dp[1][g[j].se.se]=max(dp[1][g[j].se.se],dp[0][j]);
ans=max(ans,dp[1][g[j].se.se]);
}
pre=i+1;
}
}
cout<<ans;
return 0;
}