#t530. 食物链

食物链

Food Chain

Special for beginners, ^_^

Problem Description

In the animal kingdom there are three types of animals A,B,CA,B,C, and their food chain forms an interesting cycle: AA eats BB, BB eats CC, and CC eats AA.

There are NN animals numbered 1∼N1\sim N. Each animal is of one of the types A,B,CA,B,C, but we do not know which type each actually is.

Someone describes the food-chain relationships among these NN animals using two kinds of statements:

The first kind is 1 X Y, meaning XX and YY are of the same type.

The second kind is 2 X Y, meaning XX eats YY.

Regarding the NN animals, the person utters KK statements one after another in the two forms above; some of these KK statements are true and some are false. A statement is false (otherwise it is true) if any of the following three conditions holds:

  1. The current statement conflicts with some earlier true statement;
  2. In the current statement, XX or YY is greater than NN;
  3. The current statement says that XX eats XX.

Your task is: given NN (1≤N≤50,0001\le N\le50{,}000) and the KK statements (0≤K≤100,0000\le K\le100{,}000), output the total number of false statements.

Input Format

The first line contains two integers NN and KK separated by a space.

The next KK lines each contain three positive integers D,X,YD,X,Y separated by spaces, where DD indicates the kind of statement.

If D=1D=1, then XX and YY are of the same type.

If D=2D=2, then XX eats YY.

Output Format

A single integer: the number of false statements.

Sample Input 1

100 7
1 101 1
2 1 2
2 2 3
2 3 3
1 1 3
2 3 1
1 5 5

Sample Output 1

3

Hint

$$\begin{matrix} 100&7\\\\1&101&1&false\\\\2&1&2&true\\\\2&2&3&true\\\\2&3&3&false\\\\1&1&3&false\\\\2&3&1&true\\\\1&5&5&true \end{matrix}$$

Constraints

1s, 1024KiB for each test case.