#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n, m;
  cin >> n >> m;
  long long node[n + 1];
  memset(node, 0, sizeof(node));
  for (long long i = 0; i < m; i++) {
    long long x, y, val;
    cin >> x >> y >> val;
    node[x] -= val;
    node[y] += val;
  }
  vector<pair<long long, long long> > a, b;
  for (long long i = 1; i <= n; i++) {
    if (node[i] > 0)
      a.push_back({node[i], i});
    else
      b.push_back({abs(node[i]), i});
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  vector<pair<long long, long long> > ans;
  vector<long long> c;
  long long ind = a.size() - 1;
  for (long long i = 0; i < b.size(); i++) {
    long long curr = b[i].first;
    while (curr > 0 && ind >= 0) {
      long long tmp = a[ind].first;
      ans.push_back({b[i].second, a[ind].second});
      if (tmp > curr) {
        a[ind].first -= curr;
        c.push_back(curr);
        curr = 0;
        break;
      }
      c.push_back(tmp);
      ind--;
      curr -= tmp;
    }
  }
  cout << ans.size() << '\n';
  for (long long i = 0; i < ans.size(); i++)
    cout << ans[i].first << " " << ans[i].second << " " << c[i] << '\n';
}
