#include <bits/stdc++.h>
using namespace std;
long long blc[100000ll * 2];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cout << fixed;
  cout << setprecision(10);
  long long n, m;
  cin >> n >> m;
  vector<long long> d1, d2;
  for (long long i = 0; i < m; i++) {
    long long a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    blc[a] += c;
    blc[b] -= c;
  }
  for (long long i = 0; i < n; i++) {
    if (blc[i] > 0) d1.push_back(i);
    if (blc[i] < 0) d2.push_back(i);
  }
  long long i = 0;
  long long j = 0;
  vector<long long> p1, p2, p3;
  long long ans = 0;
  while (i < d1.size()) {
    long long x = min(abs(blc[d1[i]]), abs(blc[d2[j]]));
    ans++;
    p1.push_back(d1[i] + 1);
    p2.push_back(d2[j] + 1);
    p3.push_back(x);
    blc[d1[i]] -= x;
    blc[d2[j]] += x;
    if (blc[d1[i]] == 0) i++;
    if (blc[d2[j]] == 0) j++;
  }
  cout << ans << endl;
  for (long long i = 0; i < ans; i++) {
    cout << p1[i] << " " << p2[i] << " " << p3[i] << endl;
  }
}
