#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 1e5 + 5;
ll debt[maxn];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0, u, v, d; i < m; i++) {
    scanf("%d%d%d", &u, &v, &d);
    debt[u] -= d;
    debt[v] += d;
  }
  vector<pair<int, ll>> vec;
  for (int i = 1; i <= n; i++)
    if (debt[i] < 0) vec.push_back({i, -debt[i]});
  vector<tuple<int, int, ll>> ans;
  for (int i = 1; i <= n; i++)
    while (debt[i] > 0) {
      auto &now = vec.back();
      ll d = min(now.second, debt[i]);
      ans.push_back({now.first, i, d});
      now.second -= d;
      debt[i] -= d;
      if (now.second == 0) vec.pop_back();
    }
  printf("%d\n", ans.size());
  for (auto &x : ans) printf("%d %d %lld\n", get<0>(x), get<1>(x), get<2>(x));
}
