#include <bits/stdc++.h>
using namespace std;
long long t[300005];
long long tot = 0, p = 1;
struct node {
  long long x, y, wi;
} ans[300005 * 2];
long long find(long long xx) {
  for (long long i = p; t[xx] > 0; i++) {
    if (t[i] < 0) {
      tot++;
      if (t[xx] + t[i] >= 0) {
        ans[tot].x = i, ans[tot].y = xx, ans[tot].wi = -t[i];
        t[xx] += t[i];
        t[i] = 0;
      } else {
        ans[tot].x = i, ans[tot].y = xx, ans[tot].wi = t[xx];
        t[i] += t[xx];
        t[xx] = 0;
      }
    }
    p = i;
  }
}
int main() {
  long long n, m;
  scanf("%lld%lld", &n, &m);
  for (long long i = 1; i <= m; i++) {
    long long a, b, w;
    scanf("%lld%lld%lld", &a, &b, &w);
    t[a] -= w, t[b] += w;
  }
  for (long long i = 1; i <= n; i++)
    if (t[i] > 0) find(i);
  printf("%lld\n", tot);
  for (long long i = 1; i <= tot; i++)
    printf("%lld %lld %lld\n", ans[i].x, ans[i].y, ans[i].wi);
  return 0;
}
