#include <bits/stdc++.h>
using namespace std;
int abs(int x) {
  if (x < 0) return -x;
  return x;
}
int addmod(int v1, int v2) {
  int v3 = v1 + v2;
  if (v3 >= 1000000007) v3 -= 1000000007;
  return v3;
}
int cc[100];
int dp[400006];
int endat[400006];
vector<pair<int, int> > v[1000005];
int main() {
  int n, m;
  scanf("%d", &n), scanf("%d", &m);
  int x, y, z;
  for (int i = (0); i < (m); i++) {
    scanf("%d", &x), scanf("%d", &y), scanf("%d", &z);
    x--, y--;
    v[z].push_back(pair<int, int>(x, y));
  }
  int ans = 1;
  for (int i = (0); i < (100005); i++) {
    set<pair<int, int> > ss;
    set<pair<int, int> >::iterator it;
    for (int j = (0); j < (v[i].size()); j++) {
      ss.insert(pair<int, int>(v[i][j].second, dp[v[i][j].first] + 1));
    }
    for ((it) = (ss).begin(); (it) != (ss).end(); (it)++) {
      dp[it->first] = max(dp[it->first], it->second);
    }
  }
  for (int i = (0); i < (n); i++) ans = max(ans, dp[i]);
  cout << (ans), cout << (endl);
  return 0;
}
