#include <bits/stdc++.h>
const int N = 100005;
int n, m;
std::map<int, long long> ma1[N], ma2[N];
std::map<int, long long>::iterator it;
std::pair<int, long long> a[N], b[N];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int x, y;
    long long w;
    scanf("%d%d%lld", &x, &y, &w);
    ma1[y][x] += w;
    ma2[x][y] += w;
  }
  for (int i = 1; i <= n; i++) {
    int top1 = 0, top2 = 0;
    it = ma1[i].begin();
    while (it != ma1[i].end()) a[++top1] = (*it), it++;
    it = ma2[i].begin();
    while (it != ma2[i].end()) b[++top2] = (*it), it++;
    int tmp1 = top1, tmp2 = top2;
    while (top1 && top2) {
      long long w = std::min(a[top1].second, b[top2].second);
      int x = a[top1].first, y = b[top2].first;
      a[top1].second -= w;
      b[top2].second -= w;
      ma1[i][x] -= w;
      ma2[i][y] -= w;
      ma2[x][i] -= w;
      ma1[y][i] -= w;
      if (x != y) ma1[y][x] += w, ma2[x][y] += w;
      if (!a[top1].second) ma1[i].erase(x), ma2[x].erase(i), top1--;
      if (!b[top2].second) ma2[i].erase(y), ma1[y].erase(i), top2--;
    }
  }
  int tot = 0;
  for (int i = 1; i <= n; i++)
    for (it = ma2[i].begin(); it != ma2[i].end(); it++)
      if ((*it).second) tot++;
  printf("%d\n", tot);
  for (int i = 1; i <= n; i++)
    for (it = ma2[i].begin(); it != ma2[i].end(); it++)
      if ((*it).second) printf("%d %d %lld\n", i, (*it).first, (*it).second);
  return 0;
}
