#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int> > g(n, vector<int>(n));
  queue<int> q;
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    --a, --b;
    g[a][b] = c;
  }
  for (int h = 0; h < n; ++h) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        for (int k = 0; k < n; ++k) {
          if (g[i][k] > 0 && g[k][j] > 0) {
            int d = min(g[i][k], g[k][j]);
            g[i][k] -= d;
            g[k][j] -= d;
            g[i][j] += d;
          }
        }
      }
    }
  }
  int sum = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      sum += g[i][j];
    }
  }
  cout << sum;
  return 0;
}
