#include <bits/stdc++.h>
using namespace std;
void rwFile() {
  freopen("INP.inp", "r", stdin);
  freopen("OUT.out", "w", stdout);
}
const int MAXN = 1e5 + 5;
long long inD[MAXN];
long long outD[MAXN];
struct i2 {
  long long x, y;
  i2() {}
  i2(long long _x, long long _y) {
    x = _x;
    y = _y;
  }
};
void solve() {
  int n, m;
  cin >> n >> m;
  vector<i2> In, Out;
  for (int i = 0; i < m; i++) {
    int u, v;
    long long d;
    cin >> u >> v >> d;
    inD[v] += d;
    outD[u] += d;
  }
  for (int i = 1; i <= n; i++) {
    long long diff = inD[i] - outD[i];
    if (diff > 0)
      In.push_back(i2(i, diff));
    else if (diff < 0)
      Out.push_back(i2(i, -diff));
  }
  vector<i2> Edge;
  vector<long long> Cost;
  int i = 0, j = 0;
  while (i < In.size() && j < Out.size()) {
    Edge.push_back(i2(In[i].x, Out[j].x));
    long long diff = min(In[i].y, Out[j].y);
    Cost.push_back(diff);
    In[i].y -= diff;
    if (In[i].y == 0) i++;
    Out[j].y -= diff;
    if (Out[j].y == 0) j++;
  }
  cout << Cost.size() << '\n';
  for (int x = 0; x < Cost.size(); x++) {
    cout << Edge[x].y << " " << Edge[x].x << " " << Cost[x] << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cout.tie(nullptr);
  solve();
}
