#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
struct node {
  long long v;
  int id;
} e[N];
bool operator<(node a, node b) { return a.v < b.v; }
struct query {
  int u, v;
  long long d;
} ans[N * 3];
int n, m, u, v, tot;
long long mon[N], d;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d%lld", &u, &v, &d);
    mon[u] -= d;
    mon[v] += d;
  }
  for (int i = 1; i <= n; ++i) {
    e[i].v = mon[i];
    e[i].id = i;
  }
  sort(e + 1, e + n + 1);
  int l = 1, r = n;
  while (l < r) {
    while (l < r && e[l].v == 0) l++;
    while (l < r && e[r].v == 0) r--;
    if (l >= r) break;
    if (abs(e[l].v) == e[r].v) {
      ans[tot++] = {e[l].id, e[r].id, e[r].v};
      e[l].v = e[r].v = 0;
      l++;
      r--;
    } else if (abs(e[l].v) < e[r].v) {
      ans[tot++] = {e[l].id, e[r].id, abs(e[l].v)};
      e[r].v -= abs(e[l].v);
      l++;
    } else {
      ans[tot++] = {e[l].id, e[r].id, e[r].v};
      e[l].v += e[r].v;
      r--;
    }
  }
  printf("%d\n", tot);
  for (int i = 0; i < tot; ++i)
    printf("%d %d %lld\n", ans[i].u, ans[i].v, ans[i].d);
  return 0;
}
