#include <bits/stdc++.h>
using namespace std;
inline long long modadd(long long n, long long m) {
  return ((n + m) % 998244353 + 998244353) % 998244353;
}
inline long long modsub(long long n, long long m) {
  return ((n - m + 998244353) % 998244353 + 998244353) % 998244353;
}
inline long long modpro(long long n, long long m) {
  return ((n * m) % 998244353 + 998244353) % 998244353;
}
inline unsigned long long int powe(long long first, long long second) {
  unsigned long long int res = 1;
  while (second > 0) {
    if (second & 1) res = res * first;
    second = second >> 1;
    first = first * first;
  }
  return res;
}
inline long long powmod(long long first, long long second) {
  long long res = 1;
  while (second > 0) {
    if (second & 1) res = modpro(res, first);
    second = second >> 1;
    first = modpro(first, first);
  }
  return res;
}
inline long long modadd3(long long first, long long second, long long z) {
  return modadd(modadd(first, second), z);
}
inline long long modadd4(long long first, long long second, long long z,
                         long long w) {
  return modadd(modadd(first, second), modadd(z, w));
}
template <typename T>
inline T max3(T first, T second, T z) {
  return max(max(first, second), z);
}
template <typename T>
inline T max4(T first, T second, T z, T w) {
  return max(max3(first, second, w), z);
}
template <typename T>
inline T min3(T first, T second, T z) {
  return min(min(first, second), z);
}
template <typename T>
inline T min4(T first, T second, T z, T w) {
  return min(min3(first, second, w), z);
}
template <typename T>
void printArr(T *arr, int n) {
  for (int i = 0; i < n; i++) {
    cout << arr[i] << " ";
  }
  cout << endl;
}
long long a[100005];
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  int erer = 1;
  for (int(erer2) = (1); (erer2) < (erer + 1); (erer2)++) {
    int n, m;
    cin >> n >> m;
    int u, first;
    long long w;
    for (int(i) = (0); (i) < (m); (i)++) {
      cin >> u >> first >> w;
      a[u] -= w;
      a[first] += w;
    }
    vector<pair<long long, int> > v;
    vector<pair<pair<int, int>, long long> > ans;
    for (int(i) = (1); (i) < (n + 1); (i)++) {
      v.push_back({a[i], i});
    }
    sort(v.begin(), v.end());
    int i = 0, j = n - 1;
    long long amt, ct;
    while (v[i].first < 0 || v[j].first > 0) {
      amt = -1LL * v[i].first;
      ct = v[j].first;
      if (amt < ct) {
        if (amt > 0) {
          ans.push_back({{v[i].second, v[j].second}, amt});
        }
        v[i].first = 0;
        i++;
        v[j].first = v[j].first - amt;
      } else {
        if (ct > 0) {
          ans.push_back({{v[i].second, v[j].second}, ct});
        }
        v[j].first = 0;
        j--;
        v[i].first = v[i].first + ct;
      }
    }
    cout << ans.size() << endl;
    for (int(i) = (0); (i) < (((long long)(ans).size())); (i)++) {
      if (ans[i].second != 0)
        cout << (ans[i].first.first) << " " << (ans[i].first.second) << " "
             << (ans[i].second) << endl;
    }
  }
  return 0;
}
