#include <bits/stdc++.h>
using namespace std;
long long matr[129][129];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n + 1; i++)
    for (int j = 0; j < n + 1; j++) matr[i][j] = 0;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    matr[a][b] = c;
  }
  for (int k = 0; k < n + 1; k++)
    for (int i = 0; i < n + 1; i++)
      if ((matr[i][k] != 0))
        for (int j = 0; j < n + 1; j++)
          if (matr[k][j] != 0) {
            long long tr = min(matr[i][k], matr[k][j]);
            matr[i][j] += tr;
            matr[i][k] -= tr;
            matr[k][j] -= tr;
          }
  long long su = 0;
  for (int i = 0; i < n + 1; i++)
    for (int j = 0; j < n + 1; j++) su += matr[i][j];
  cout << su;
}
