#include <bits/stdc++.h>
using namespace std;
inline int read() {
  char ch;
  bool f = false;
  int res = 0;
  while (((ch = getchar()) < '0' || ch > '9') && ch != '-')
    ;
  if (ch == '-')
    f = true;
  else
    res = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    res = (res << 3) + (res << 1) + ch - '0';
  return f ? ~res + 1 : res;
}
map<int, int> mp[100009];
map<int, int>::iterator z, jt;
int main() {
  int n, m, a, b, w, c, ans = 0;
  n = read(), m = read();
  for (int i = 1; i <= m; ++i) {
    a = read(), b = read(), w = read();
    z = mp[a].lower_bound(w);
    if (z == mp[a].begin())
      a = 1;
    else
      a = 1 + (--z)->second;
    z = mp[b].upper_bound(w);
    if (z == mp[b].begin())
      c = 0;
    else
      c = (--z)->second;
    if (c < a) {
      mp[b][w] = max(mp[b][w], a);
      ans = max(ans, mp[b][w]), z = mp[b].upper_bound(w);
      while (!(z == mp[b].end() || z->second > a)) jt = z++, mp[b].erase(jt);
    }
  }
  printf("%d\n", ans);
  return 0;
}
