#include <bits/stdc++.h>
using namespace std;
const int MAX = 300005;
vector<pair<int, int> > edge[MAX];
int dp[MAX], cur[MAX];
int main() {
  int n, m, u, v, w;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &u, &v, &w);
    edge[w].push_back(make_pair(u, v));
  }
  for (int i = 0; i < MAX; i++) {
    for (int j = 0; j < edge[i].size(); j++) {
      u = edge[i][j].first;
      v = edge[i][j].second;
      cur[v] = max(cur[v], dp[u] + 1);
    }
    for (int j = 0; j < edge[i].size(); j++) {
      u = edge[i][j].first;
      v = edge[i][j].second;
      dp[v] = max(dp[v], cur[v]);
    }
  }
  int ans = 0;
  for (int i = 1; i <= MAX; i++) ans = max(ans, dp[i]);
  printf("%d", ans);
}
