#include <bits/stdc++.h>
using namespace std;
typedef vector<vector<pair<int, int> > > vvi;
const int MAX = 1000000 + 5;
int main() {
  int n, p;
  cin >> n >> p;
  int indeg[1005] = {0}, outdeg[1005] = {0};
  vvi adj(n, vector<pair<int, int> >());
  for (int i = 0; i < p; ++i) {
    int a, b, d;
    cin >> a >> b >> d;
    --a;
    --b;
    adj[a].push_back(pair<int, int>(b, d));
    outdeg[a] = 1;
    indeg[b] = 1;
  }
  vector<pair<pair<int, int>, int> > ans;
  for (int i = 0; i < n; ++i)
    if (outdeg[i] && indeg[i] == 0) {
      int water = MAX, curr = i, next = i;
      while (outdeg[curr]) {
        next = adj[curr][0].first;
        water = min(water, adj[curr][0].second);
        curr = next;
      }
      ans.push_back(make_pair(pair<int, int>(i, curr), water));
    }
  cout << ans.size() << endl;
  for (size_t i = 0; i < ans.size(); ++i)
    cout << ans[i].first.first + 1 << ' ' << ans[i].first.second + 1 << ' '
         << ans[i].second << endl;
}
