#include <bits/stdc++.h>
using namespace std;
const long long maxn = 100000 + 10;
long long cnt[maxn], n, m;
queue<pair<long long, long long> > loser, winner;
vector<long long> a, b, c;
signed main() {
  cin >> n >> m;
  for (long long i = 0, u, v, d; i < m; ++i) {
    cin >> u >> v >> d;
    cnt[u] -= d;
    cnt[v] += d;
  }
  long long ans = 0;
  for (long long i = 1; i <= n; ++i) {
    if (cnt[i] > 0)
      winner.push({i, cnt[i]});
    else if (cnt[i] < 0)
      loser.push({i, -cnt[i]});
  }
  while ((!winner.empty()) && (!loser.empty())) {
    long long t = min(loser.front().second, winner.front().second);
    a.push_back(loser.front().first);
    b.push_back(winner.front().first);
    c.push_back(t);
    pair<long long, long long> temp = loser.front();
    loser.pop();
    if (temp.second > t) loser.push({temp.first, temp.second - t});
    temp = winner.front();
    winner.pop();
    if (temp.second > t) winner.push({temp.first, temp.second - t});
  }
  cout << a.size() << "\n";
  for (long long i = 0; i < a.size(); ++i) {
    cout << a[i] << " " << b[i] << " " << c[i] << "\n";
  }
  return 0;
}
