#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m, a, b, c, cnt = 0;
  cin >> n >> m;
  int adj[n + 1][n + 1];
  for (int i = 0; i < n + 1; i++)
    for (int j = 0; j < n + 1; j++) adj[i][j] = 10000;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    adj[a][b] = c;
    adj[b][a] = c;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      for (int k = 1; k <= n; k++)
        if (adj[j][i] != 10000 && adj[i][k] != 10000 &&
            (adj[j][k] != 10000 && adj[j][i] + adj[i][k] < adj[j][k])) {
          adj[j][k] = adj[k][j] = adj[j][i] + adj[i][k];
          cnt++;
          cout << i << ' ' << j << ' ' << k << '\n';
        }
  cout << cnt << '\n';
  return 0;
}
