#include <bits/stdc++.h>
using namespace std;
#define ll long long
vector<map<ll, ll>> mp;
ll get(ll x, ll w)
{
auto it = mp[x].lower_bound(w);
if(it == mp[x].begin())
return 1;
it--;
return it->second + 1;
}
void solve()
{
ll n, m;
cin >> n >> m;
mp.resize(n+1);
ll answer = 0;
for (ll i = 0; i < m; i++)
{
ll a, b, w;
cin >> a >> b >> w;
ll val = get(a, w);
if(get(b, w + 1) > val)
continue;
mp[b][w] = max(mp[b][w], val);
auto it = mp[b].upper_bound(w);
while(it != mp[b].end() && it->second <= val)
{
it = mp[b].erase(it);
}
answer = max(answer,val);
}
cout << answer << endl;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}/*1691053055.4208179*/
553A - Kyoya and Colored Balls | 1364A - XXXXX |
1499B - Binary Removals | 1569C - Jury Meeting |
108A - Palindromic Times | 46A - Ball Game |
114A - Cifera | 776A - A Serial Killer |
25B - Phone numbers | 1633C - Kill the Monster |
1611A - Make Even | 1030B - Vasya and Cornfield |
1631A - Min Max Swap | 1296B - Food Buying |
133A - HQ9+ | 1650D - Twist the Permutation |
1209A - Paint the Numbers | 1234A - Equalize Prices Again |
1613A - Long Comparison | 1624B - Make AP |
660B - Seating On Bus | 405A - Gravity Flip |
499B - Lecture | 709A - Juicer |
1358C - Celex Update | 1466B - Last minute enhancements |
450B - Jzzhu and Sequences | 1582C - Grandma Capa Knits a Scarf |
492A - Vanya and Cubes | 217A - Ice Skating |