#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const int mod = (int)1e+9 + 7;
const double pi = acos(-1.0);
vector<pair<int, pair<int, int> > > rb;
int bo[300300];
vector<pair<int, int> > vc;
int main() {
  int n, m, i;
  scanf("%d%d", &n, &m);
  for (i = 0; i < m; i++) {
    int u, v, c;
    scanf("%d%d%d", &u, &v, &c);
    rb.push_back(make_pair(c, make_pair(u, v)));
  }
  sort(rb.begin(), rb.end());
  int otv = 0;
  for (i = 0; i < m; i++) {
    int u = rb[i].second.first, v = rb[i].second.second, c = rb[i].first;
    if (i > 0 && rb[i - 1].first < c)
      while (!vc.empty()) {
        int u = vc.back().first, o = vc.back().second;
        bo[u] = max(bo[u], o);
        vc.pop_back();
        otv = max(otv, bo[u]);
      }
    vc.push_back(make_pair(v, bo[u] + 1));
  }
  while (!vc.empty()) {
    int u = vc.back().first, o = vc.back().second;
    bo[u] = max(bo[u], o);
    vc.pop_back();
    otv = max(otv, bo[u]);
  }
  printf("%d", otv);
  return (0);
}
