#include <bits/stdc++.h>
using namespace std;
const long long inff = 0x3f3f3f3f3f3f3f3f;
int n, m, dp[300008], ans, b[300008], qw, zz;
struct as {
  int x, y, z;
} a[300008];
bool cmp(as a, as b) { return a.z > b.z; }
int main() {
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i(1); i <= (m); ++i)
    scanf("%d", &a[i].x), scanf("%d", &a[i].y), scanf("%d", &a[i].z);
  sort(a + 1, a + m + 1, cmp);
  for (int i(1); i <= (m); ++i) {
    if (a[i].z != qw) {
      for (int j(zz); j <= (i - 1); ++j) dp[a[j].x] = b[a[j].x];
      qw = a[i].z;
      zz = i;
    }
    b[a[i].x] = max(dp[a[i].y] + 1, b[a[i].x]);
  }
  for (int j(zz); j <= (m); ++j) dp[a[j].x] = b[a[j].x];
  for (int i(1); i <= (n); ++i) ans = max(ans, dp[i]);
  cout << ans << endl;
  return 0;
}
