#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 20;
const int MAXM = 3e5 + 20;
int n, m;
long long in[MAXN];
vector<pair<int, long long>> v0;
vector<tuple<int, int, long long>> ans;
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    in[a] -= c;
    in[b] += c;
  }
  for (int i = 1; i <= n; ++i) {
    if (in[i] < 0) v0.push_back({i, -in[i]});
  }
  for (int i = 1; i <= n; ++i) {
    while (in[i] > 0) {
      long long i2 = min(v0.back().second, in[i]);
      in[i] -= i2, v0.back().second -= i2;
      ans.emplace_back(v0.back().first, i, i2);
      if (!v0.back().second) {
        v0.pop_back();
      }
    }
  }
  cout << ans.size() << endl;
  for (auto i : ans) {
    cout << get<0>(i) << " " << get<1>(i) << " " << get<2>(i) << "\n";
  }
}
