#include <bits/stdc++.h>
using namespace std;
void boost() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int main() {
  boost();
  long long n, m;
  cin >> n >> m;
  vector<pair<long long, long long>> cash;
  long long a, b, c;
  long long i;
  for (i = 0; i < n; i++) {
    cash.push_back(make_pair(0, i + 1));
  }
  for (i = 0; i < m; i++) {
    cin >> a >> b >> c;
    cash[a - 1].first -= c;
    cash[b - 1].first += c;
  }
  sort(cash.begin(), cash.end());
  long long p1 = 0, p2 = n - 1, minm, owe1, owe2, cnt = 0;
  vector<pair<int, pair<int, long long>>> vec;
  while (p1 < p2) {
    if (cash[p2].first == 0) {
      p2--;
      continue;
    }
    if (cash[p1].first == 0) {
      p1++;
      continue;
    }
    cnt++;
    owe1 = abs(cash[p1].first);
    owe2 = abs(cash[p2].first);
    minm = min(owe1, owe2);
    vec.push_back(make_pair(cash[p1].second, make_pair(cash[p2].second, minm)));
    cash[p1].first += minm;
    cash[p2].first -= minm;
  }
  cout << cnt << endl;
  for (i = 0; i < cnt; i++) {
    cout << vec[i].first << ' ' << vec[i].second.first << ' '
         << vec[i].second.second << endl;
  }
}
