#include <bits/stdc++.h>
const long long mod = 1000000000 + 7;
long long pow_mod(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
using namespace std;
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<long long> a(n + 1);
  for (int i = 0, u, v, d; i < m; i++) {
    cin >> u >> v >> d;
    a[u] += d;
    a[v] -= d;
  }
  queue<pair<int, long long>> q, q2;
  for (int i = 1; i <= n; i++) {
    if (a[i] > 0) q.emplace(i, a[i]);
    if (a[i] < 0) q2.emplace(i, -a[i]);
  }
  vector<tuple<int, int, long long>> res;
  while (!q.empty() && !q2.empty()) {
    auto [a, b] = q.front();
    q.pop();
    auto [c, d] = q2.front();
    q2.pop();
    if (b > d) {
      res.emplace_back(a, c, d);
      q.emplace(a, b - d);
    } else if (b == d) {
      res.emplace_back(a, c, b);
    } else {
      res.emplace_back(a, c, b);
      q2.emplace(c, d - b);
    }
  }
  cout << res.size() << endl;
  for (auto& [a, b, c] : res) {
    cout << a << " " << b << " " << c << endl;
  }
  return 0;
}
