#include <bits/stdc++.h>
using namespace std;
const long long N = 6e5 + 5;
long long a[N], b[N];
long long n, m, ge[N << 1], cnt1, cnt2, ru[N], ans;
struct pigu {
  long long co, dao, quan;
} c[N << 1];
inline long long read() {
  char c = getchar();
  long long fu = 1, x = 0;
  while (!isdigit(c)) {
    if (c == '-') fu = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  return fu * x;
}
signed main() {
  n = read();
  m = read();
  for (long long i = 1, x, y, z; i <= m; i++) {
    x = read();
    y = read();
    z = read();
    ru[x] += z;
    ru[y] -= z;
  }
  for (long long i = 1; i <= n; i++) {
    if (ru[i] > 0) a[++cnt1] = i;
    if (ru[i] < 0) b[++cnt2] = i;
  }
  long long len = cnt1, dao = 1;
  for (long long i = 1; i <= len; i++) {
    if (dao <= cnt2 && ru[b[dao]] == 0) dao++;
    while (ru[a[i]] > (-ru[b[dao]])) {
      c[++ans] = {a[i], b[dao], -ru[b[dao]]};
      ru[a[i]] += ru[b[dao]];
      ru[b[dao]] = 0;
      dao++;
    }
    if (ru[a[i]] != 0) {
      c[++ans] = {a[i], b[dao], ru[a[i]]};
      ru[b[dao]] += ru[a[i]];
      ru[a[i]] = 0;
    }
  }
  cout << ans << "\n";
  for (long long i = 1; i <= ans; i++)
    cout << c[i].co << " " << c[i].dao << " " << c[i].quan << "\n";
}
