#include <bits/stdc++.h>
using namespace std;
map<long long, long long> mp[100002];
long long query(long long x, long long w) {
  long long mx = 0;
  for (; w > 0; w -= w & -w) {
    auto it = mp[x].find(w);
    if (it != mp[x].end()) mx = max(mx, it->second);
  }
  return mx + 1;
}
void update(long long x, long long j, long long w) {
  for (; w <= 100001; w += w & -w) mp[x][w] = max(mp[x][w], j);
}
int main() {
  long long n, m, i, j, x, y, w, ans;
  cin >> n >> m;
  ans = 0;
  for (i = 0; i < m; i++) {
    cin >> x >> y >> w;
    j = query(x, w);
    update(y, j, w + 1);
  }
  for (i = 1; i <= n; i++) ans = max(ans, query(i, 100001) - 1);
  cout << ans;
}
