#include <bits/stdc++.h>
using namespace std;
const long long int INF = 1e18 + 1;
const long long int MOD = 1000000007L;
clock_t time_p = clock();
void timer() {
  time_p = clock() - time_p;
  cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}
template <typename Arg1>
ostream& operator<<(ostream& out, const vector<Arg1>& a) {
  out << "[ ";
  for (const auto& x : a) out << x << " ";
  return out << "]";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n, m;
  cin >> n >> m;
  vector<long long int> det(n, 0);
  for (int i = 0; i < (m); ++i) {
    long long int u, v, d;
    cin >> u >> v >> d;
    u--;
    v--;
    det[u] -= d;
    det[v] += d;
  }
  vector<pair<long long int, long long int> > first, second;
  for (int i = 0; i < (n); ++i) {
    if (det[i] < 0) {
      first.push_back({i, -det[i]});
    } else if (det[i] > 0) {
      second.push_back({i, det[i]});
    }
  }
  priority_queue<pair<long long int, long long int> > L, R;
  vector<long long int> au, av, ad;
  for (auto x : first) {
    L.push({x.second, x.first});
  }
  for (auto y : second) {
    R.push({y.second, y.first});
  }
  while (!L.empty() && !R.empty()) {
    pair<long long int, long long int> l = L.top();
    L.pop();
    pair<long long int, long long int> r = R.top();
    R.pop();
    long long int amount = min(l.first, r.first);
    au.push_back(l.second);
    av.push_back(r.second);
    ad.push_back(amount);
    l.first -= amount;
    r.first -= amount;
    if (l.first > 0) L.push(l);
    if (r.first > 0) R.push(r);
  }
  long long int sz = au.size();
  cout << au.size() << endl;
  for (int i = 0; i < (sz); ++i) {
    cout << au[i] + 1 << " " << av[i] + 1 << " " << ad[i] << "\n";
  }
  timer();
  return 0;
}
