#include <bits/stdc++.h>
using namespace std;
const int mxn = 3e5 + 5;
int n, m;
int dp[mxn], tmp[mxn];
vector<pair<int, pair<int, int> > > edge;
int main() {
  ios::sync_with_stdio(false);
  int w, x, y;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> x >> y >> w;
    edge.push_back(make_pair(w, make_pair(x, y)));
  }
  sort(edge.begin(), edge.end());
  for (int i = 0; i < edge.size(); i++) {
    int j = i + 1;
    while (edge[i].first == edge[j].first) {
      tmp[edge[j].second.second] = 0;
      j++;
    }
    j--;
    int d = i;
    while (d <= j) {
      tmp[edge[d].second.second] =
          max(tmp[edge[d].second.second], dp[edge[d].second.first] + 1);
      d++;
    }
    while (i <= j) {
      dp[edge[i].second.second] =
          max(dp[edge[i].second.second], tmp[edge[i].second.second]);
      i++;
    }
    i--;
  }
  int ans = -1;
  for (int i = 1; i < mxn; i++) ans = max(ans, dp[i]);
  cout << ans;
  return 0;
}
