#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<long long>> adj;
  vector<long long> pos, neg;
  vector<long long> bal(n + 1);
  for (int i = 0; i < m; i++) {
    long long u, v, d;
    cin >> u >> v >> d;
    bal[u] -= d;
    bal[v] += d;
  }
  for (int i = 1; i <= n; i++) {
    if (bal[i] > 0) pos.push_back(i);
    if (bal[i] < 0) neg.push_back(i);
  }
  int pi = 0, ni = 0;
  while (pi < pos.size()) {
    long long u, v, d;
    u = neg[ni];
    v = pos[pi];
    bal[u] = abs(bal[u]);
    d = min(bal[u], bal[v]);
    bal[u] -= d;
    bal[v] -= d;
    adj.push_back({u, v, d});
    if (bal[u] == 0) ni += 1;
    if (bal[v] == 0) pi += 1;
  }
  cout << adj.size() << "\n";
  for (auto& v : adj) cout << v[0] << " " << v[1] << " " << v[2] << "\n";
}
