#include <bits/stdc++.h>
using namespace std;
class Timer {
  clock_t start;

 public:
  Timer() : start(clock()) {}
  double now() { return (double)(clock() - start) / CLOCKS_PER_SEC; }
} timer;
struct node {
  int u, v, l;
  friend bool operator<(const node& a, const node& b) { return a.l < b.l; }
} in[300005];
int dp[300005];
queue<pair<int, int> > qe;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int u, v, l;
    scanf("%d%d%d", &in[i].u, &in[i].v, &in[i].l);
  }
  sort(in + 1, in + 1 + m);
  for (int i = 1; i <= m;) {
    int l = in[i].l;
    while (i <= m && in[i].l == l) {
      qe.push(make_pair(in[i].v, dp[in[i].u] + 1));
      i++;
    }
    while (!qe.empty()) {
      dp[qe.front().first] = max(dp[qe.front().first], qe.front().second);
      qe.pop();
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
  return 0;
}
