#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int n, m;
  cin >> n >> m;
  vector<long long> bal;
  bal.resize(n + 1);
  for (int i = 0; i < m; i++) {
    int u, v;
    long long w;
    cin >> u >> v >> w;
    bal[u] -= w;
    bal[v] += w;
  }
  vector<int> neg, pos;
  for (int u = 1; u <= n; u++) {
    if (bal[u] < 0) {
      neg.push_back(u);
    } else if (bal[u] > 0) {
      pos.push_back(u);
    }
  }
  vector<pair<pair<int, int>, long long> > ans;
  int l = 0;
  int r = 0;
  while (l < neg.size() && r < pos.size()) {
    long long x = min(-bal[neg[l]], bal[pos[r]]);
    bal[neg[l]] += x;
    bal[pos[r]] -= x;
    ans.push_back(make_pair(make_pair(neg[l], pos[r]), x));
    if (bal[neg[l]] == 0) l++;
    if (bal[pos[r]] == 0) r++;
  }
  cout << ans.size() << '\n';
  for (auto x : ans) {
    cout << x.first.first << " " << x.first.second << " " << x.second << '\n';
  }
  return 0;
}
