시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 512 MB142494131.538%

문제

You and your friends have just returned from a beautiful vacation in the mountains of the Netherlands. When on vacation, it’s annoying to split the bill on every expense every time, so you just kept all the receipts from the vacation, and wrote down who paid how much for who. Now, it is time to settle the bill.

You could each take all the receipts showing that someone paid something for you, and then pay that person back. But then you would need a lot of transactions, and you want to keep up the lazy spirit from your trip. In the end, it does not matter who transfers money to whom; as long as in the end, everyone’s balance is 0.

Can you figure out the least number of transactions needed to settle the score? Assume everyone has enough spare cash to transfer an arbitrary amount of money to another person.

입력

Input consists of

  • A line containing two integers M, the number of people in the group, with 1 ≤ M ≤ 20, and N, the number of receipts from the trip, with 0 ≤ N ≤ 1000.
  • N lines, each with three integers a, b, p, where 0 ≤ a, b < M, and 1 ≤ p ≤ 1000, signifying a receipt showing that person a paid p euros for person b.

출력

Output a single line containing a single integer, the least number of transactions necessary to settle all bills.

예제 입력 1

4 2
0 1 1
2 3 1

예제 출력 1

2

예제 입력 2

5 5
0 1 3
1 2 3
2 3 3
3 4 3
4 0 3

예제 출력 2

0

예제 입력 3

5 4
0 1 1
0 2 1
0 3 1
0 4 1

예제 출력 3

4