#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e2 + 5;
int adj[MAX][MAX];
int n, m, x, y, c;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long ans = 0;
  cin >> n >> m;
  while (m--) {
    cin >> x >> y >> c;
    adj[x][y] = c;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      if (adj[i][j] == 0) continue;
      for (int k = 1; k <= n; k++) {
        if (adj[j][k] == 0) continue;
        x = adj[i][j], y = adj[j][k];
        adj[i][j] -= min(x, y);
        adj[i][k] += min(x, y);
        adj[j][k] -= min(x, y);
      }
    }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) ans += (i != j) * adj[i][j];
  cout << ans;
  return 0;
}
