#include <bits/stdc++.h>
#pragma GCC optimize(3, "Ofast", "inline")
using namespace std;
const long long mod = 1e9 + 7;
const long long maxn = 1e6 + 5;
long long a[maxn];
int main() {
  long long n, m;
  cin >> n >> m;
  for (long long i = 1; i <= m; i++) {
    long long u, v, d;
    cin >> u >> v >> d;
    a[u] -= d;
    a[v] += d;
  }
  vector<pair<long long, long long> > in, out;
  vector<pair<pair<long long, long long>, long long> > ans;
  for (long long i = 1; i <= n; i++) {
    if (a[i] < 0) out.push_back({-a[i], i});
    if (a[i] > 0) in.push_back({a[i], i});
  }
  long long i = 0, j = 0;
  while (i < out.size() && j < in.size()) {
    if (out[i].first > in[j].first) {
      ans.push_back({{out[i].second, in[j].second}, in[j].first});
      out[i].first -= in[j].first;
      j++;
    } else if (out[i].first < in[j].first) {
      ans.push_back({{out[i].second, in[j].second}, out[i].first});
      in[j].first -= out[i].first;
      i++;
    } else {
      ans.push_back({{out[i].second, in[j].second}, in[j].first});
      i++, j++;
    }
  }
  cout << ans.size() << "\n";
  for (long long i = 0; i < ans.size(); i++) {
    cout << ans[i].first.first << " " << ans[i].first.second << " "
         << ans[i].second << "\n";
  }
}
