#include <bits/stdc++.h>
using namespace std;
struct re {
  int x, y;
  long long d;
};
const int inf = 1e9 + 10;
const int N = 1e5 + 10;
int n, m;
long long a[N];
priority_queue<pair<long long, int> > pqPos, pqNeg;
vector<re> ans;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    int x, y, d;
    cin >> x >> y >> d;
    a[x] -= d;
    a[y] += d;
  }
  for (int i = 1; i <= n; ++i)
    if (a[i] < 0)
      pqNeg.push(pair<long long, int>(-a[i], i));
    else if (a[i] > 0)
      pqPos.push(pair<long long, int>(a[i], i));
  while (pqNeg.size() && pqPos.size()) {
    if (pqNeg.top().first > pqPos.top().first) {
      pair<long long, int> tg = pqNeg.top();
      pqNeg.pop();
      tg.first -= pqPos.top().first;
      pqNeg.push(tg);
      ans.push_back({tg.second, pqPos.top().second, pqPos.top().first});
      pqPos.pop();
    } else {
      pair<long long, int> tg = pqPos.top();
      pqPos.pop();
      tg.first -= pqNeg.top().first;
      if (tg.first) pqPos.push(tg);
      ans.push_back({pqNeg.top().second, tg.second, pqNeg.top().first});
      pqNeg.pop();
    }
  }
  cout << ans.size() << '\n';
  for (auto i : ans) cout << i.x << ' ' << i.y << ' ' << i.d << '\n';
  return 0;
}
