#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  register long long x = 0, f = 1;
  register char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + (ch ^ 48), ch = getchar();
  return x * f;
}
const int M = 3e5 + 5;
const int N = 1e5 + 5;
int n, m, ct, cnt, num, x[N], y[N], id[N], nu[N];
long long d[N], pr[N];
int main() {
  n = read(), m = read();
  for (int i = 1; i <= m; i++) {
    int x = read(), y = read();
    long long k = read();
    d[x] += k, d[y] -= k;
  }
  for (int i = 1; i <= n; i++) {
    if (d[i] > 0) id[++cnt] = i;
    if (d[i] < 0) nu[++num] = i;
  }
  int l = 1, r = 1;
  while (l <= cnt && r <= num) {
    long long k = min(d[id[l]], -d[nu[r]]);
    if (k == d[id[l]])
      x[++ct] = id[l++], y[ct] = nu[r], pr[ct] = k, d[nu[r]] += k;
    else
      x[++ct] = id[l], y[ct] = nu[r++], pr[ct] = k, d[id[l]] -= k;
    if (!d[id[l]]) ++l;
    if (!d[nu[r]]) ++r;
  }
  printf("%d\n", ct);
  while (ct) printf("%d %d %lld\n", x[ct], y[ct], pr[ct]), ct--;
}
