#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, i, j, k, d, a[200][200];
  cin >> n >> m;
  for (i = 1; i <= m; ++i) {
    int x, y;
    cin >> x >> y;
    cin >> a[x][y];
  }
  for (int g = 1; g <= n; ++g)
    for (i = 1; i <= n; ++i)
      for (j = 1; j <= n; ++j)
        for (k = 1; k <= n; ++k)
          if (a[i][j] && a[j][k]) {
            if (a[i][j] < a[j][k])
              d = a[i][j];
            else
              d = a[j][k];
            a[i][j] -= d;
            a[j][k] -= d;
            a[i][k] += d;
          }
  long ans = 0;
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= n; ++j) ans += a[i][j];
  cout << ans << endl;
  return 0;
}
