#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long int llINF = (long long)(1e18) + 100;
const int MAXN = 1e5 + 10;
long long int in[MAXN], out[MAXN];
vector<long long int> e, s;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  long long int x;
  cin >> n >> m;
  long long int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> x;
    out[a] += x;
    in[b] += x;
  }
  for (int i = 1; i <= n; i++) {
    if (in[i] > out[i])
      e.push_back(i);
    else
      s.push_back(i);
    out[i] -= in[i];
    out[i] = max(out[i], -out[i]);
  }
  vector<tuple<long long int, long long int, long long int> > ans;
  for (auto a : s) {
    while (out[a]) {
      b = e.back();
      e.pop_back();
      long long int mn = min(out[a], out[b]);
      out[a] -= mn;
      out[b] -= mn;
      ans.push_back({a, b, mn});
      if (out[b]) e.push_back(b);
    }
  }
  cout << ans.size() << endl;
  for (auto t : ans) {
    tie(a, b, x) = t;
    cout << a << ' ' << b << ' ' << x << endl;
  }
}
