#include <bits/stdc++.h>
using namespace std;
int dp[300005];
int dp2[300005];
int val[300005];
int val2[300005];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int n, m;
  cin >> n >> m;
  pair<int, pair<int, int> > p[m];
  for (int i = 0; i < m; i++) {
    int x, y, w;
    cin >> x >> y >> w;
    p[i].first = w;
    p[i].second.first = x;
    p[i].second.second = y;
  }
  sort(p, p + m);
  int ans = 0;
  int prev = 0;
  for (int i = 0; i < m; i++) {
    int x, y, w;
    w = p[i].first;
    x = p[i].second.first;
    y = p[i].second.second;
    if (val[x] < w) {
      if (dp[y] < dp[x] + 1) {
        if (val[y] < w) {
          dp2[y] = dp[y];
          val2[y] = val[y];
        }
        dp[y] = dp[x] + 1;
        val[y] = w;
      }
    }
    if (val2[x] < w && dp[y] < dp2[x] + 1) {
      if (val[y] < w) {
        dp2[y] = dp[y];
        val2[y] = val[y];
      }
      dp[y] = dp2[x] + 1;
      val[y] = w;
    }
    ans = max(ans, dp[y]);
  }
  cout << ans;
}
