#include <bits/stdc++.h>
using namespace std;
int matrix[100][100] = {0};
int n, m;
int a, b, c;
void input() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> a >> b >> c;
    matrix[a - 1][b - 1] = c;
  }
}
void rearrangeDebt() {
  for (int k = 0; k < n; ++k) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        if (i == j || i == k || j == k) continue;
        if (matrix[i][k] > matrix[k][j]) {
          matrix[i][j] += matrix[k][j];
          matrix[i][k] -= matrix[k][j];
          matrix[k][j] = 0;
        } else {
          matrix[i][j] += matrix[i][k];
          matrix[k][j] -= matrix[i][k];
          matrix[i][k] = 0;
        }
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (matrix[i][j] > matrix[j][i]) {
        matrix[i][j] -= matrix[j][i];
        matrix[j][i] = 0;
      } else {
        matrix[j][i] -= matrix[i][j];
        matrix[i][j] = 0;
      }
    }
  }
}
int findOptimalDebt() {
  int optimal = 0;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) optimal += matrix[i][j];
  return optimal;
}
int main() {
  input();
  rearrangeDebt();
  cout << findOptimalDebt();
}
