#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
long long diff[N];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) diff[i] = 0;
  for (int i = 1; i <= m; i++) {
    int u, v, d;
    cin >> u >> v >> d;
    diff[u] += d;
    diff[v] -= d;
  }
  long long res = 0;
  queue<int> g, b;
  for (int i = 1; i <= n; i++) {
    if (diff[i] > 0) {
      res += diff[i];
      g.push(i);
    } else if (diff[i] < 0) {
      b.push(i);
    }
  }
  vector<pair<int, pair<int, long long> > > ans;
  while (!g.empty() && !b.empty()) {
    int u = g.front();
    int v = b.front();
    long long m = min(diff[u], -diff[v]);
    ans.push_back(make_pair(u, make_pair(v, m)));
    diff[u] -= m;
    diff[v] += m;
    if (diff[u] == 0) g.pop();
    if (diff[v] == 0) b.pop();
  }
  cout << ans.size() << endl;
  for (auto it = ans.begin(); it != ans.end(); it++) {
    cout << it->first << " " << it->second.first << " " << it->second.second
         << endl;
  }
  return 0;
}
