#include <iostream>
#include <algorithm>
using namespace std;
int n,m,ans[300005],d[300005],an;
struct node
{
int u,v,w;
}a[300005];
bool cmp(node e,node f)
{
return e.w < f.w;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin >> n >> m;
for(int i = 1;i <= m;i ++)cin >> a[i].u >> a[i].v >> a[i].w;
sort(a + 1,a + m + 1,cmp);
for(int i = 1;i <= m;i ++)
if(a[i].w > d[a[i].u])
if(ans[a[i].v] < ans[a[i].u] + 1)
{
ans[a[i].v] = ans[a[i].u] + 1;
d[a[i].v] = a[i].w;
}
for(int i = 1;i <= n;i ++)an = max(an,ans[i]);
cout << an;
}