#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
const ll MAXN = 1e5 + 100;
ll c[MAXN];
bool cmp1(ll a, ll b) { return c[a] > c[b]; }
bool cmp2(ll a, ll b) { return c[a] < c[b]; }
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0);
  ll n, m;
  ll tot = 0, rem = 0;
  cin >> n >> m;
  vector<ll> ins, outs;
  for (ll i = 0; i < m; i++) {
    ll u, v, d;
    cin >> u >> v >> d;
    c[u] -= d;
    c[v] += d;
  }
  for (ll i = 1; i <= n; i++) {
    if (c[i] > 0)
      ins.push_back(i);
    else if (c[i] < 0)
      outs.push_back(i);
    c[i] = abs(c[i]);
  }
  sort(ins.begin(), ins.end(), cmp1);
  sort(outs.begin(), outs.end(), cmp1);
  auto it = ins.begin(), it2 = outs.begin();
  vector<pair<pii, ll>> v;
  while (it != ins.end() && it2 != outs.end()) {
    ll s = min(c[*it], c[*it2]);
    c[*it] -= s;
    c[*it2] -= s;
    v.push_back({{*it2, *it}, s});
    while (it != ins.end() && c[*it] == 0) it++;
    while (it2 != outs.end() && c[*it2] == 0) it2++;
  }
  cout << v.size() << endl;
  for (auto first : v) {
    cout << first.first.first << " " << first.first.second << " "
         << first.second << "\n";
  }
}
