#include <bits/stdc++.h>
using namespace std;
template <class t>
inline t read(t &x) {
  x = 0;
  char c = getchar();
  bool f = 0;
  while (!isdigit(c)) f |= c == '-', c = getchar();
  while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  if (f) x = -x;
  return x;
}
template <class t>
inline void write(t x) {
  if (x < 0) {
    putchar('-'), write(-x);
  } else {
    if (x > 9) write(x / 10);
    putchar('0' + x % 10);
  }
}
const long long N = 1e5 + 5;
long long n, m, in[N], out[N];
vector<long long> a, b;
struct edge {
  long long x, y, z;
};
vector<edge> ans;
signed main() {
  read(n);
  read(m);
  for (long long i = 1, x, y, z; i <= m; i++) {
    read(x);
    read(y);
    read(z);
    out[x] += z;
    in[y] += z;
  }
  for (long long i = 1; i <= n; i++) {
    long long tp = min(in[i], out[i]);
    in[i] -= tp;
    out[i] -= tp;
    if (in[i]) b.push_back(i);
    if (out[i]) a.push_back(i);
  }
  for (long long i = 0, j = 0; i < a.size() && j < b.size();) {
    long long tp = min(out[a[i]], in[b[j]]);
    out[a[i]] -= tp;
    in[b[j]] -= tp;
    ans.push_back((edge){a[i], b[j], tp});
    if (out[a[i]] == 0) i++;
    if (in[b[j]] == 0) j++;
  }
  write(ans.size());
  puts("");
  for (long long i = 0; i < ans.size(); i++) {
    write(ans[i].x);
    putchar(' ');
    write(ans[i].y);
    putchar(' ');
    write(ans[i].z);
    puts("");
  }
}
