Imagine that there is a group of three friends: A, B and С. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.

This task is a generalisation of a described example. Imagine that your group of friends has n people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.


-----Input-----

The first line contains two integers n and m (1 ≤ n ≤ 100; 0 ≤ m ≤ 10^4). The next m lines contain the debts. The i-th line contains three integers a_{i}, b_{i}, c_{i} (1 ≤ a_{i}, b_{i} ≤ n; a_{i} ≠ b_{i}; 1 ≤ c_{i} ≤ 100), which mean that person a_{i} owes person b_{i} c_{i} rubles.

Assume that the people are numbered by integers from 1 to n.

It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (x, y) and pair of people (y, x).


-----Output-----

Print a single integer — the minimum sum of debts in the optimal rearrangement.


-----Examples-----
Input
5 3
1 2 10
2 3 1
2 4 1

Output
10

Input
3 0

Output
0

Input
4 3
1 2 1
2 3 1
3 1 1

Output
0



-----Note-----

In the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.

In the second sample, there are no debts.

In the third sample, you can annul all the debts.

Source: codeforces | Difficulty: EASY
Tags: Implementation


Solutions:
(n, m) = map(int, input().split())
debt = [0] * (n + 1)
for i in range(m):
	(a, b, c) = map(int, input().split())
	debt[a] -= c
	debt[b] += c
ans = 0
for i in debt:
	if i > 0:
		ans += i
print(ans)


(n, m) = map(int, input().split())
baaki = [0] * 15000
for i in range(m):
	(x, y, cbaaki) = map(int, input().split())
	baaki[x] += cbaaki
	baaki[y] -= cbaaki
sm = 0
for i in baaki:
	if i > 0:
		sm += i
print(sm)


(n, m) = list(map(int, input().split()))
ms = [[0, 0] for i in range(n)]
for i in range(m):
	(a, b, c) = list(map(int, input().split()))
	ms[a - 1][0] += c
	ms[b - 1][1] += c
sol = 0
for i in range(n):
	sol += max(ms[i][0] - ms[i][1], 0)
print(sol)


(n, m) = map(int, input().split())
l = [0] * 102
for i in range(m):
	(a, b, c) = map(int, input().split())
	l[a] += c
	l[b] -= c
sum = 0
for i in range(n + 1):
	if l[i] > 0:
		sum += l[i]
print(sum)


import sys

def input():
	return sys.stdin.readline().strip()
(n, m) = map(int, input().split())
val = [0 for i in range(n + 1)]
for i in range(m):
	(a, b, c) = map(int, input().split())
	val[a] += c
	val[b] -= c
count = 0
for i in range(1, n + 1):
	if val[i] > 0:
		count += val[i]
print(count)


def debt(n, r):
	for i in range(n):
		(a, b, c) = [int(i) for i in input().split()]
		r[a - 1] -= c
		r[b - 1] += c
	r = [abs(i) for i in r]
	return int(sum(r) / 2)

def main():
	(n, m) = [int(i) for i in input().split()]
	r = [0 for i in range(n)]
	print(debt(m, r))
main()


(n, m) = map(int, input().split())
ans = 0
w = [0] * n
while m:
	(a, b, c) = map(int, input().split())
	a -= 1
	b -= 1
	m -= 1
	w[a] += c
	w[b] -= c
for val in w:
	if val > 0:
		ans += val
print(ans)


(n, m) = map(int, input().split())
xs = [0] * n
for _ in range(m):
	(a, b, c) = map(int, input().strip().split())
	xs[a - 1] -= c
	xs[b - 1] += c
print(sum((x for x in xs if x > 0)))


from collections import defaultdict
debts = defaultdict()
(n, m) = map(int, input().split())
for i in range(n):
	debts[i + 1] = 0
for _ in range(m):
	(a, b, c) = map(int, input().split())
	debts[a] -= c
	debts[b] += c
sum = 0
for each in debts.values():
	if each > 0:
		sum += each
print(sum)


(n, m) = [int(x) for x in input().split()]
gives = [0 for i in range(n + 1)]
receives = [0 for i in range(n + 1)]
for i in range(m):
	(a, b, c) = [int(x) for x in input().split()]
	gives[a] += c
	receives[b] += c
