#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
long long int a[N + 5];
struct node {
  int a, b;
  long long int x;
  node(int aa = 0, int bb = 0, long long int c = 0) { a = aa, b = bb, x = c; }
} b[N + 5];
int main() {
  int n, m, x, y, len = 0;
  long long int d;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%lli%", &x, &y, &d);
    a[x] -= d;
    a[y] += d;
  }
  vector<int> vec1;
  vector<int> vec2;
  for (int i = 1; i <= n; i++) {
    if (a[i] < 0)
      vec1.push_back(i);
    else if (a[i] > 0)
      vec2.push_back(i);
  }
  int index = 0;
  for (int i = 0; i < vec1.size(); i++) {
    for (int j = index; j < vec2.size(); j++) {
      if (a[vec1[i]] + a[vec2[j]] > 0) {
        b[len++] = node(vec1[i], vec2[j], -1 * a[vec1[i]]);
        a[vec2[j]] += a[vec1[i]];
        break;
      } else if (a[vec1[i]] + a[vec2[j]] == 0) {
        index++;
        b[len++] = node(vec1[i], vec2[j], -1 * a[vec1[i]]);
        break;
      } else {
        a[vec1[i]] += a[vec2[j]];
        b[len++] = node(vec1[i], vec2[j], a[vec2[j]]);
        index++;
      }
    }
  }
  printf("%d\n", len);
  for (int i = 0; i < len; i++) {
    printf("%d %d %lli\n", b[i].a, b[i].b, b[i].x);
  }
  return 0;
}
