#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
map<int, int> mp[N];
map<int, int>::iterator it, jt;
int n, m, u, v, w, ans;
signed main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> u >> v >> w;
    it = mp[u].lower_bound(w);
    int a = (it == mp[u].begin()) ? 1 : 1 + (--it)->second;
    it = mp[v].upper_bound(w);
    int c = (it == mp[v].begin()) ? 0 : (--it)->second;
    if (c < a) {
      ans = max(ans, mp[v][w] = max(mp[v][w], a));
      for (it = mp[v].upper_bound(w); !(it == mp[v].end() || (it->second > a));)
        mp[v].erase(it++);
    }
  }
  cout << ans;
}
