#include <bits/stdc++.h>
using namespace std;
const long long N = 1000005;
struct item {
  long long a, b, c;
};
long long n, m, u, v, w, d[N];
signed main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (long long i = 1; i <= m; i++) {
    cin >> u >> v >> w;
    d[u] -= w;
    d[v] += w;
  }
  long long pos = 1;
  vector<item> vec;
  for (long long i = 1; i <= n; i++) {
    while (d[i] > 0) {
      while (d[pos] >= 0 && pos <= n) ++pos;
      long long tmp = min(-d[pos], d[i]);
      d[pos] += tmp;
      d[i] -= tmp;
      vec.push_back({pos, i, tmp});
    }
  }
  cout << vec.size() << endl;
  for (item i : vec) cout << i.a << " " << i.b << " " << i.c << endl;
}
