#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> dp(300005), ttt(300005, INT_MAX);
vector<vector<pair<int, int> > > edge(300005);
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    edge[w].push_back(make_pair(u, v));
  }
  for (int i = 1; i < 300005; i++) {
    for (auto it : edge[i]) {
      int u = it.first;
      int v = it.second;
      ttt[v] = 0;
    }
    for (auto it : edge[i]) {
      int u = it.first;
      int v = it.second;
      ttt[v] = max(ttt[v], dp[u] + 1);
    }
    for (auto it : edge[i]) {
      int u = it.first;
      int v = it.second;
      dp[v] = max(ttt[v], dp[v]);
    }
  }
  int fans = 0;
  for (int i = 1; i <= n; i++) fans = max(fans, dp[i]);
  cout << fans;
  return 0;
}
