#include <bits/stdc++.h>
using namespace std;
int dp[300010];
int d[300010];
vector<pair<int, int> > a[100010];
int n, m;
int main() {
  cin >> n >> m;
  int u, v, c;
  for (int i = 1; i <= m; i++) {
    cin >> u >> v >> c;
    a[c].push_back(make_pair(u, v));
  }
  for (int i = 1; i <= 100000; i++) {
    for (int j = 0; j < a[i].size(); j++) {
      u = a[i][j].first;
      v = a[i][j].second;
      d[v] = max(d[v], dp[u] + 1);
    }
    for (int j = 0; j < a[i].size(); j++) {
      v = a[i][j].second;
      dp[v] = d[v];
    }
  }
  int res = 0;
  for (int i = 1; i <= n; i++) res = max(res, d[i]);
  cout << res;
  return 0;
}
