#include <bits/stdc++.h>
using namespace std;
int main() {
  int T = 1;
  while (T--) {
    int n;
    std::cin >> n;
    ;
    int m;
    std::cin >> m;
    ;
    std::vector<long long int> v(n + 1, 0);
    for (int i = 0; i < m; ++i) {
      long long int a, b, c;
      std::cin >> a >> b >> c;
      ;
      v[a] -= c;
      v[b] += c;
    }
    set<pair<long long int, long long int> > pos;
    set<pair<long long int, long long int> > neg;
    for (int i = 0; i < n + 1; ++i) {
      if (v[i] > 0) {
        pos.insert({(long long int)v[i], (long long int)i});
      } else if (v[i] < 0) {
        neg.insert({(long long int)v[i], (long long int)i});
      }
    }
    vector<tuple<long long int, long long int, long long int> > answer;
    while (pos.size() != 0 && neg.size() != 0) {
      auto it = pos.end();
      it--;
      pair<long long int, long long int> p = *it;
      long long int p_index = it->second;
      long long int p_val = it->first;
      pos.erase(it);
      auto jt = neg.begin();
      pair<long long int, long long int> q = *jt;
      long long int q_index = jt->second;
      long long int q_val = (-1) * jt->first;
      neg.erase(jt);
      if (p_val >= q_val) {
        answer.push_back({q_index, p_index, q_val});
        p.first -= q_val;
        v[p.second] = p.first;
        q.first = 0;
        v[q.second] = q.first;
      } else {
        answer.push_back({q_index, p_index, p_val});
        p.first = 0;
        v[p.second] = p.first;
        q.first += p_val;
        v[q.second] = q.first;
      }
      if (p.first != 0) {
        pos.insert(p);
      }
      if (q.first != 0) {
        neg.insert(q);
      }
    }
    std::cout << answer.size() << std::endl;
    ;
    for (int i = 0; i < (int)answer.size(); ++i) {
      std::cout << get<0>(answer[i]) << ' ' << get<1>(answer[i]) << ' '
                << get<2>(answer[i]) << ' ' << std::endl
                << std::flush;
      ;
    }
  }
  return 0;
}
