#!/usr/bin/env rdmd
import std.stdio, std.string, std.conv;
import std.algorithm, std.array;

void main()
{
    for(string S; (S=readln().chomp()).length; )
    {
        auto nm = S.split().map!(to!int)();
        auto n=nm[0], m=nm[1];
        auto d = new int[][](n,n);
        foreach(i;0..m)
        {
            auto abc = readln().split().map!(to!int)();
            d[abc[0]-1][abc[1]-1]=abc[2];
        }
        foreach(k;0..n)
            foreach(i;0..n)
                foreach(j;0..n)
                {
                    auto c = min(d[i][k],d[k][j]);
                    d[i][j] += c;
                    d[i][k] -= c;
                    d[k][j] -= c;
                }
        auto t = 0;
        foreach(i;0..n)
            foreach(j;0..n)
                t += d[i][j];
        writeln(t);
    }
}
