#include <bits/stdc++.h>
using namespace std;
int n, m, d[111][111];
int main() {
  scanf("%d%d", &n, &m);
  int x, y, z;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &x, &y, &z);
    d[x][y] = z;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i == j) continue;
      if (d[i][j] > d[j][i])
        d[i][j] -= d[j][i], d[j][i] = 0;
      else
        d[j][i] -= d[i][j], d[i][j] = 0;
    }
  }
  for (int j = 1; j <= n; j++) {
    for (int i = 1; i <= n; i++) {
      for (int k = 1; k <= n; k++) {
        if (i == j || i == k || j == k) continue;
        if (d[i][j] && d[j][k] && d[i][j] >= d[j][k]) {
          d[i][j] -= d[j][k];
          d[i][k] += d[j][k];
          d[j][k] = 0;
          if (d[i][j] > d[j][i])
            d[i][j] -= d[j][i], d[j][i] = 0;
          else
            d[j][i] -= d[i][j], d[i][j] = 0;
          if (d[i][k] > d[k][i])
            d[i][k] -= d[k][i], d[k][i] = 0;
          else
            d[k][i] -= d[i][k], d[i][k] = 0;
        }
        if (d[i][j] && d[j][k] && d[i][j] <= d[j][k]) {
          d[j][k] -= d[i][j];
          d[i][k] += d[i][j];
          d[i][j] = 0;
          if (d[j][k] > d[k][j])
            d[j][k] -= d[k][j], d[k][j] = 0;
          else
            d[k][j] -= d[j][k], d[j][k] = 0;
          if (d[i][k] > d[k][i])
            d[i][k] -= d[k][i], d[k][i] = 0;
          else
            d[k][i] -= d[i][k], d[i][k] = 0;
        }
      }
    }
  }
  int sol = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i == j) continue;
      sol += d[i][j];
    }
  }
  printf("%d", sol);
  return 0;
}
