#include <bits/stdc++.h>
using namespace std;
long long in[300010];
vector<pair<long long, long long> > d1, d2;
vector<pair<pair<long long, long long>, long long> > ans;
signed main() {
  long long n, m;
  cin >> n >> m;
  for (long long i = 1; i <= m; i++) {
    long long a, b, w;
    cin >> a >> b >> w;
    in[b] += w;
    in[a] -= w;
  }
  for (long long i = 1; i <= n; i++) {
    if (in[i] < 0) d1.push_back({i, -in[i]});
    if (in[i] > 0) d2.push_back({i, in[i]});
  }
  long long l = 0, r = 0;
  long long lim1 = d1.size(), lim2 = d2.size();
  while (l < lim1 && r < lim2) {
    long long tmp = min(d1[l].second, d2[r].second);
    ans.push_back({{d1[l].first, d2[r].first}, tmp});
    d1[l].second -= tmp;
    d2[r].second -= tmp;
    if (d1[l].second == 0) l++;
    if (d2[r].second == 0) r++;
  }
  cout << ans.size() << endl;
  for (auto &it : ans) {
    cout << it.first.first << " " << it.first.second << " " << it.second
         << endl;
  }
}
