#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
template <typename T>
bool mmax(T &m, const T q) {
  if (m < q) {
    m = q;
    return true;
  } else
    return false;
}
template <typename T>
bool mmin(T &m, const T q) {
  if (m > q) {
    m = q;
    return true;
  } else
    return false;
}
void __print(long long first) { cerr << first; }
void __print(long first) { cerr << first; }
void __print(unsigned first) { cerr << first; }
void __print(unsigned long first) { cerr << first; }
void __print(unsigned long long first) { cerr << first; }
void __print(float first) { cerr << first; }
void __print(double first) { cerr << first; }
void __print(long double first) { cerr << first; }
void __print(char first) { cerr << '\'' << first << '\''; }
void __print(const char *first) { cerr << '\"' << first << '\"'; }
void __print(const string &first) { cerr << '\"' << first << '\"'; }
void __print(bool first) { cerr << (first ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &first) {
  cerr << '{';
  __print(first.first);
  cerr << ',';
  __print(first.second);
  cerr << '}';
}
template <typename T>
void __print(const T &first) {
  long long f = 0;
  cerr << '{';
  for (auto &i : first) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
long long dx[] = {0, 0, 1, -1};
long long dy[] = {1, -1, 0, 0};
const long long N = 301010;
vector<pair<long long, long long> > w[N];
long long dp[N], t[N];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  for (long long i = 0; i < m; i++) {
    long long u, v, wt;
    cin >> u >> v >> wt;
    w[wt].push_back({u, v});
  }
  for (long long i = 0; i < N; i++) {
    for (auto j : w[i]) {
      long long u = j.first, v = j.second;
      t[v] = 0;
    }
    for (auto j : w[i]) {
      long long u = j.first, v = j.second;
      t[v] = max(dp[u] + 1, t[v]);
    }
    for (auto j : w[i]) {
      long long u = j.first, v = j.second;
      dp[v] = max(dp[v], t[v]);
    }
  }
  long long ans = 0;
  for (long long i = 1; i <= n; i++) ans = max(ans, dp[i]);
  cout << ans;
  return 0;
}
