#include <bits/stdc++.h>
using namespace std;
int getnum() {
  char ch;
  while (ch = getchar(), ch == 10 || ch == 32)
    ;
  int ans = ch - 48;
  while ((ch = getchar()) != EOF && (ch >= '0' && ch <= '9')) {
    ans *= 10;
    ans += ch - '0';
  }
  return ans;
}
struct info {
  int a, b, c;
} q[10005];
int vis[10005];
int main() {
  int n, m;
  while (scanf("%d %d", &n, &m) != EOF) {
    memset(vis, 1, sizeof(vis));
    long long sum = 0;
    for (int i = 0; i <= m - 1; i++) {
      scanf("%d %d %d", &q[i].a, &q[i].b, &q[i].c);
    }
    for (int i = 0; i <= m - 1;) {
      if (vis[i]) {
        for (int j = 0; j <= m - 1;) {
          if (vis[j]) {
            if (q[i].b == q[j].a && q[i].a != q[j].b) {
              if (q[i].c <= q[j].c) {
                q[j].c = q[j].c - q[i].c;
                q[i].b = q[j].b;
              } else {
                q[i].c = q[i].c - q[j].c;
                q[j].a = q[i].a;
              }
              if (q[j].c == 0) vis[j] = 0;
              if (q[i].c == 0) vis[i] = 0;
            } else if (q[i].b == q[j].a && q[i].a == q[j].b) {
              if (q[i].c <= q[j].c) {
                q[j].c = q[j].c - q[i].c;
                q[i].c = 0;
              } else {
                q[i].c = q[i].c - q[j].c;
                q[j].c = 0;
              }
              if (q[j].c == 0) vis[j] = 0;
              if (q[i].c == 0) vis[i] = 0;
            }
            j++;
          } else
            j++;
        }
        i++;
      } else
        i++;
    }
    for (int i = 0; i <= m - 1; i++) sum += q[i].c;
    cout << sum << endl;
  }
  return 0;
}
