#include <bits/stdc++.h>
using namespace std;
const long long maxinput = 500005;
long long n, m;
long long money[maxinput];
struct compare {
  bool operator()(pair<long long, long long> x, pair<long long, long long> y) {
    return (x.second < y.second);
  }
};
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  memset(money, 0, sizeof(money));
  cin >> n >> m;
  for (long long i = 0; i < m; i++) {
    long long u, v, d;
    cin >> u >> v >> d;
    money[u] -= d;
    money[v] += d;
  }
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 compare>
      pq_pos, pq_neg;
  for (long long i = 1; i <= n; i++) {
    if (money[i] < 0)
      pq_neg.push(make_pair(i, -money[i]));
    else if (money[i] > 0)
      pq_pos.push(make_pair(i, money[i]));
  }
  vector<pair<pair<long long, long long>, long long>> ans;
  while (!pq_pos.empty()) {
    long long val1 = pq_pos.top().second;
    long long id1 = pq_pos.top().first;
    long long val2 = pq_neg.top().second;
    long long id2 = pq_neg.top().first;
    pq_pos.pop();
    pq_neg.pop();
    if (val1 > val2) {
      ans.push_back(make_pair(make_pair(id2, id1), val2));
      pq_pos.push(make_pair(id1, val1 - val2));
    } else if (val2 > val1) {
      ans.push_back(make_pair(make_pair(id2, id1), val1));
      pq_neg.push(make_pair(id2, val2 - val1));
    } else
      ans.push_back(make_pair(make_pair(id2, id1), val2));
  }
  cout << (long long)ans.size() << endl;
  for (long long i = 0; i < ans.size(); i++)
    cout << ans[i].first.first << " " << ans[i].first.second << " "
         << ans[i].second << endl;
}
