#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
long long int poww(long long int b, long long int e) {
  if (e == 0)
    return 1;
  else if (e % 2 == 0) {
    long long int a = pow(b, e / 2);
    return a * a;
  } else {
    long long int a = pow(b, e / 2);
    return b * a * a;
  }
}
long long int powm(long long int x, long long int y,
                   long long int m = 1000000007) {
  x = x % m;
  long long int res = 1;
  while (y) {
    if (y & 1) res = res * x;
    res %= m;
    y = y >> 1;
    x = x * x;
    x %= m;
  }
  return res;
}
long long int modInverse(long long int a, long long int m = 1000000007) {
  return powm(a, m - 2, m);
}
struct hash_pair {
  template <class T1, class T2>
  size_t operator()(const pair<T1, T2>& p) const {
    auto hash1 = hash<T1>{}(p.first);
    auto hash2 = hash<T2>{}(p.second);
    return hash1 ^ hash2;
  }
};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int n, m, x, y, z;
  cin >> n >> m;
  vector<long long int> give(n + 1), take(n + 1);
  for (long long int i = (0); i <= (m - 1); i += (1)) {
    cin >> x >> y >> z;
    give[x] += z;
    take[y] += z;
  }
  vector<long long int> mp1, mp2;
  for (long long int i = (1); i <= (n); i += (1)) {
    if (give[i] > take[i]) {
      mp1.push_back(i);
      give[i] -= take[i];
      take[i] = 0;
    } else {
      mp2.push_back(i);
      take[i] -= give[i];
      give[i] = 0;
    }
  }
  long long int itr = 0;
  vector<vector<long long int>> anss;
  for (long long int i = (0); i <= ((long long int)mp1.size() - 1); i += (1)) {
    for (; itr < (long long int)mp2.size(); ++itr) {
      if (give[mp1[i]] <= take[mp2[itr]]) {
        vector<long long int> w;
        if (give[mp1[i]] != 0) {
          w.push_back(mp1[i]);
          w.push_back(mp2[itr]);
          w.push_back(give[mp1[i]]);
          anss.push_back(w);
        }
        take[mp2[itr]] -= give[mp1[i]];
        give[mp1[i]] = 0;
        break;
      } else {
        vector<long long int> w;
        if (take[mp2[itr]] != 0) {
          w.push_back(mp1[i]);
          w.push_back(mp2[itr]);
          w.push_back(take[mp2[itr]]);
          anss.push_back(w);
        }
        give[mp1[i]] -= take[mp2[itr]];
        take[mp2[itr]] = 0;
      }
    }
  }
  cout << anss.size() << "\n";
  for (long long int i = (0); i <= ((long long int)anss.size() - 1); i += (1)) {
    cout << anss[i][0] << " " << anss[i][1] << " " << anss[i][2] << "\n";
  }
  return 0;
}
