#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<long long> f(n);
  int u, v;
  long long z;
  while (m--) {
    cin >> u >> v >> z;
    f[--u] -= z;
    f[--v] += z;
  }
  vector<long long> a, b;
  for (int i = 0; i < n; i++)
    if (f[i] > 0)
      a.push_back(i);
    else if (f[i] < 0)
      b.push_back(i);
  vector<vector<long long> > ans;
  int id = 0;
  for (int i = 0; i < a.size(); i++) {
    while (id < b.size() && f[a[i]] > 0) {
      long long x = min(f[a[i]], -f[b[id]]);
      f[a[i]] -= x;
      f[b[id]] += x;
      ans.push_back({b[id], a[i], x});
      if (f[b[id]] == 0) id++;
    }
  }
  cout << ans.size() << "\n";
  for (auto x : ans) cout << x[0] + 1 << " " << x[1] + 1 << " " << x[2] << "\n";
}
