#include <bits/stdc++.h>
using namespace std;
const int N = 1000 * 100 * 3 + 10;
pair<int, pair<int, int> > a[N];
int dp[N], alaki[N];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++)
    cin >> a[i].second.first >> a[i].second.second >> a[i].first;
  sort(a, a + m);
  int i = 0, j;
  for (; i < m;) {
    j = i;
    while (a[i].first == a[j].first && j < m) {
      alaki[a[j].second.second] =
          max(dp[a[j].second.first] + 1, alaki[a[j].second.second]);
      j++;
    }
    for (; i < j; i++)
      dp[a[i].second.second] =
          max(dp[a[i].second.second], alaki[a[i].second.second]);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) ans = max(dp[i], ans);
  cout << ans;
  return 0;
}
