#include <bits/stdc++.h>
using namespace std;
const int N = 300300;
int t, n, m, h[2][N], *p = h[0], *q = h[1];
map<int, vector<pair<int, int> > > g;
int main() {
  cin >> n >> m;
  for (int u, v, w; m-- > 0 && cin >> u >> v >> w;)
    g[w].push_back(make_pair(u, v));
  for (map<int, vector<pair<int, int> > >::iterator i = g.begin(); i != g.end();
       ++i) {
    for (vector<pair<int, int> >::iterator j = i->second.begin();
         j != i->second.end(); ++j)
      q[j->first] = max(q[j->first], p[j->first]);
    for (vector<pair<int, int> >::iterator j = i->second.begin();
         j != i->second.end(); ++j) {
      int u = j->first, v = j->second;
      t = max(t, p[v] = max(p[v], q[u] + 1));
    }
  }
  cout << t << endl;
}
