#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<pair<int, int> > adj[100005];
int dp[300005];
int x[300005];
int main() {
  ios_base::sync_with_stdio(0);
  int i, j;
  cin >> n >> m;
  while (m--) {
    int u, v, e;
    cin >> u >> v >> e;
    adj[e].push_back(make_pair(u, v));
  }
  for (i = 1; i <= 1e5; i++) {
    for (j = 0; j < adj[i].size(); j++) {
      int u = adj[i][j].second;
      x[u] = 0;
    }
    for (j = 0; j < adj[i].size(); j++) {
      int u = adj[i][j].first;
      int v = adj[i][j].second;
      x[v] = max(x[v], dp[u] + 1);
    }
    for (j = 0; j < adj[i].size(); j++) {
      int u = adj[i][j].second;
      dp[u] = max(dp[u], x[u]);
    }
  }
  int res = 0;
  for (i = 1; i <= n; i++) res = max(res, dp[i]);
  cout << res;
  return 0;
}
