#pragma GCC optimize ("O2")
#include "bits/stdc++.h"
using namespace std;
///////////// START TEMPLATE //////////////////////
#if DEBUG && !ONLINE_JUDGE
#include "../debug.h"
#else
#define debug(...)
#endif
template<typename T>
void _R(T &x){cin>>x;}
template<typename T>
void _R(vector<T> &x){for(auto it=x.begin();it!=x.end();it++){_R(*it);}}
void R(){}
template<typename T,typename... K>
void R(T& head,K&... tail){_R(head);R(tail...);}
template<typename T>
void _W(const T &x,const char c){cout<<x;}
template<typename T>
void _W(const vector<T> &x,const char c){for(auto it=x.cbegin();it!=x.cend();it++){if(it!=x.cbegin())putchar(c);_W(*it,c);}}
void W(){}
template<typename T,typename... K>
void W(const T& head,const K&... tail){_W(head,' ');cout<<(sizeof...(tail)?' ':'\n')<<flush;W(tail...);}
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef map<int,int> mii;
typedef map<string,int> msi;
typedef long long lli;
#define pb push_back
#define IT iterator
#define PQ priority_queue
#define GR greater
#define fi first
#define se second
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define REP(i,a) FOR(i,0,(int)(a)-1)
#define FORd(i,a,b) for(int i=(a);i>=(b);i--)
#define TR(c,it) for(auto it:(c))
#define size(x) ((int)((x).size()))
#define all(x) x.begin(), x.end()
#define uni(x) x.erase(unique(all(x)), x.end())
#define sqr(x) ((x)*(x))
#define LC(x) ((x)<<1)
#define RC(x) (((x)<<1)+1)
#define EPS 1e-9
#define EULER 2.7182818284
#define MOD 1000000007
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;
template<typename T> inline T gcd(T a,T b) {if(a==0)return b;return gcd(b%a,a);}
template<typename T> inline void amin(T& x,T y) {if(x>y)x=y;}
template<typename T> inline void amax(T& x,T y) {if(x<y)x=y;}
template<typename A,typename B>
class comp{public:bool operator()(const pair<A,B> &a, const pair<A,B> &b){
if(a.fi!=b.fi)return a.fi<b.fi;else return a.fi>b.fi;}};
lli fast_exp(lli a,lli b){lli res=1;
while(b){if(b&1LL){res*=a;res%=MOD;}
b>>=1LL;a*=a;a%=MOD;}
return res;}
//////////////// END TEMPLATE /////////////////////////
//global variables
int n, m;
struct edge {
int a, b;
int i;
int w;
};
const int N = 100000;
vector<pair<int, int>> g[N];
edge e[N];
int a[N] = {0};
int vis[N] = {0};
//end global variables
void preprocess(void) {
return;
}
void dfse(int x) {
vis[x] = 1;
a[x] = 1;
int v = e[x].b;
TR(g[v], u) {
if (vis[u.se] and e[u.se].i > e[x].i and e[u.se].w > e[x].w) {
a[x] = max(a[x], a[u.se] + 1);
} else if (!vis[u.se] ) {
if (e[u.se].i > e[x].i and e[u.se].w > e[x].w) {
dfse(u.se);
a[x] = max(a[x], a[u.se] + 1);
}
}
}
}
signed main (int argc, char* argv[], char* envp[]){
//freopen("output.txt", "w", stdout);
//freopen("input.txt", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.precision(20);
preprocess();
int teeee;
teeee = 1;
//cin >> teeee;
FOR(zeeee, 1, teeee) {
//cout << "Case #" << zeeee << ": ";
cin >> n >> m;
REP(i, m) {
int a, b, w;
cin >> a >> b >> w;
a--, b--;
e[i] = {a, b, i, w};
g[a].pb({b, i});
}
REP(i, m) {
if (!vis[i]) {
dfse(i);
}
}
int ans = 0;
REP(i, m) {
amax(ans, a[i]);
}
cout << ans << endl;
}
cerr << (((double) clock()) / ((double) CLOCKS_PER_SEC)) << endl;
return 0;
}
I3ByYWdtYSBHQ0Mgb3B0aW1pemUgKCJPMiIpCiNpbmNsdWRlICJiaXRzL3N0ZGMrKy5oIgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKLy8vLy8vLy8vLy8vLyBTVEFSVCBURU1QTEFURSAvLy8vLy8vLy8vLy8vLy8vLy8vLy8vCiNpZiBERUJVRyAmJiAhT05MSU5FX0pVREdFCiAgICAjaW5jbHVkZSAiLi4vZGVidWcuaCIKI2Vsc2UKICAgICNkZWZpbmUgZGVidWcoLi4u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F0gPSBtYXgoYVt4XSwgYVt1LnNlXSArIDEpOwogICAgICAgIH0gZWxzZSBpZiAoIXZpc1t1LnNlXSApIHsKICAgICAgICAgICAgaWYgKGVbdS5zZV0uaSA+IGVbeF0uaSBhbmQgZVt1LnNlXS53ID4gZVt4XS53KSB7CiAgICAgICAgICAgICAgICBkZnNlKHUuc2UpOwogICAgICAgICAgICAgICAgYVt4XSA9IG1heChhW3hdLCBhW3Uuc2VdICsgMSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9Cn0KCnNpZ25lZCBtYWluIChpbnQgYXJnYywgY2hhciogYXJndltdLCBjaGFyKiBlbnZwW10pewogICAgLy9mcmVvcGVuKCJvdXRwdXQudHh0IiwgInciLCBzdGRvdXQpOwogICAgLy9mcmVvcGVuKCJpbnB1dC50eHQiLCAiciIsIHN0ZGluKTsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogICAgY2luLnRpZShOVUxMKTsKICAgIGNvdXQucHJlY2lzaW9uKDIwKTsKICAgIHByZXByb2Nlc3MoKTsKICAgIGludCB0ZWVlZTsKICAgIHRlZWVlID0gMTsKICAgIC8vY2luID4+IHRlZWVlOwoKICAgIEZPUih6ZWVlZSwgMSwgdGVlZWUpIHsKICAgICAgICAvL2NvdXQgPDwgIkNhc2UgIyIgPDwgemVlZWUgPDwgIjogIjsKICAgICAgICBjaW4gPj4gbiA+PiBtOwogICAgICAgIFJFUChpLCBtKSB7CiAgICAgICAgICAgIGludCBhLCBiLCB3OwogICAgICAgICAgICBjaW4gPj4gYSA+PiBiID4+IHc7CiAgICAgICAgICAgIGEtLSwgYi0tOwogICAgICAgICAgICBlW2ldID0ge2EsIGIsIGksIHd9OwogICAgICAgICAgICBnW2FdLnBiKHtiLCBpfSk7CiAgICAgICAgfQogICAgICAgIFJFUChpLCBtKSB7CiAgICAgICAgICAgIGlmICghdmlzW2ldKSB7CiAgICAgICAgICAgICAgICBkZnNlKGkpOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGludCBhbnMgPSAwOwogICAgICAgIFJFUChpLCBtKSB7CiAgICAgICAgICAgIGFtYXgoYW5zLCBhW2ldKTsKICAgICAgICB9CiAgICAgICAgY291dCA8PCBhbnMgPDwgZW5kbDsKICAgIH0KCiAgICBjZXJyIDw8ICgoKGRvdWJsZSkgY2xvY2soKSkgLyAoKGRvdWJsZSkgQ0xPQ0tTX1BFUl9TRUMpKSA8PCBlbmRsOwogICAgcmV0dXJuIDA7Cn0=