#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long maxr = 1e18;
const int maxn = 1e5 + 10;
long long sum[maxn];
vector<pair<int, long long> > G[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    long long u, v, w;
    cin >> u >> v >> w;
    sum[u] += w;
    sum[v] -= w;
  }
  queue<int> in, out;
  for (int i = 1; i <= n; ++i) {
    if (sum[i] > 0)
      out.push(i);
    else if (sum[i] < 0)
      in.push(i);
  }
  int num = 0;
  while (!out.empty()) {
    int u = out.front();
    out.pop();
    int v = in.front();
    in.pop();
    long long minn = min(sum[u], -sum[v]);
    sum[u] -= minn;
    sum[v] += minn;
    G[u].push_back(pair<int, long long>(v, minn));
    num++;
    if (sum[u] > 0) out.push(u);
    if (sum[v] < 0) in.push(v);
  }
  cout << num << endl;
  for (int i = 1; i <= n; ++i) {
    for (const pair<int, long long> &t : G[i]) {
      cout << i << ' ' << t.first << ' ' << t.second << endl;
    }
  }
  return 0;
}
