#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 6;
set<int> lenlist[maxn];
map<int, int> dp[maxn];
struct node {
  int u, v, len;
};
bool cmp(node& a, node& b) { return a.len > b.len; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int n, m;
  cin >> n >> m;
  vector<node> ip(m);
  for (int i = 0; i < m; i++) {
    cin >> ip[i].u >> ip[i].v >> ip[i].len;
    lenlist[ip[i].u].insert(ip[i].len);
  }
  sort(ip.begin(), ip.end(), cmp);
  int ans = 0;
  for (int i = 0; i < m; i++) {
    int val = 1;
    set<int>::iterator it = lenlist[ip[i].v].upper_bound(ip[i].len);
    if (it != lenlist[ip[i].v].end()) {
      val += dp[ip[i].v][*it];
    }
    it = lenlist[ip[i].u].upper_bound(ip[i].len);
    if (it != lenlist[ip[i].u].end()) val = max(val, dp[ip[i].u][*it]);
    dp[ip[i].u][ip[i].len] = max(val, dp[ip[i].u][ip[i].len]);
    ans = max(ans, dp[ip[i].u][ip[i].len]);
  }
  cout << ans;
  return 0;
}
