#include <bits/stdc++.h>
using namespace std;
int n, m, u, v, c, ans, d, mat[105][105];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d %d %d", &u, &v, &c);
    mat[u][v] = c;
  }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      for (int k = 1; k <= n; ++k) {
        if (mat[i][j] > 0 && mat[j][k] > 0) {
          d = min(mat[i][j], mat[j][k]);
          mat[i][j] -= d;
          mat[j][k] -= d;
          mat[i][k] += d;
        }
      }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      if (i != j) ans += mat[i][j];
  printf("%d\n", ans);
  return 0;
}
