#include <bits/stdc++.h>
using namespace std;
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline int read() {
  int x = 0;
  char ch = getchar();
  bool positive = 1;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') positive = 0;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline void write(int a) {
  if (a >= 10) write(a / 10);
  putchar('0' + a % 10);
}
inline void writeln(int a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  write(a);
  puts("");
}
const int N = 100005;
int ans, n, m;
map<int, int> dp[N];
map<int, int>::iterator it;
int main() {
  n = read();
  m = read();
  for (int i = 1; i <= m; i++) {
    int u = read(), v = read(), w = read(), t;
    it = dp[u].lower_bound(w);
    if (it == dp[u].begin()) {
      t = 1;
    } else {
      it--;
      t = it->second + 1;
    }
    ans = max(ans, t);
    if (dp[v].count(w)) {
      if (t > dp[v][w])
        dp[v][w] = t;
      else
        continue;
    } else
      dp[v][w] = t;
    it = dp[v].lower_bound(w);
    if (it != dp[v].begin()) {
      it--;
      if (it->second >= t) {
        dp[v].erase(w);
        continue;
      }
    }
    it = dp[v].upper_bound(w);
    while (1) {
      if (it == dp[v].end() || it->second > t) break;
      dp[v].erase(it++);
    }
  }
  cout << ans << endl;
}
