#include <bits/stdc++.h>
using namespace std;
int a[102][102];
int main() {
  int n, m;
  cin >> n >> m;
  int sum = 0;
  for (int i = 0; i < m; i++) {
    int x, y, c;
    cin >> x >> y >> c;
    a[x - 1][y - 1] = c;
    sum += c;
  }
  for (int i = 0; i < n; i++) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        int k = 0;
        while (k < n && a[i][j] > 0) {
          if (a[j][k] > 0) {
            int t = min(a[i][j], a[j][k]);
            a[j][k] -= t;
            a[i][j] -= t;
            a[i][k] += t;
            sum -= t;
          }
          k++;
        }
      }
    for (int i = 0; i < n; i++) {
      sum -= a[i][i];
      a[i][i] = 0;
    }
  }
  cout << sum << endl;
  return 0;
}
