#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 100;
pair<pair<int, int>, pair<int, int> > maxT[N];
int dp[N];
pair<int, pair<int, int> > e[N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  int m;
  int l = 0;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> e[i].second.first >> e[i].second.second >> e[i].first;
  }
  sort(e, e + m);
  int ans = 0;
  for (int i = 0; i < m; i++) {
    int v = e[i].second.first;
    int u = e[i].second.second;
    int w = e[i].first;
    if (maxT[v].first.second == w) {
      dp[i] = maxT[v].second.first + 1;
    } else {
      dp[i] = maxT[v].first.first + 1;
    }
    if (dp[i] > maxT[u].first.first) {
      if (w == maxT[u].first.second) {
        maxT[u].first.first = dp[i];
      } else {
        maxT[u].second.first = maxT[u].first.first;
        maxT[u].second.second = maxT[u].first.second;
        maxT[u].first.first = dp[i];
        maxT[u].first.second = w;
      }
    }
    ans = max(ans, dp[i]);
  }
  cout << ans;
}
