#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int i, n, m, u, v, d, sum = 0;
  cin >> n >> m;
  long long int hashi[n + 1];
  memset(hashi, 0, sizeof(hashi));
  for (i = 0; i < m; i++) {
    cin >> u >> v >> d;
    hashi[u] -= d;
    hashi[v] += d;
  }
  vector<long long int> neg, pos;
  for (i = 1; i <= n; i++) {
    if (hashi[i] < 0) {
      neg.push_back(i);
    } else if (hashi[i] > 0) {
      pos.push_back(i);
      sum += hashi[i];
    }
  }
  long long int np = 0, pp = 0;
  vector<pair<long long int, pair<long long int, long long int> > > g;
  while (sum) {
    long long int mini = min(abs(hashi[neg[np]]), hashi[pos[pp]]);
    g.push_back({neg[np], {pos[pp], mini}});
    hashi[neg[np]] += mini;
    hashi[pos[pp]] -= mini;
    sum -= mini;
    if (hashi[pos[pp]] == 0) {
      pp++;
    }
    if (hashi[neg[np]] == 0) {
      np++;
    }
  }
  cout << g.size() << endl;
  for (auto p : g) {
    cout << p.first << " " << p.second.first << " " << p.second.second << endl;
  }
  return 0;
}
