#include <bits/stdc++.h>
using namespace std;
void sb() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
std::vector<vector<int>> graph(1e5);
vector<int> visited(1e5, 0);
std::vector<pair<pair<int, int>, long long>> ans;
std::vector<pair<long long, long long>> g(1e5, {0, 0});
std::vector<pair<long long, long long>> pay;
std::vector<pair<long long, long long>> to;
void dfs(int start, int src) {
  for (int i = 0; i < graph[start].size() && visited[src] < 0; i++) {
    if (visited[graph[start][i]] > 0) {
      ans.push_back({{src + 1, graph[start][i] + 1},
                     min(abs(visited[src]), visited[graph[start][i]])});
      visited[src] += min(abs(visited[src]), visited[graph[src][i]]);
      visited[graph[start][i]] -=
          min(abs(visited[src]), visited[graph[start][i]]);
      if (visited[src] < 0) {
        dfs(graph[start][i], src);
      }
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    long long d;
    cin >> u >> v >> d;
    u--;
    v--;
    g[u].first += d;
    g[v].second += d;
    graph[u].push_back(v);
  }
  for (int i = 0; i < g.size(); i++) {
    if (g[i].first > g[i].second)
      pay.push_back({g[i].first - g[i].second, i + 1});
    if (g[i].first < g[i].second)
      to.push_back({g[i].second - g[i].first, i + 1});
  }
  sort(pay.begin(), pay.end());
  sort(to.begin(), to.end());
  for (int i = 0, j = 0; i < pay.size() && j < to.size();) {
    if (pay[i].first > to[j].first) {
      ans.push_back({{pay[i].second, to[j].second}, to[j].first});
      pay[i].first -= to[j].first;
      j++;
    } else if (pay[i].first < to[j].first) {
      ans.push_back({{pay[i].second, to[j].second}, pay[i].first});
      to[j].first -= pay[i].first;
      i++;
    } else {
      ans.push_back({{pay[i].second, to[j].second}, pay[i].first});
      i++;
      j++;
    }
  }
  cout << ans.size() << endl;
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i].first.first << " " << ans[i].first.second << " "
         << ans[i].second << endl;
  }
}
