#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
long long a[N], b[N];
vector<int> x, y;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    int x, y;
    long long d;
    scanf("%d%d%lld", &x, &y, &d);
    a[x] += d;
    b[y] += d;
  }
  for (int i = 1; i <= n; ++i) {
    a[i] = b[i] - a[i];
    if (a[i] < 0)
      x.push_back(i), a[i] = -a[i];
    else if (a[i] > 0)
      y.push_back(i);
  }
  vector<pair<pair<int, int>, long long> > ans;
  int r = 0;
  for (int i = 0; i < x.size() && r < y.size(); ++i) {
    if (a[x[i]] >= a[y[r]]) {
      while (r < y.size() && a[x[i]] >= a[y[r]]) {
        a[x[i]] -= a[y[r]];
        ans.push_back(make_pair(make_pair(x[i], y[r]), a[y[r]]));
        r++;
      }
      if (i == x.size() - 1) break;
      if (a[x[i]]) i--;
    } else {
      a[y[r]] -= a[x[i]];
      ans.push_back(make_pair(make_pair(x[i], y[r]), a[x[i]]));
    }
  }
  printf("%d\n", ans.size());
  for (int i = 0; i < ans.size(); ++i)
    printf("%d %d %lld\n", ans[i].first.first, ans[i].first.second,
           ans[i].second);
  return 0;
}
