#include <bits/stdc++.h>
using namespace std;
struct masivs {
  long long in;
  long long out;
  long long poz;
};
masivs mas[100005];
long long i, j, k, n, m, a, b, c;
vector<pair<pair<long long, long long>, long long> > ab;
int main() {
  std::cin.sync_with_stdio(false);
  std::cin.tie(0);
  cin >> n >> m;
  i = 0;
  while (i < n) {
    mas[i].poz = i;
    i++;
  }
  i = 0;
  while (i < m) {
    cin >> a >> b >> c;
    a--;
    b--;
    mas[a].in += c;
    mas[b].out += c;
    k = min(mas[a].in, mas[a].out);
    mas[a].in -= k;
    mas[a].out -= k;
    k = min(mas[b].in, mas[b].out);
    mas[b].in -= k;
    mas[b].out -= k;
    i++;
  }
  j = 0;
  i = 0;
  while (i < n) {
    while (mas[i].in) {
      while (mas[j].out == 0) {
        j++;
      }
      k = min(mas[i].in, mas[j].out);
      ab.push_back({{i + 1, j + 1}, k});
      mas[i].in -= k;
      mas[j].out -= k;
    }
    i++;
  }
  cout << ab.size() << "\n";
  for (j = 0; j < ab.size(); j++) {
    cout << ab[j].first.first << " " << ab[j].first.second << " "
         << ab[j].second << "\n";
  }
  return 0;
}
