#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 20;
int n, m;
long long bal[MAX];
int main(void) {
  scanf("%d%d", &n, &m);
  int u, v;
  long long w;
  while (m--) {
    scanf("%d%d%lld", &u, &v, &w);
    bal[u] += w, bal[v] -= w;
  }
  vector<int> out;
  queue<int> in;
  for (register int i = (1); i <= (n); ++i)
    if (bal[i] > 0)
      out.push_back(i);
    else if (bal[i] < 0)
      in.push(i);
  int sum = 0;
  string ans;
  char buf[100];
  for (int u : out) {
    while (!in.empty() && bal[u]) {
      int v = in.front();
      long long t = min(bal[u], abs(bal[v]));
      bal[u] -= t, bal[v] += t;
      if (!bal[v]) in.pop();
      sprintf(buf, "%d %d %lld\n", u, v, t);
      ans += buf;
      ++sum;
    }
  }
  printf("%d\n", sum);
  cout << ans;
  return 0;
}
