#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, c, d[300][300] = {0}, e, f = 0, g = 0, h, j, k, i, l, n, m;
  cin >> n >> m;
  for (i = 1; i <= m; i++) {
    cin >> a >> b >> c;
    d[a][b] = c;
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      for (k = 1; k <= n; k++) {
        if (d[i][j] > 0 && d[j][k] > 0) {
          e = min(d[i][j], d[j][k]);
          d[i][j] -= e;
          d[j][k] -= e;
          d[i][k] += e;
        }
      }
    }
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      if (i != j) {
        f += d[i][j];
      }
    }
  }
  cout << f;
  return 0;
}
