#include <bits/stdc++.h>
using namespace std;
const unsigned long long mod = 1e9 + 7;
template <class S, class T>
ostream& operator<<(ostream& os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<vector<T>> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}
struct hen {
  long long s, t, debt;
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long n, m;
  cin >> n >> m;
  vector<long long> cum(n, 0);
  for (int i = 0; i < (int)m; ++i) {
    long long a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    cum[a] -= c;
    cum[b] += c;
  }
  vector<pair<long long, long long>> pos, neg;
  for (int i = 0; i < (int)n; ++i) {
    if (cum[i] > 0) pos.push_back({i, cum[i]});
    if (cum[i] < 0) neg.push_back({i, -cum[i]});
  }
  vector<hen> res;
  long long A = pos.size();
  long long B = neg.size();
  int j = 0;
  for (int i = 0; i < (int)A; ++i) {
    while (1) {
      if (j >= B) break;
      long long hiku;
      if (pos[i].second >= neg[j].second) {
        hiku = neg[j].second;
      } else {
        hiku = pos[i].second;
      }
      res.push_back((hen){pos[i].first, neg[j].first, hiku});
      pos[i].second -= hiku;
      neg[j].second -= hiku;
      if (neg[j].second == 0) {
        j++;
      }
      if (pos[i].second == 0) break;
    }
  }
  cout << (int)res.size() << endl;
  for (int i = 0; i < (int)res.size(); ++i) {
    cout << res[i].t + 1 << " " << res[i].s + 1 << " " << res[i].debt << endl;
  }
  return 0;
}
