#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<long long> bal(n);
  for (int i = 0; i < m; i++) {
    int u, v, d;
    cin >> u >> v >> d;
    u--;
    v--;
    bal[u] += d;
    bal[v] -= d;
  }
  long long sum = 0;
  vector<pair<long long, int>> pos, neg;
  for (int i = 0; i < n; i++) {
    sum += abs(bal[i]);
    if (bal[i] > 0) {
      pos.emplace_back(bal[i], i);
    } else if (bal[i] < 0) {
      neg.emplace_back(-bal[i], i);
    }
  }
  vector<tuple<int, int, long long>> ans;
  while (!pos.empty() && !neg.empty()) {
    long long x = min(pos.back().first, neg.back().first);
    ans.emplace_back(pos.back().second, neg.back().second, x);
    pos.back().first -= x;
    if (pos.back().first == 0) {
      pos.pop_back();
    }
    neg.back().first -= x;
    if (neg.back().first == 0) {
      neg.pop_back();
    }
  }
  cout << ans.size() << '\n';
  for (auto t : ans) {
    cout << get<0>(t) + 1 << ' ' << get<1>(t) + 1 << ' ' << get<2>(t) << '\n';
  }
  return 0;
}
