#include <bits/stdc++.h>
using namespace std;
const long double PI = 3.1415926535897932384626;
long long MOD = 1000000007;
const long double EPS = 1e-9;
const int N = 3e5 + 7;
inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
inline unsigned int setBits(unsigned int v) {
  unsigned int c;
  for (c = 0; v; ++c) v &= (v - 1);
  return c;
}
inline bool isPowerOfTwo(unsigned int v) { return (v && !(v & (v - 1))); }
bool isPalin(const string& s) {
  int i, j;
  for (i = 0, j = s.length() - 1; i <= j; ++i, --j)
    if (s[i] != s[j]) return false;
  return true;
}
long long power(long long base, unsigned long long exp, long long mod = MOD) {
  long long res = 1;
  base %= mod;
  while (exp) {
    if (exp & 1) res = (res * base) % mod;
    exp >>= 1;
    base = (base * base) % mod;
  }
  return res;
}
long long modInv(long long a, long long mod = MOD) {
  if (mod == 1) return 0;
  long long x = 1, y = 0, m = mod;
  while (a > 1) {
    long long q = a / m, t = m;
    m = a % m;
    a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0) x += mod;
  return x;
}
inline unsigned int iabs(int x) {
  unsigned int y = x;
  return (y > 0) ? y : -y;
}
inline long long add(long long a, long long b, long long mod = MOD) {
  long long res = (a % mod + b % mod) % mod;
  return res;
}
inline long long sub(long long a, long long b, long long mod = MOD) {
  long long res = (a % mod - b % mod + mod) % mod;
  return res;
}
inline long long mult(long long a, long long b, long long mod = MOD) {
  long long res = (a % mod * b % mod) % mod;
  return res;
}
inline long long div(long long a, long long b, long long mod = MOD) {
  long long res = (a % mod * modInv(b, mod)) % mod;
  return res;
}
vector<pair<int, int>> graph[1007];
void openf() {}
pair<int, int> bfsutil(int src, vector<bool>& visited) {
  visited[src] = true;
  queue<int> q;
  q.push(src);
  int mincost = INT_MAX;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    if (graph[u].empty()) {
      return {u, mincost};
    }
    for (int i = 0; i < graph[u].size(); ++i) {
      pair<int, int> p = graph[u][i];
      if (p.first == src) {
        return {p.first, mincost};
      } else if (!visited[p.first]) {
        visited[p.first] = true;
        mincost = min(mincost, p.second);
        q.push(p.first);
      }
    }
  }
  return {-1, -1};
}
void bfs(vector<pair<pair<int, int>, int>>& paths, int n, vector<int>& out,
         vector<int>& in) {
  vector<bool> visited(n + 1);
  for (int i = 1; i <= n; ++i) {
    if (!visited[i] && out[i] == 1 && !in[i]) {
      pair<int, int> sink = bfsutil(i, visited);
      if (i != sink.first) {
        paths.push_back({{i, sink.first}, sink.second});
      }
    }
  }
}
bool compare(const pair<pair<int, int>, int>& a,
             const pair<pair<int, int>, int>& b) {
  return (a.first.first < b.first.first);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  openf();
  int n, p;
  cin >> n >> p;
  vector<int> out(n + 1);
  vector<int> in(n + 1);
  while (p--) {
    int a, b, d;
    cin >> a >> b >> d;
    ++out[a];
    ++in[b];
    graph[a].push_back({b, d});
  }
  vector<pair<pair<int, int>, int>> paths;
  bfs(paths, n, out, in);
  sort(paths.begin(), paths.end(), compare);
  int m = paths.size();
  cout << m << endl;
  for (int i = 0; i < m; ++i) {
    pair<pair<int, int>, int> p = paths[i];
    cout << p.first.first << " " << p.first.second << " " << p.second << endl;
  }
  return 0;
}
