#include <bits/stdc++.h>
using namespace std;
int maxarr(vector<int> &arr);
int minarr(vector<int> &arr);
long long modulo(long long base, long long exponent, long long modulus);
long long choose(long long n, long long k);
bool isprime(long long n);
long long inverse(long long a, long long m);
long long fastmult(long long a, long long b, long long c);
vector<long long> a(1000010);
vector<long long> prime;
long long sum[1000010];
vector<bool> aprime(1000010);
void setprime();
long long divCeil(long long a, long long b);
long long arrceil(vector<long long> a, long long k);
void build() {}
int main() {
  long long n, p;
  cin >> n >> p;
  map<long long, long long> stset;
  map<long long, long long> adj;
  map<pair<long long, long long>, long long> cost;
  for (int i = 0; i < p; i++) {
    long long x, y, z;
    cin >> x >> y >> z;
    adj[x] = y;
    cost[make_pair(x, y)] = z;
    stset[x]++;
    stset[y]--;
  }
  set<long long> startset;
  for (map<long long, long long>::iterator it = stset.begin();
       it != stset.end(); it++) {
    if (it->second == 1) startset.insert(it->first);
  }
  set<long long>::iterator it;
  cout << startset.size() << endl;
  for (it = startset.begin(); it != startset.end(); it++) {
    long long x, y, z = LONG_LONG_MAX;
    x = *it;
    long long temp = x;
    while (adj[temp] != 0) {
      z = min(z, cost[make_pair(temp, adj[temp])]);
      temp = adj[temp];
    }
    y = temp;
    cout << x << " " << y << " " << z << endl;
  }
  build();
  return 0;
}
int maxarr(vector<int> &arr) {
  int maxi = INT_MIN;
  for (int i = 0; i < arr.size(); i++) maxi = max(arr[i], maxi);
  return maxi;
}
int minarr(vector<int> &arr) {
  int mini = INT_MAX;
  for (int i = 0; i < arr.size(); i++) mini = min(arr[i], mini);
  return mini;
}
long long modulo(long long base, long long exponent, long long modulus) {
  if (modulus == 1) return 0;
  long long result = 1;
  base = base % modulus;
  while (exponent > 0) {
    if (exponent % 2 == 1) {
      result = (result * base) % modulus;
    }
    exponent = exponent >> 1;
    base = (base * base) % modulus;
  }
  return result;
}
long long choose(long long n, long long k) {
  if (k == 0) return 1;
  return (n * choose(n - 1, k - 1)) / k;
}
bool isprime(long long n) {
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) return false;
  }
  return true;
}
void EE(long long a, long long b, long long &co1, long long &co2) {
  if (a % b == 0) {
    co1 = 0;
    co2 = 1;
    return;
  }
  EE(b, a % b, co1, co2);
  long long temp = co1;
  co1 = co2;
  co2 = temp - co2 * (a / b);
}
long long inverse(long long a, long long m) {
  long long x, y;
  EE(a, m, x, y);
  if (x < 0) x += m;
  return x;
}
long long fastmult(long long a, long long b, long long c) {
  if (b == 0) return 0;
  long long res = fastmult(a, b >> 1, c);
  res = (res + res) % c;
  if (b % 2 == 1) {
    res = (res + a) % c;
  }
  return res;
}
void setprime() {
  aprime[0] = true;
  aprime[1] = true;
  for (int i = 2; i * i < 1000010; i++) {
    if (aprime[i] == false) {
      for (int j = i * 2; j < 1000010; j += i) {
        aprime[j] = true;
      }
    }
  }
}
long long divCeil(long long a, long long b) { return (a + b - 1) / b; }
long long arrceil(vector<long long> a, long long k) {
  long long n = a.size();
  long long low = 0;
  long long high = n - 1;
  if (a[0] > k) return -1;
  while (low < high) {
    long long mid = (low + high + 1) / 2;
    if (a[mid] <= k)
      low = mid;
    else
      high = mid - 1;
  }
  return low;
}
