#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
long long modpow(long long a, long long b,
                 long long mod = (long long)(1e9 + 7)) {
  if (!b) return 1;
  a %= mod;
  return modpow(a * a % mod, b / 2, mod) * (b & 1 ? a : 1) % mod;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, m;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  vector<long long> bal(n + 1);
  for (int i = 0; i < m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    bal[u] -= w;
    bal[v] += w;
  }
  vector<pair<int, long long>> vec;
  for (int i = 1; i <= n; i++)
    if (bal[i] < 0) vec.emplace_back(make_pair(i, -bal[i]));
  stringstream s;
  int k = 0;
  for (int i = 1; i <= n; i++) {
    if (bal[i] > 0 && !vec.empty()) {
      long long mn = min(vec.back().second, bal[i]);
      s << vec.back().first << " " << i << " " << mn << endl;
      k++;
      vec.back().second -= mn, bal[i] -= mn;
      if (!vec.back().second) vec.pop_back();
      if (bal[i]) i--;
    }
  }
  cout << k << endl << s.str();
  return 0;
}
