#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1000000007;
const long long int INF = LONG_MAX;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long int n, m;
  cin >> n >> m;
  vector<pair<long long int, pair<long long int, long long int> > > edge(m);
  for (long long int i = 0; i < (m); ++i) {
    long long int a, b, c;
    cin >> a >> b >> c;
    edge[i] = make_pair(c, make_pair(a - 1, b - 1));
  }
  sort((edge).begin(), (edge).end());
  vector<long long int> dp(n, 0);
  long long int i = 0;
  vector<long long int> tdp(n, -1);
  while (i < m) {
    long long int j = i - 1;
    while (j < m - 1 && edge[j + 1].first == edge[i].first) {
      ++j;
      tdp[edge[j].second.first] =
          max(tdp[edge[j].second.first], (dp[edge[j].second.first]));
      tdp[edge[j].second.second] =
          max(tdp[edge[j].second.second], (dp[edge[j].second.second]));
    }
    for (long long int k = (i); k < (j + 1); ++k) {
      dp[edge[k].second.second] =
          max(dp[edge[k].second.second], (tdp[edge[k].second.first] + 1));
    }
    i = j + 1;
  }
  cout << (*max_element((dp).begin(), (dp).end())) << endl;
  ;
}
