#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int M = 1e5 + 5;
int main() {
  ios_base::sync_with_stdio(false);
  long long val[N];
  int i, n, m;
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    val[i] = 0;
  }
  for (i = 0; i < m; i++) {
    int a, b;
    long long d;
    cin >> a >> b;
    cin >> d;
    val[a] -= d;
    val[b] += d;
  }
  vector<pair<long long, int> > v[N];
  vector<int> f;
  set<pair<long long, int> > st;
  for (i = 1; i <= n; i++) {
    if (val[i] > 0)
      f.push_back(i);
    else if (val[i] < 0)
      st.insert(make_pair(val[i], i));
  }
  int lu = f.size();
  int cnt = 0;
  for (auto x : f) {
    long long foo = val[x];
    while (1) {
      pair<long long, int> p = *(st.begin());
      st.erase(st.begin());
      long long bar = -p.first;
      if (bar > foo) {
        cnt++;
        v[p.second].push_back(make_pair(min(foo, bar), x));
        bar -= foo;
        bar *= -1;
        st.insert(make_pair(bar, p.second));
        break;
      } else if (bar < foo) {
        cnt++;
        v[p.second].push_back(make_pair(min(foo, bar), x));
        foo -= bar;
      } else {
        cnt++;
        v[p.second].push_back(make_pair(min(foo, bar), x));
        break;
      }
    }
  }
  cout << cnt << endl;
  for (i = 1; i <= n; i++) {
    for (auto x : v[i]) cout << i << " " << x.second << " " << x.first << endl;
  }
  return 0;
}
