#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
int n, m, ans;
int dp[N], tmp[N];
vector<pair<int, int> > w[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  while (m--) {
    int u, v, weight;
    cin >> u >> v >> weight;
    u--, v--;
    w[weight].push_back({u, v});
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < w[i].size(); j++) {
      int u = w[i][j].first, v = w[i][j].second;
      tmp[v] = 0;
    }
    for (int j = 0; j < w[i].size(); j++) {
      int u = w[i][j].first, v = w[i][j].second;
      tmp[v] = max(tmp[v], dp[u] + 1);
    }
    for (int j = 0; j < w[i].size(); j++) {
      int u = w[i][j].first, v = w[i][j].second;
      dp[v] = max(dp[v], tmp[v]);
    }
  }
  for (int i = 0; i < n; i++) ans = max(ans, dp[i]);
  cout << ans;
}
