#include <bits/stdc++.h>
using namespace std;
long long n, m;
long long val[100008];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (long long int i = 0; i < m; i++) {
    long long x, y, w;
    cin >> x >> y >> w;
    if (x == y) continue;
    val[x] += w;
    val[y] -= w;
  }
  std::vector<long long> posw, negw;
  for (long long int i = 1; i < n + 1; i++) {
    if (val[i] > 0) {
      posw.push_back(i);
    }
    if (val[i] < 0) {
      negw.push_back(i);
    }
  }
  long long cp = 0, cn = 0;
  std::vector<pair<long long, pair<long long int, long long int>>> res;
  while (cp < posw.size() && cn < negw.size()) {
    if (val[posw[cp]] > abs(val[negw[cn]])) {
      res.push_back({posw[cp], {negw[cn], abs(val[negw[cn]])}});
      val[posw[cp]] -= abs(val[negw[cn]]);
      cn++;
      continue;
    }
    if (val[posw[cp]] == abs(val[negw[cn]])) {
      res.push_back({posw[cp], {negw[cn], val[posw[cp]]}});
      val[negw[cn]] = (val[negw[cn]] + val[posw[cp]]);
      val[posw[cp]] = 0;
      cp++;
      cn++;
      continue;
    }
    if (val[posw[cp]] < abs(val[negw[cn]])) {
      res.push_back({posw[cp], {negw[cn], val[posw[cp]]}});
      val[negw[cn]] = (val[negw[cn]] + val[posw[cp]]);
      val[posw[cp]] = 0;
      cp++;
      continue;
    }
  }
  cout << res.size() << '\n';
  for (auto i : res) {
    cout << i.first << " " << i.second.first << " " << i.second.second << '\n';
  }
}
