#include <bits/stdc++.h>
using namespace std;
int n, m;
int in[110], out[110];
int main() {
  while (scanf("%d%d", &n, &m) != EOF) {
    int a, b, c;
    memset(in, 0, sizeof(in));
    memset(out, 0, sizeof(out));
    for (int i = 0; i < m; i++) {
      scanf("%d%d%d", &a, &b, &c);
      a--, b--;
      in[b] += c;
      out[a] += c;
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
      int t = min(in[i], out[i]);
      in[i] -= t, out[i] -= t;
      ans += in[i] + out[i];
    }
    cout << ans / 2 << endl;
  }
  return 0;
}
