#include <bits/stdc++.h>
using namespace std;
const int n = 1e5 + 5;
int N, M, K;
long long D[n];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M;
  for (int i = 0; i < M; ++i) {
    long long u, v, d;
    cin >> u >> v >> d;
    u--;
    v--;
    D[u] -= d;
    D[v] += d;
  }
  vector<int> Pos, Neg;
  for (int i = 0; i < N; ++i) {
    if (D[i] >= 0)
      Pos.push_back(i);
    else
      Neg.push_back(i);
  }
  int l = 0, r = 0;
  vector<tuple<int, int, long long>> res;
  while (l < Neg.size() && r < Pos.size()) {
    long long d = min(abs(D[Neg[l]]), abs(D[Pos[r]]));
    if (d > 0) res.push_back({Neg[l] + 1, Pos[r] + 1, d});
    D[Neg[l]] += d;
    D[Pos[r]] -= d;
    while (l < Neg.size() && D[Neg[l]] == 0) {
      l++;
    }
    while (r < Pos.size() && D[Pos[r]] == 0) {
      r++;
    }
  }
  cout << res.size() << endl;
  for (auto p : res) {
    cout << get<0>(p) << " " << get<1>(p) << " " << get<2>(p) << "\n";
  }
  return 0;
}
