#include <bits/stdc++.h>
using namespace std;
const long long INF = 9223372036854775807ll;
const long long dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, inf = 2147483647,
                nmax = 300005;
struct trip {
  long long a, b, c;
};
long long n, m, s[nmax];
vector<pair<long long, long long> > neg, poz;
vector<trip> sol;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  for (long long i = 1; i <= m; ++i) {
    long long x, y, w;
    cin >> x >> y >> w;
    s[x] -= w;
    s[y] += w;
  }
  for (long long i = 1; i <= n; ++i) {
    if (s[i] > 0) poz.push_back({i, s[i]});
    if (s[i] < 0) neg.push_back({i, -s[i]});
  }
  while (poz.size() && neg.size()) {
    long long x = min(poz.back().second, neg.back().second);
    long long a = neg.back().first, b = poz.back().first;
    sol.push_back({a, b, x});
    poz.back().second -= x;
    neg.back().second -= x;
    if (poz.back().second == 0) poz.pop_back();
    if (neg.back().second == 0) neg.pop_back();
  }
  cout << sol.size() << "\n";
  for (auto x : sol) cout << x.a << " " << x.b << " " << x.c << "\n";
  return 0;
}