opt = 0
for i in range(n + 1):
	diff = gives[i] - receives[i]
	if diff > 0:
		opt += diff
print(opt)


(n, m) = map(int, input().split(' '))
have = (n + 1) * [0]
for _ in range(m):
	(person_1, person_2, owns_) = map(int, input().split(' '))
	have[person_1] += owns_
	have[person_2] -= owns_
print(sum((i for i in have if i > 0)))


(n, m) = map(int, input().split())
abc = [list(map(int, input().split())) for i in range(m)]
mat = [[0] * n for i in range(n)]
for t in abc:
	mat[t[0] - 1][t[1] - 1] = t[2]
total = 0
for i in range(n):
	s = sum(mat[i][:]) - sum([mat[x][i] for x in range(n)])
	if s < 0:
		continue
	total += s
print(total)


(n, m) = map(int, input().split())
mas = [0] * (n + 1)
for i in range(m):
	(a, b, c) = map(int, input().split())
	mas[b] += c
	mas[a] -= c
sum = 0
for i in mas:
	sum += max(i, -i)
print(int(sum / 2))


(n, m) = [int(i) for i in input().split()]
dicDev = {}
dicRec = {}
final = 0
for i in range(1, n + 1):
	dicDev[i] = 0
	dicRec[i] = 0
for _ in range(m):
	(a, b, c) = [int(i) for i in input().split()]
	dicDev[a] += c
	dicRec[b] += c
for i in range(1, n + 1):
	re = dicDev[i] - dicRec[i]
	if re > 0:
		final += re
print(final)


R = lambda : list(map(int, input().split()))
(n, m) = R()
p = [0] * n
for i in range(m):
	(a, b, c) = R()
	p[a - 1] -= c
	p[b - 1] += c
print(sum((i for i in p if i > 0)))


import sys

def ints_input():
	return [int(i) for i in sys.stdin.readline().strip('\n').split(' ')]

def print_list(arr):
	sys.stdout.writelines((str(x) + ' ' for x in arr))
	sys.stdout.write('\n')

def fast_input(type=str):
	return type(sys.stdin.readline().strip('\n'))
(n, m) = ints_input()
depts = [0 for j in range(109)]
for _ in range(m):
	(i, j, k) = ints_input()
	depts[i] += k
	depts[j] -= k
print(sum(filter(lambda x: x > 0, depts)))


from sys import stdin, stdout
from os import path
rd = lambda : stdin.readline().strip()
wr = stdout.write
if path.exists('input.txt'):
	stdin = open('input.txt', 'r')
(x, y) = map(int, rd().split())
out = [0] * (x + 1)
for _ in range(y):
	(a, b, c) = map(int, rd().split())
	out[a] -= c
	out[b] += c
sum = 0
for i in out:
	if i >= 0:
		sum += i
wr(str(sum) + '\n')


(n, m) = [int(x) for x in input().split()]
recebe = n * [0]
paga = n * [0]
for i in range(m):
	(a, b, c) = [int(x) for x in input().split()]
	paga[a - 1] += c
	recebe[b - 1] += c
sum = 0
for i in range(n):
	saldo = recebe[i] - paga[i]
	if saldo > 0:
		sum += saldo
print(sum)


(n, m) = [int(x) for x in input().split()]
listaDividas = []
for i in range(101):
	listaDividas.append([0, 0])
for i in range(m):
	(individado, agiota, quantidade) = [int(x) for x in input().split()]
	listaDividas[individado][0] += quantidade
	listaDividas[agiota][1] += quantidade
quantidadeTotal = 0
for i in range(101):
	quantidadeTotal += max(listaDividas[i][0] - listaDividas[i][1], 0)
print(quantidadeTotal)


def inp():
	return map(int, input().split())
from collections import *
mem = Counter()
(n, m) = inp()
for i in range(m):
	(a, b, c) = inp()
	mem[a] -= c
	mem[b] += c
mem += Counter()
print(sum(mem.values()))


(n, m) = map(int, input().split())
arr = [[0] * n for i in range(n)]
for i in range(m):
	(a, b, c) = map(int, input().split())
	arr[a - 1][b - 1] += c
	arr[b - 1][a - 1] -= c
cnt = 0
for i in range(n):
	t = 0
	for j in range(n):
		t += arr[i][j]
	cnt += abs(t)
