#include <bits/stdc++.h>
using namespace std;
const int MAX = 300005;
vector<pair<int, int> > w[MAX];
int dp[MAX], tmp[MAX];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int x, y, z;
    scanf("%d %d %d", &x, &y, &z);
    w[z].push_back(make_pair(x, y));
  }
  for (int i = 1; i < MAX; i++) {
    for (int j = 0; j < w[i].size(); j++) {
      int x = w[i][j].first, y = w[i][j].second;
      tmp[y] = max(tmp[y], dp[x] + 1);
    }
    for (int j = 0; j < w[i].size(); j++) {
      int x = w[i][j].first, y = w[i][j].second;
      dp[y] = max(dp[y], tmp[y]);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) ans = max(ans, dp[i]);
  printf("%d\n", ans);
  return 0;
}
