#include <bits/stdc++.h>
using namespace std;
int d[400000];
int main() {
  int i, j, k, n, m, ans, x, y, w;
  vector<pair<int, pair<int, int> > > a;
  vector<pair<int, int> > u;
  scanf("%d%d", &n, &m);
  a.clear();
  for (i = 0; i < m; i++) {
    scanf("%d%d%d", &x, &y, &w);
    x--;
    y--;
    a.push_back(make_pair(w, make_pair(x, y)));
  }
  sort(a.begin(), a.end());
  memset(d, 0, sizeof(d));
  for (i = 0; i < a.size(); i++) {
    for (j = i; j < a.size(); j++)
      if (a[j].first != a[i].first) break;
    j--;
    u.clear();
    for (k = i; k <= j; k++) {
      u.push_back(make_pair(a[k].second.second, d[a[k].second.first] + 1));
    }
    for (k = 0; k < u.size(); k++)
      if (d[u[k].first] < u[k].second) d[u[k].first] = u[k].second;
    i = j;
  }
  ans = 0;
  for (i = 0; i < n; i++)
    if (d[i] > ans) ans = d[i];
  printf("%d\n", ans);
  return 0;
}
