#include <bits/stdc++.h>
using namespace std;
int N, M;
long long A[100010];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    long long x, y, z;
    cin >> x >> y >> z;
    A[x] -= z;
    A[y] += z;
  }
  priority_queue<pair<long long, long long>> pq1, pq2;
  for (int i = 1; i <= N; i++) {
    if (A[i] < 0) pq1.emplace(A[i], i);
    if (A[i] > 0) pq2.emplace(A[i], i);
  }
  vector<pair<pair<long long, long long>, long long>> ans;
  while (!pq1.empty() && !pq2.empty()) {
    pair<long long, long long> from = pq1.top();
    pq1.pop();
    pair<long long, long long> to = pq2.top();
    pq2.pop();
    if (-from.first <= to.first) {
      if (from.second != to.second)
        ans.push_back({{from.second, to.second}, -from.first});
      if (to.first + from.first) pq2.emplace(to.first + from.first, to.second);
    } else {
      if (from.second != to.second)
        ans.push_back({{from.second, to.second}, to.first});
      if (to.first + from.first)
        pq1.emplace(from.first + to.first, from.second);
    }
  }
  cout << ans.size() << '\n';
  for (auto x : ans) {
    cout << x.first.first << ' ' << x.first.second << ' ' << x.second << '\n';
  }
}
