#include <bits/stdc++.h>
using namespace std;
long long G[110][110];
long long N;
long long dist[110];
void warshall() {
  for (long long i = 0; i < N; i++) {
    for (long long j = 0; j < N; j++) {
      for (long long k = 0; k < N; k++) {
        G[j][k] = min(G[j][k], G[j][i] + G[i][k]);
      }
    }
  }
}
void print() {
  for (long long i = 0; i < N; i++) {
    for (long long j = 0; j < N; j++) {
      cout << G[i][j] << " ";
    }
    cout << endl;
  }
  cout << endl;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long M;
  cin >> N >> M;
  vector<long long> A(N), B(N), C(N);
  for (long long i = 0; i < N; i++) {
    for (long long j = 0; j < N; j++) {
      G[i][j] = 1000000000;
    }
  }
  for (long long i = 0; i < N; i++) {
    cin >> A[i] >> B[i] >> C[i];
    A[i]--;
    B[i]--;
    G[A[i]][B[i]] = C[i];
  }
  warshall();
  print();
  long long ans = 0;
  for (long long i = 0; i < N; i++) {
    dist[i] = G[0][i];
  }
  for (long long i = 0; i < M; i++) {
    for (long long j = 0; j < M; j++) {
      G[i][j] = 1000000000;
      if (i == j) continue;
      G[A[j]][B[j]] = C[j];
    }
    warshall();
    bool F = true;
    for (long long i = 0; i < N; i++) {
      if (dist[i] < G[0][i] && G[0][i] != 1000000000) F = false;
    }
    if (!F) ans++;
  }
  cout << ans << endl;
  return 0;
}
