#include <bits/stdc++.h>
const long long int inf = 9e18;
const long double pi = 2 * acos(0.0);
using namespace std;
long long int power(long long int a, long long int n) {
  if (n == 0) {
    return 1;
  }
  long long int p = power(a, n / 2) % 998244353;
  p = (p * p) % 998244353;
  if (n % 2 == 1) {
    p = (p * a) % 998244353;
  }
  return p;
}
long long int gcd(long long int a, long long int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
int isprime(long long int n) {
  for (long long int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      return 0;
    }
  }
  return 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  int n, m;
  cin >> n >> m;
  vector<pair<long long int, long long int> > a[100005];
  for (int i = 0; i < m; i++) {
    int u, v;
    long long int w;
    cin >> u >> v >> w;
    a[u].push_back(make_pair(v, -w));
    a[v].push_back(make_pair(u, w));
  }
  vector<pair<long long int, long long int> > v;
  for (int i = 1; i <= n; i++) {
    long long int sum = 0;
    for (int j = 0; j < a[i].size(); j++) {
      sum += a[i][j].second;
    }
    if (sum > 0) {
      v.push_back(make_pair(sum, i));
    } else {
      v.push_back(make_pair(sum, i));
    }
  }
  sort(v.begin(), v.end());
  vector<pair<pair<long long int, long long int>, long long int> > ans;
  int itr1 = 0;
  int itr2 = v.size() - 1;
  while (itr1 < itr2) {
    long long int z = min(abs(v[itr1].first), abs(v[itr2].first));
    v[itr1].first += z;
    v[itr2].first -= z;
    if (z > 0) {
      ans.push_back(make_pair(make_pair(v[itr1].second, v[itr2].second), z));
    }
    if (v[itr1].first == 0) {
      itr1++;
    }
    if (v[itr2].first == 0) {
      itr2--;
    }
  }
  cout << ans.size() << "\n";
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i].first.first << " " << ans[i].first.second << " "
         << ans[i].second << "\n";
  }
  return 0;
}
