#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int SIZE = 1e6 + 10;
struct EDGE {
  int x, y, v;
  EDGE(int _x = 0, int _y = 0, int _v = 0) : x(_x), y(_y), v(_v) {}
  bool operator<(const EDGE& b) const { return v < b.v; }
} a[SIZE];
int dp[SIZE];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < (m); ++i) {
    scanf("%d%d%d", &(a[i].x), &(a[i].y), &(a[i].v));
  }
  sort(a, a + m);
  for (int i = 0; i < (m); ++i) {
    int j;
    vector<pair<int, int> > pp;
    for (j = i; j < m && a[j].v == a[i].v; j++) {
      pp.push_back(make_pair(dp[a[j].x] + 1, a[j].y));
    }
    for (int k = 0; k < (((int)(pp).size())); ++k) {
      dp[pp[k].second] = max(dp[pp[k].second], pp[k].first);
    }
    i = j - 1;
  }
  int an = 0;
  for (int i = 0; i < (n); ++i) an = max(an, dp[i + 1]);
  printf("%d\n", an);
  return 0;
}
