#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 100;
const long long mod = 1e9 + 7;
const long long inf = 1e18;
long long n, m;
map<long long, long long> mp[maxn];
map<long long, bool> vis[maxn];
vector<long long> gin[maxn], gout[maxn];
vector<pair<long long, long long> > ans;
int main() {
  long long v, u, c, p1, p2;
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n >> m;
  for (long long i = 0; i < m; i++) {
    cin >> v >> u >> c;
    gout[v].push_back(u);
    gin[u].push_back(v);
    mp[v][u] += c;
  }
  for (long long i = 1; i <= n; i++) {
    p1 = 0;
    p2 = 0;
    while (p1 < gin[i].size() && p2 < gout[i].size()) {
      v = gin[i][p1];
      u = gout[i][p2];
      if (mp[v][i] == 0 || v == i) {
        mp[v][i] = 0;
        p1++;
        continue;
      }
      if (mp[i][u] == 0 || i == u) {
        mp[i][u] = 0;
        p2++;
        continue;
      }
      if (mp[v][i] >= mp[i][u]) {
        mp[v][i] -= mp[i][u];
        mp[v][u] += mp[i][u];
        mp[i][u] = 0;
        gout[v].push_back(u);
        gin[u].push_back(v);
        p2++;
      } else {
        mp[i][u] -= mp[v][i];
        mp[v][u] += mp[v][i];
        mp[v][i] = 0;
        gout[v].push_back(u);
        gin[u].push_back(v);
        p1++;
      }
    }
  }
  for (long long i = 1; i <= n; i++) {
    for (long long j = 0; j < gout[i].size(); j++) {
      v = gout[i][j];
      if (!vis[i][v] && mp[i][v] > 0 && i != v) {
        ans.push_back({i, v});
        vis[i][v] = 1;
      }
    }
  }
  cout << ans.size() << '\n';
  for (long long i = 0; i < ans.size(); i++) {
    cout << ans[i].first << " " << ans[i].second << " "
         << mp[ans[i].first][ans[i].second] << '\n';
  }
  return 0;
}
