#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
long long mul(long long a, long long b) { return (a * b) % (1000000007); }
long long add(long long a, long long b) { return (a + b) % (1000000007); }
long long sub(long long a, long long b) {
  return ((a - b) % (1000000007) + (1000000007)) % (1000000007);
}
void upd(long long &a, long long b) {
  a = (a % (1000000007) + b % (1000000007)) % (1000000007);
}
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
set<pair<int, int> > S[(100000 + 10)];
set<pair<int, int> >::iterator it, it2;
int n, m;
int main() {
  cin >> n >> m;
  int ans = 0;
  for (int i = 1; i <= m; i++) {
    int u = read(), v = read(), w = read();
    it = S[u].upper_bound(make_pair(w, -1));
    int value = 1;
    if (it != S[u].begin()) {
      --it;
      pair<int, int> pa = *it;
      int pc = pa.first, pv = pa.second;
      if (pa.first < w) value = max(value, pv + 1);
    }
    ans = max(ans, value);
    ;
    pair<int, int> now = make_pair(w, value);
    it = S[v].upper_bound(now);
    if (it != S[v].end()) {
      if ((it->first) == now.first && (it->second) > now.second) {
        continue;
      }
    }
    while (1) {
      it = S[v].upper_bound(now);
      if (it == S[v].end()) break;
      if ((it->second) <= now.second)
        S[v].erase(it);
      else
        break;
    }
    it = S[v].upper_bound(now);
    if (it != S[v].begin()) {
      it--;
      if ((it->first) < now.first && (it->second) > now.second) continue;
    }
    while (1) {
      it = S[v].upper_bound(now);
      if (it == S[v].begin()) break;
      it--;
      if ((it->first) == now.first && (it->second) < now.second)
        S[v].erase(it);
      else
        break;
    }
    S[v].insert(now);
  }
  cout << ans;
  return 0;
}
