#include <bits/stdc++.h>
using namespace std;
int n, m;
long long d[100001];
vector<int> pos;
vector<int> neg;
struct ans {
  int u, v;
  long long d;
};
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    long long w;
    cin >> w;
    d[u] += w;
    d[v] -= w;
  }
  for (int i = 1; i <= n; i++) {
    if (d[i] < 0)
      neg.push_back(i);
    else if (d[i] > 0)
      pos.push_back(i);
  }
  vector<ans> res;
  while (!pos.empty() && !neg.empty()) {
    long long w1 = d[pos.back()];
    long long w2 = -d[neg.back()];
    int a = pos.back();
    int b = neg.back();
    long long w = min(w1, w2);
    res.push_back({a, b, w});
    d[pos.back()] -= w;
    d[neg.back()] += w;
    if (d[pos.back()] == 0) pos.pop_back();
    if (d[neg.back()] == 0) neg.pop_back();
  }
  cout << res.size() << "\n";
  for (auto v : res) {
    cout << v.u << " " << v.v << " " << v.d << "\n";
  }
  return 0;
}
