#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m, a[10010], b[10010], c[10010];
  vector<pair<int, int> > v[110];
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i] >> c[i];
    v[a[i]].push_back(make_pair(b[i], c[i]));
  }
  int vsize;
  for (int i = 0; i < m; i++) {
    vsize = v[b[i]].size();
    for (int j = 0; j < vsize; j++) {
      if (c[i] >= v[b[i]][j].second) {
        c[i] -= v[b[i]][j].second;
        v[b[i]][j].second = 0;
      } else {
        v[b[i]][j].second -= c[i];
        c[i] = 0;
      }
    }
  }
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    vsize = v[i].size();
    for (int j = 0; j < vsize; j++) {
      sum += v[i][j].second;
    }
  }
  cout << sum;
  return 0;
}
