#include <bits/stdc++.h>
using namespace std;
void canhazfast() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
}
template <typename T>
T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
template <typename T>
T extgcd(T a, T b, T &x, T &y) {
  T x0 = 1, y0 = 0, x1 = 0, y1 = 1;
  while (b) {
    T q = a / b;
    a %= b;
    swap(a, b);
    x0 -= q * x1;
    swap(x0, x1);
    y0 -= q * y1;
    swap(y0, y1);
  }
  x = x0;
  y = y0;
  return a;
}
int ctz(unsigned x) { return __builtin_ctz(x); }
int ctzll(unsigned long long x) { return __builtin_ctzll(x); }
int clz(unsigned x) { return __builtin_clz(x); }
int clzll(unsigned long long x) { return __builtin_clzll(x); }
int popcnt(unsigned x) { return __builtin_popcount(x); }
int popcntll(unsigned long long x) { return __builtin_popcountll(x); }
int bsr(unsigned x) { return 31 ^ clz(x); }
int bsrll(unsigned long long x) { return 63 ^ clzll(x); }
map<int, long long> in[100016], out[100016];
void do_vertex(int u) {
  for (auto i = in[u].begin(), j = out[u].begin();
       i != in[u].end() && j != out[u].end();) {
    int s, t;
    long long fs, ft;
    tie(s, fs) = *i;
    tie(t, ft) = *j;
    long long z = min(fs, ft);
    if (z > 0) {
      out[s][u] -= z;
      in[t][u] -= z;
      i->second -= z;
      j->second -= z;
      if (s != t) {
        out[s][t] += z;
        in[t][s] += z;
      }
    }
    if (i->second == 0) i = in[u].erase(i);
    if (j->second == 0) j = out[u].erase(j);
  }
}
int main() {
  canhazfast();
  int n, m;
  vector<pair<pair<int, int>, long long>> ans;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    out[u][v] += w;
    in[v][u] += w;
  }
  for (int i = 1; i <= n; ++i) do_vertex(i);
  for (int i = 1; i <= n; ++i) {
    for (const auto &x : out[i]) {
      if (!x.second || x.first == i) continue;
      ans.emplace_back(pair<int, int>(i, x.first), x.second);
    }
  }
  cout << ans.size() << '\n';
  for (const pair<pair<int, int>, long long> e : ans) {
    cout << e.first.first << ' ' << e.first.second << ' ' << e.second << '\n';
  }
  return 0;
}
