#include <bits/stdc++.h>
using namespace std;
const int maxn = 102;
int g[maxn][maxn];
int ma[maxn];
int n, m;
int main() {
  while (scanf("%d %d", &n, &m) != EOF) {
    memset(g, 0, sizeof(g));
    memset(ma, 0, sizeof(ma));
    int a, b, c;
    while (m--) {
      scanf("%d %d %d", &a, &b, &c);
      g[a][b] = c;
      ma[b] += c;
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (g[i][j]) {
          if (ma[i] >= g[i][j]) {
            ma[i] -= g[i][j];
          } else {
            ma[i] = 0;
            break;
          }
        }
      }
    }
    int res = 0;
    for (int i = 1; i <= n; i++) {
      res += ma[i];
    }
    printf("%d\n", res);
  }
  return 0;
}
