#include <bits/stdc++.h>
using namespace std;
long long dibe[2000005], pabe[2000005];
vector<long long> sh, sr;
struct nd {
  long long u, v, w;
};
vector<nd> ans;
int main() {
  long long i, j, k, l, m, n, t, x, y, u, v, w;
  nd baal;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> u >> v >> w;
    dibe[u] += w;
    pabe[v] += w;
  }
  for (i = 1; i <= n; i++) {
    x = min(dibe[i], pabe[i]);
    dibe[i] -= x;
    pabe[i] -= x;
  }
  for (i = 1; i <= n; i++) {
    if (dibe[i] > 0)
      sh.push_back(i);
    else
      sr.push_back(i);
  }
  while (sh.size() && sr.size()) {
    x = min(dibe[sh.back()], pabe[sr.back()]);
    dibe[sh.back()] -= x;
    pabe[sr.back()] -= x;
    baal.u = sh.back();
    baal.v = sr.back();
    baal.w = x;
    if (x) ans.push_back(baal);
    if (!dibe[sh.back()]) sh.pop_back();
    if (!pabe[sr.back()]) sr.pop_back();
  }
  cout << ans.size() << endl;
  for (i = 0; i < ans.size(); i++) {
    cout << ans[i].u << ' ' << ans[i].v << ' ' << ans[i].w << endl;
  }
  return 0;
}
