#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m;
  cin >> n >> m;
  vector<long long> v(n + 1);
  for (int i = 0; i < m; i++) {
    long long x, y, z;
    cin >> x >> y >> z;
    v[x] -= z;
    v[y] += z;
  }
  long long j = 1;
  vector<pair<long long, pair<long long, long long> > > v1;
  for (long long i = 1; i <= n; i++) {
    while (v[i] > 0) {
      while (v[j] >= 0 && j <= n) {
        j++;
      }
      long long mini = min(abs(v[i]), abs(v[j]));
      v[i] -= mini;
      v[j] += mini;
      v1.push_back(make_pair(j, make_pair(i, mini)));
    }
  }
  cout << v1.size() << endl;
  for (auto i : v1) {
    cout << i.first << " " << i.second.first << " " << i.second.second << endl;
  }
}
