#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline long long rand(long long x, long long y) {
  return rng() % (y + 1 - x) + x;
}
string inline to_string(char c) {
  string second(1, c);
  return second;
}
template <typename T>
inline T gcd(T a, T b) {
  return a == 0 ? llabs(b) : gcd(b % a, a);
}
using ll = long long;
using ld = long double;
using pi = pair<ll, ll>;
using spi = pair<ll, pi>;
using dpi = pair<pi, pi>;
long long LLINF = 1e18;
int INF = 1e9 + 1e6;
ll n, m, A[(200006)];
vector<spi> e;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (ll i = 1; i <= ll(m); ++i) {
    ll a, b, w;
    cin >> a >> b >> w;
    A[a] -= w, A[b] += w;
  }
  deque<pi> dq;
  for (ll i = 1; i <= ll(n); ++i)
    if (A[i]) dq.emplace_back(A[i], i);
  sort((dq).begin(), (dq).end());
  while (dq.size()) {
    assert(dq[0].first < 0 && dq.back().first > 0);
    ll pay = min(-dq[0].first, dq.back().first);
    e.emplace_back(pay, pi(dq[0].second, dq.back().second));
    dq[0].first += pay, dq.back().first -= pay;
    if (dq[0].first == 0) dq.pop_front();
    if (dq.back().first == 0) dq.pop_back();
  }
  cout << e.size() << '\n';
  for (auto i : e)
    cout << i.second.first << ' ' << i.second.second << ' ' << i.first << '\n';
}
