#include <bits/stdc++.h>
using namespace std;
int a[110][110];
int n, m;
int main() {
  int i, j, k, l;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) scanf("%d%d%d", &j, &k, &l), a[j][k] = l;
  for (k = 1; k <= n; k++)
    for (i = 1; i <= n; i++)
      for (j = 1; j <= n; j++)
        if (a[i][k] && a[k][j]) {
          int mi = min(a[i][k], a[k][j]);
          a[i][k] -= mi;
          a[k][j] -= mi;
          a[i][j] += mi;
        }
  int sum = 0;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++) sum += a[i][j];
  printf("%d\n", sum);
  return 0;
}
