PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000048. Một hoặc hai (1 or 2)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Một hoặc hai (1 or 2)

1 or 2

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn face-down cards numbered from 11 to nn. Each card ii contains an unknown value AiA_i equal to either 11 or 22. You do not know these values, but you are given some relations between them.

There are mm constraints. Constraint ii states that AXi+AYi+ZiA_{X_i}+A_{Y_i}+Z_i is even. It is guaranteed that at least one assignment of A1,…,AnA_1,\ldots,A_n satisfies all constraints. At a cost of 11, you may turn over any card to learn its exact value.

Find the minimum number of cards you must reveal so that the observed values together with the known constraints determine all nn card values with certainty, regardless of the initially valid assignment.

Input

The first line contains n,mn,m; each of the next mm lines contains Xi,Yi,ZiX_i,Y_i,Z_i, with Xi<YiX_i<Y_i and distinct pairs.

Output

Print one integer, the minimum total cost.

Subtasks

  • Subtask 1 (20 points): 2≤n≤252\le n\le25, 1≤m≤3001\le m\le300; 1≤Xi<Yi≤n1\le X_i<Y_i\le n, 1≤Zi≤1001\le Z_i\le100; pairs (Xi,Yi)(X_i,Y_i) are distinct; all constraints can be satisfied together.
  • Subtask 2 (30 points): 2≤n≤20002\le n\le2000, 1≤m≤1051\le m\le10^5; 1≤Xi<Yi≤n1\le X_i<Y_i\le n, 1≤Zi≤1001\le Z_i\le100; pairs (Xi,Yi)(X_i,Y_i) are distinct; all constraints can be satisfied together.
  • Subtask 3 (50 points): 2≤n≤1052\le n\le10^5, 1≤m≤1051\le m\le10^5; 1≤Xi<Yi≤n1\le X_i<Y_i\le n, 1≤Zi≤1001\le Z_i\le100; pairs (Xi,Yi)(X_i,Y_i) are distinct; all constraints can be satisfied together.

Examples

Input

6 5
1 2 1
2 3 2
1 3 3
4 5 4
5 6 5

Output

2

Explanation

The constraints connect cards in two separate groups: {1,2,3}\{1,2,3\} and {4,5,6}\{4,5,6\}. Knowing the value of one card in each group determines all remaining values through the given relations. No relation connects the two groups, so one revealed card is insufficient. The answer is 2.