#include <bits/stdc++.h>
using namespace std;
template <class A>
istream& operator>>(istream& in, vector<A>& i) {
  for (auto& x : i) in >> x;
  return in;
};
template <class A, class B>
istream& operator>>(istream& in, pair<A, B>& i) {
  in >> i.first >> i.second;
  return in;
}
int main() {
  int x;
  int n, m;
  cin >> n >> m;
  vector<long long> debt(n + 1);
  for (int i = 0; i < m; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    debt[u] += w;
    debt[v] -= w;
  }
  vector<tuple<int, int, long long>> answer;
  int j = 1;
  for (int i = 1; i <= n; i++) {
    if (debt[i] <= 0) continue;
    while (debt[i]) {
      while (debt[j] >= 0 && j <= n) ++j;
      long long a = min(debt[i], -debt[j]);
      debt[i] -= a;
      debt[j] += a;
      answer.emplace_back(i, j, a);
    }
  }
  cout << answer.size() << endl;
  for (auto& x : answer)
    cout << get<0>(x) << ' ' << get<1>(x) << ' ' << get<2>(x) << endl;
}
