#include <bits/stdc++.h>
using namespace std;
int n, m;
long long v[100100];
vector<int> pos, neg;
vector<int> X, Y;
vector<long long> V;
int main() {
  scanf("%d %d", &n, &m);
  int x, y;
  long long z;
  memset(v, 0, sizeof(v));
  for (int i = 0; i < m; i++) {
    scanf("%d %d %I64d", &x, &y, &z);
    v[x] += z;
    v[y] -= z;
  }
  for (int i = 1; i <= n; i++) {
    if (v[i] > 0) pos.push_back(i);
    if (v[i] < 0) neg.push_back(i);
  }
  int i = 0, j = 0;
  while (i < pos.size() && j < neg.size()) {
    long long tem = v[pos[i]] > -v[neg[j]] ? -v[neg[j]] : v[pos[i]];
    v[pos[i]] -= tem;
    v[neg[j]] += tem;
    X.push_back(pos[i]);
    Y.push_back(neg[j]);
    V.push_back(tem);
    if (v[pos[i]] <= 0) i++;
    if (v[neg[j]] >= 0) j++;
  }
  printf("%d\n", X.size());
  for (int i = 0; i < X.size(); i++) {
    cout << X[i] << " " << Y[i] << " " << V[i] << endl;
  }
}