print(cnt // 2)


(n, m) = map(int, input().split())
wallet = []
for _ in range(1, n + 2):
	wallet.append(0)
for _ in range(m):
	(a, b, c) = map(int, input().split())
	wallet[a] -= c
	wallet[b] += c
totalDebt = 0
for e in wallet:
	if e < 0:
		totalDebt += -e
print(totalDebt)


(n, m) = map(int, input().split())
in_c = [0 for i in range(n)]
out_c = [0 for i in range(n)]
for i in range(m):
	(a, b, c) = map(int, input().split())
	in_c[b - 1] += c
	out_c[a - 1] += c
total = 0
for i in range(n):
	if in_c[i] > out_c[i]:
		total += in_c[i] - out_c[i]
print(total)


from collections import defaultdict
(n, m) = map(int, input().split())
s = defaultdict(lambda : 0)
for i in range(m):
	(a, b, c) = map(int, input().split())
	s[a] -= c
	s[b] += c
print(sum((i for i in s.values() if i > 0)))


class CodeforcesTask376BSolution:

	def __init__(self):
		self.result = ''
		self.n_m = []
		self.debts = []

	def read_input(self):
		self.n_m = [int(x) for x in input().split(' ')]
		for x in range(self.n_m[1]):
			self.debts.append([int(y) for y in input().split(' ')])

	def process_task(self):
		people_balance = [0] * self.n_m[0]
		for d in self.debts:
			people_balance[d[0] - 1] -= d[2]
			people_balance[d[1] - 1] += d[2]
		self.result = str(sum([abs(x) for x in people_balance]) // 2)

	def get_result(self):
		return self.result
Solution = CodeforcesTask376BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())


from sys import stdin
(a, b) = map(int, stdin.readline().split())
z = [0] * a
for _ in ' ' * b:
	(u, v, w) = map(int, stdin.readline().split())
	z[u - 1] += w
	z[v - 1] -= w
print(sum((max(0, z[i]) for i in range(a))))


(n, m) = map(int, input().split())
debts = [0] * n
credit = [0] * n
for cont in range(0, m):
	(x, y, c) = map(int, input().split())
	debts[x - 1] += c
	credit[y - 1] += c
total = [0] * n
for cont in range(0, n):
	total[cont] = debts[cont] - credit[cont]
ris = 0
for n in total:
	if n > 0:
		ris += n
print(ris)


from sys import stdin, stdout
from os import path
rd = lambda : stdin.readline().strip()
wr = stdout.write
if path.exists('input.txt'):
	stdin = open('input.txt', 'r')
import time
(x, y) = map(int, rd().split())
amr = dict()
for _ in range(y):
	(a, b, c) = map(int, rd().split())
	if a in amr:
		amr[a] -= c
	else:
		amr[a] = -1 * c
	if b in amr:
		amr[b] += c
	else:
		amr[b] = c
out = 0
for (key, val) in amr.items():
	if val > 0:
		out += val
print(out)


def solve(n, memo):
	for debitor in range(n):
		for creditor in range(n):
			fst_debit = memo[debitor][creditor]
			if fst_debit > 0:
				for (i, snd_debit) in enumerate(memo[creditor]):
					_min = min(fst_debit, snd_debit)
					memo[debitor][i] += _min
					memo[debitor][creditor] -= _min
					memo[creditor][i] -= _min
					fst_debit -= _min
	ans = 0
	for i in range(n):
		for j in range(n):
			if i != j:
				ans += memo[i][j]
	return ans

def main():
	(n, m) = map(int, input().split())
	memo = [[0] * n for _ in range(n)]
	for _ in range(m):
		(x, y, c) = map(int, input().split())
		memo[x - 1][y - 1] = c
	ans = solve(n, memo)
	print(ans)
main()


def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def loadinput():
	(n, m) = map(int, input().split())
	debt = [0] * (n + 1)
	ows = [0] * (n + 1)
	for i in range(m):
		(x, y, z) = map(int, input().split())
		debt[y] += z
		ows[x] += z
	return (m, debt, ows)
(m, debt, ows) = loadinput()
ans = 0
if m == 0:
	print(0)
else:
	for i in range(len(debt)):
		if debt[i] < ows[i]:
			ans += ows[i] - debt[i]
	print(ans)


(n, m) = map(int, input().split())
d = [0] * n
for i in range(m):
	(a, b, debt) = map(int, input().split())
	d[a - 1] += debt
	d[b - 1] -= debt
s = 0
for i in d:
	if i > 0:
		s += i
print(s)


(n, k) = map(int, input().split())
d = [0] * n
ans = 0
for _ in range(k):
	(a, b, c) = map(int, input().split())
	a -= 1
	b -= 1
	d[a] += c
	d[b] -= c
for i in d:
	if i > 0:
		ans += i
print(ans)


(n, m) = map(int, input().split())
ow = [0] * (n + 1)
deb = [0] * (n + 1)
for i in range(m):
	(a, b, c) = map(int, input().split())
	ow[a] += c
	deb[b] += c
sum = 0
for i in range(1, n + 1):
	if ow[i] > deb[i]:
		sum += ow[i] - deb[i]
print(sum)


from sys import stdin
(n, m) = map(int, input().split())
owes = [0 for i in range(n + 1)]
for i in range(m):
	(a, b, c) = map(int, input().split())
	owes[a] += c
	owes[b] -= c
sol = 0
for owe in owes:
	if owe > 0:
		sol += owe
print(sol)


from sys import stdin
(a, b) = map(int, stdin.readline().split())
z = [[0] * 101 for i in ' ' * 101]
for _ in ' ' * b:
	(u, v, w) = map(int, stdin.readline().split())
	z[u][v] += w
for i in range(1, 101):
	for j in range(1, 101):
		if z[i][j]:
			for k in range(1, 101):
				if i == k:
					m = min(z[i][j], z[j][k])
					z[i][j] -= m
					z[j][k] -= m
					continue
				m = min(z[j][k], z[i][j])
				z[i][k] += m
				z[j][k] -= m
				z[i][j] -= m
s = 0
for i in range(1, 101):
	s += sum(z[i])
print(s)


(n, m) = map(int, input().split())
lis = [0] * (n + 1)
for i in range(m):
	(a, b, c) = map(int, input().split())
	lis[a] -= c
	lis[b] += c
sum_ = 0
for i in lis:
	if i >= 0:
		sum_ += i
print(sum_)


sumi = 0
(n, m) = map(int, input().strip().split())
debt = [0] * (n + 1)
crdt = [0] * (n + 1)
for i in range(0, m):
	(a, b, c) = map(int, input().strip().split())
	debt[a] += c
	crdt[b] += c
for i in range(0, len(debt)):
	if crdt[i] > debt[i]:
		sumi += crdt[i] - debt[i]
print(sumi)


(n, k) = map(int, input().split())
owed = [0 for _ in range(n)]
owes = [0 for _ in range(n)]
for i in range(k):
	(a, b, c) = map(int, input().split())
	owed[b - 1] += c
	owes[a - 1] += c
sum = 0
for i in range(n):
	if owes[i] > owed[i]:
		sum += owes[i] - owed[i]
print(sum)


(n, m) = map(int, input().split())
arr = [0] * n
for i in range(m):
	(a, b, c) = map(int, input().split())
	a -= 1
	b -= 1
	arr[b] += c
	arr[a] -= c
s = 0
for e in arr:
	if e < 0:
		s += -e
print(s)


(n, m) = map(int, input().split())
p = [0] * (n + 1)
for i in range(m):
	(a, b, d) = map(int, input().split())
	p[a] += d
	p[b] -= d
print(sum((abs(i) for i in p)) // 2)


from collections import defaultdict as dd
d = dd(lambda : 0)
from sys import stdin
stdin.readline

def mp():
	return list(map(int, stdin.readline().strip().split()))

def it():
	return int(stdin.readline().strip())
from math import pi, sqrt
(n, m) = mp()
try:
	ans = 0
	for _ in range(m):
		(a, b, deb) = mp()
		d[a] += deb
		d[b] -= deb
	for i in d:
		if d[i] > 0:
			ans += d[i]
	print(ans)
except:
	print(0)


def main():
	(n, m) = map(int, input().split())
	a = [0] * n
	for i in range(m):
		(x, y, z) = map(int, input().split())
		a[x - 1] -= z
		a[y - 1] += z
	ans = 0
	for i in a:
		if i > 0:
			ans += i
	print(ans)
main()


from collections import defaultdict
(n, m) = list(map(int, input().split()))
friends_money = defaultdict(int)
for friend in range(1, n + 1):
	friends_money[friend]
for i in range(m):
	(f1, f2, money) = list(map(int, input().split()))
	friends_money[f1] = friends_money[f1] - money
	friends_money[f2] = friends_money[f2] + money
debt = 0
for money in friends_money.values():
	if money < 0:
		debt = debt + abs(money)
print(debt)


(a, b) = map(int, input().split())
c = [0] * a
for i in range(b):
	(d, e, f) = map(int, input().split())
	c[d - 1] += f
	c[e - 1] -= f
g = 0
for i in c:
	if i > 0:
		g += i
print(g)


x = [0] * 101
y = [0] * 101
(n, m) = map(int, input().split())
ans = 0
for i in range(m):
	(a, b, c) = map(int, input().split())
	x[a] += c
	y[b] += c
for i in range(1, n + 1):
	if y[i] >= x[i]:
		ans += y[i] - x[i]
print(ans)


def I():
	return list(map(int, input().split()))
(n, m) = I()
list_of_persons = [0] * (n + 1)
sum = 0
for i in range(m):
	(x, y, z) = I()
	list_of_persons[x] += z
	list_of_persons[y] -= z
for i in range(n + 1):
	if list_of_persons[i] > 0:
		sum += list_of_persons[i]
print(sum)


(n, m) = map(int, input().split())
f = [[0 for _ in range(n)] for _ in range(n)]
for i in range(m):
	(a, b, c) = map(int, input().split())
	f[a - 1][b - 1] = c
for i in range(n):
	for j in range(n):
		if f[i][j] > 0:
			for k in range(n):
				if f[j][k] > 0:
					buf = min(f[i][j], f[j][k])
					f[i][j] -= buf
					f[j][k] -= buf
					f[i][k] += buf
for i in range(n):
	f[i][i] = 0
print(sum(map(sum, f)))


(n, p) = list(map(int, input().split()))
dic = {}
for i in range(n):
	dic[i + 1] = 0
for _ in range(p):
	(src, dest, size) = list(map(int, input().split()))
	dic[src] -= size
	dic[dest] += size
resp = 0
for i in dic:
	if dic[i] > 0:
		resp += dic[i]
print(resp)


import math
import random

def main(arr, n):
	ans = 0
	owe = [0] * (n + 1)
	for i in range(len(arr)):
		(a, b, c) = arr[i]
		owe[a] += c
		owe[b] -= c
	return sum([x for x in owe if x > 0])
(n, m) = list(map(int, input().split()))
arr = []
for i in range(m):
	arr.append(list(map(int, input().split())))
print(main(arr, n))


(n, m) = [int(i) for i in input().split()]
debts = [0 for _ in range(n)]
for _ in range(m):
	(a, b, c) = [int(i) for i in input().split()]
	debts[a - 1] -= c
	debts[b - 1] += c
res = 0
for p in debts:
	if p > 0:
		res += p
print(res)


import sys

def readInputs():
	global n, m, lAdj, counts
	(n, m) = map(int, f.readline().split())
	lAdj = [[] for _ in range(n)]
	counts = n * [0]
	for _ in range(m):
		(a, b, c) = map(int, f.readline().split())
		lAdj[a - 1] += [(b - 1, c)]

def solve():
	if m == 0:
		return 0
	for i in range(n):
		for cple in lAdj[i]:
			succ = cple[0]
			debt = cple[1]
			counts[i] -= debt
			counts[succ] += debt
	sum = 0
	for i in range(n):
		if counts[i] < 0:
			sum += abs(counts[i])
	return sum

def tests():
	global f
	bFail = False
	for i in range(1, 3):
		f = open('test' + str(i) + '.txt')
		readInputs()
		lEx = list(map(int, f.readline().split()))
		lEx.sort()
		resExpect = str(lEx)
		res = str(solve())
		if res != resExpect:
			print('TEST', i, 'FAILED !' + ' Found :', res, ', Expected :', resExpect)
			bFail = True
		else:
			print('TEST', i, 'PASSED')
	if not bFail:
		print('\n--- ALL OF THE ' + str(i) + ' TESTS HAVE SUCCEEDED ---')

def main():
	global f
	i = 2
	if i == 1:
		tests()
		return
	f = sys.stdin
	readInputs()
	print(solve())
main()


(n, m) = map(int, input().split())
debts = [0] * (n + 1)
for i in range(m):
	(a, b, debt) = map(int, input().split())
	debts[a] -= debt
	debts[b] += debt
total = 0
for i in range(n):
	if debts[i + 1] > 0:
		total += debts[i + 1]
print(total)


(n, m) = map(int, input().split())
vetor = [0 for i in range(n + 1)]
for i in range(m):
	(a, b, c) = map(int, input().split())
	vetor[a] -= c
	vetor[b] += c
print(abs(sum(filter(lambda x: x <= 0, vetor))))


(n, m) = map(int, input().split())
dep = [0] * 10004
owe = [0] * 10004
for i in range(m):
	temp = list(map(int, input().split()))
	dep[temp[1]] += temp[2]
	owe[temp[0]] += temp[2]
sum = 0
for i in range(0, n + 1):
	if dep[i] < owe[i]:
		sum += owe[i] - dep[i]
print(sum)


(n, m) = map(int, input().split())
p = [0] * (n + 1)
for i in range(m):
	(a, b, c) = map(int, input().split())
	(p[a], p[b]) = (p[a] - c, p[b] + c)
print(sum((x for x in p if x > 0)))


(n, m) = map(int, input().split())
matrix = []
for row in range(n):
	matrix.append([0] * n)
for _ in range(m):
	(a, b, c) = map(int, input().split())
	matrix[a - 1][b - 1] = c
for x in range(n):
	for y in range(n):
		for z in range(n):
			a = matrix[x][y]
			b = matrix[y][z]
			c = matrix[x][z]
			if min(a, b) > 0:
				if a >= b:
					matrix[y][z] = 0
					matrix[x][y] = a - b
					matrix[x][z] += b
				else:
					matrix[x][y] = 0
					matrix[x][z] += a
					matrix[y][z] = b - a
s = 0
for i in range(n):
	for j in range(n):
		if i != j:
			s += matrix[i][j]
print(s)


lt = list(input().split())
(n, m) = (int(lt[0]), int(lt[1]))
D = {}
for i in range(1, n + 1):
	D[i] = 0
for i in range(m):
	lt = list(input().split())
	D[int(lt[0])] -= int(lt[2])
	D[int(lt[1])] += int(lt[2])
tot = 0
for i in D.values():
	if i < 0:
		tot += -i
print(tot)


(n, m) = map(int, input().split())
arr = [None] + [0 for i in range(n)]
for _ in range(m):
	(a, b, c) = map(int, input().split())
	arr[b] += c
	arr[a] += -c
final = 0
for i in range(1, n + 1):
	if arr[i] >= 0:
		final += arr[i]
print(final)


(n, m) = map(int, input().split())
owe = [0] * (n + 1)
for _ in range(m):
	(a, b, c) = map(int, input().split())
	owe[a] += c
	owe[b] -= c
s = 0
for i in owe:
	if i > 0:
		s += i
print(s)


(n, m) = map(int, input().split())
e = [[0] * n for i in range(n)]
for i in range(m):
	(a, b, c) = map(int, input().split())
	e[a - 1][b - 1] += c
	e[b - 1][a - 1] -= c
cnt = 0
for i in range(n):
	cnt1 = 0
	for j in range(n):
		cnt1 += e[i][j]
	cnt += abs(cnt1)
print(cnt // 2)


(n, m) = input().strip().split(' ')
(n, m) = [int(n), int(m)]
ag = [0 for _ in range(n)]
for i in range(m):
	(a, b, c) = input().strip().split(' ')
	(a, b, c) = [int(a), int(b), int(c)]
	ag[a - 1] += c
	ag[b - 1] -= c
sum = 0
for i in range(n):
	if ag[i] > 0:
		sum += ag[i]
print(sum)


data = input().split(' ')
n = int(data[0])
m = int(data[1])
debts = []
total = 0
for i in range(n):
	debts.append(0)
for i in range(m):
	data = input().split(' ')
	data = [int(x) for x in data]
	debts[data[0] - 1] -= data[2]
	debts[data[1] - 1] += data[2]
for i in range(len(debts)):
	if debts[i] < 0:
		total += debts[i] * -1
print(total)


import math

def facts(n):
	ans = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			ans.append(i)
			ans.append(n // i)
	ans = sorted(list(dict.fromkeys(ans)))
	if ans[-1] == n:
		ans = ans[:-1]
	return ans
(n, m) = map(int, input().split())
d = {}
ans = 0
for i in range(1, 101):
	d[i] = 0
for i in range(m):
	(a, b, c) = map(int, input().split())
	d[a] -= c
	d[b] += c
vals = sorted(d.values(), reverse=True)
for i in range(n):
	if vals[i] > 0:
		ans += vals[i]
	else:
		break
print(ans)


(n, m) = map(int, input().split())
l = [0 for i in range(n + 3)]
for i in range(m):
	(x, y, c) = map(int, input().split())
	l[x] += c
	l[y] -= c
s = 0
for i in l:
	s += max(i, 0)
print(s)


__author__ = 'asmn'
(n, m) = tuple(map(int, input().split()))
l = [0] * n
for i in range(m):
	(a, b, c) = tuple(map(int, input().split()))
	l[a - 1] += c
	l[b - 1] -= c
print(sum((abs(x) for x in l)) // 2)


(numPeople, numDebts) = tuple(map(int, input().split(' ')))
totalBalance = numPeople * [0]
for m in range(numDebts):
	(A, B, amount) = tuple(map(int, input().split(' ')))
	totalBalance[A - 1] -= amount
	totalBalance[B - 1] += amount
totalDebt = 0
for t in totalBalance:
	if t < 0:
		totalDebt += -t
print(totalDebt)


row = input().split()
n = int(row[1])
matriz = [0] * 200
total = 0
for _ in range(n):
	debitos = input().split()
	matriz[int(debitos[0])] -= int(debitos[2])
	matriz[int(debitos[1])] += int(debitos[2])
for i in matriz:
	if i < 0:
		total += i
print(str(abs(total)))


row = input().split(' ')
a = [0] * 101
for _ in range(int(row[1])):
	debts = input().split(' ')
	a[int(debts[0])] -= int(debts[2])
	a[int(debts[1])] += int(debts[2])
totalDebts = 0
for i in a:
	if i < 0:
		totalDebts += i
print(str(abs(totalDebts)))


import sys
import math
import collections
import bisect
from collections import deque as queue

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()
for t in range(1):
	(n, m) = get_ints()
	arr = [0] * n
	for i in range(m):
		(a, b, c) = get_ints()
		arr[a - 1] -= c
		arr[b - 1] += c
	ans = 0
	for i in arr:
		if i > 0:
			ans += i
	print(ans)


(n, m) = map(int, input().split())
(l, p) = ([0] * (n + 1), 0)
for i in range(m):
	(a, b, c) = map(int, input().split())
	l[a] -= c
	l[b] += c
for i in range(n + 1):
	p += abs(l[i]) if l[i] < 0 else 0
print(p)


import os
import re
import sys
from bisect import bisect, bisect_left, insort, insort_left
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from io import BytesIO, IOBase
from itertools import accumulate, combinations, combinations_with_replacement, groupby, permutations, product
from math import acos, asin, atan, ceil, cos, degrees, factorial, hypot, log2, pi, radians, sin, sqrt, tan
from operator import itemgetter, mul
from string import ascii_lowercase, ascii_uppercase, digits

def inp():
	return int(input())

def inlist():
	return list(map(int, input().split()))

def instr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())

def found(i, a):
	if a[i:i + 4] == 'bear':
		return True
	return False

def main():
	(n, m) = invr()
	track = [0] * (n + 1)
	for _ in range(m):
		(a, b, c) = invr()
		track[a] += c
		track[b] -= c
	res = 0
	for i in track:
		if i > 0:
			res += i
	print(res)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
main()


z = list(map(int, input().split()))
lst = [0] * z[0]
ans = 0
for i in range(z[1]):
	x = list(map(int, input().split()))
	lst[x[0] - 1] -= x[2]
	lst[x[1] - 1] += x[2]
for j in lst:
	if j > 0:
		ans += j
print(ans)