#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> W[100005];
int dp[300005], best[300005];
int main() {
  int n, m, a, b, c, ans = 0, mx = 0;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    W[c].push_back(make_pair(a, b));
    mx = max(c, mx);
  }
  for (int i = mx; i > 0; i--) {
    vector<int> tmp;
    for (auto p : W[i]) {
      a = p.first, b = p.second;
      best[a] = max(best[a], 1 + dp[b]);
      tmp.push_back(a);
    }
    for (int v : tmp) {
      dp[v] = max(dp[v], best[v]);
      best[v] = 0;
    }
  }
  for (int i = 1; i <= n; i++) ans = max(ans, dp[i]);
  printf("%d\n", ans);
  return 0;
}
