#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 1e5 + 5;
pair<ll, int> bal[maxn];
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int m;
  cin >> m;
  for (int i = 1; i <= n; i++) {
    bal[i].second = i;
  }
  while (m--) {
    int u, v, d;
    cin >> u >> v >> d;
    bal[u].first -= d;
    bal[v].first += d;
  }
  sort(bal + 1, bal + n + 1);
  vector<tuple<int, int, ll>> ans;
  int l = 1, r = n;
  while (l < r) {
    if (bal[l].first == 0) {
      l++;
      continue;
    }
    if (bal[r].first == 0) {
      r--;
      continue;
    }
    ll z = min(-bal[l].first, bal[r].first);
    ans.emplace_back(bal[l].second, bal[r].second, z);
    bal[l].first += z;
    bal[r].first -= z;
  }
  cout << ans.size() << '\n';
  for (auto [u, v, d] : ans) {
    cout << u << ' ' << v << ' ' << d << '\n';
  }
  return 0;
}
