#P2024. Animal Food Chain

    ID: 15306 Type: Default 1000ms 256MiB

Animal Food Chain

Animal Food Chain

In the Animal Kingdom, there are three kinds of animals: A, B, and C. Their food chain forms an interesting cycle: (A) eats (B), (B) eats (C), and (C) eats (A). Given (N) animals numbered from 1 to (N), each animal is of one type (A, B, or C), but the exact type is unknown. We are provided with (K) statements that describe the relationships among these animals using two formats:

  • 1 X Y: Indicates that animal \(X\) and animal \(Y\) are of the same type.
  • 2 X Y: Indicates that animal \(X\) eats animal \(Y\).
A statement is considered false if any of the following conditions hold:
  1. The statement contradicts any previously accepted (true) statements.
  2. Either \(X\) or \(Y\) is greater than \(N\).
  3. The statement suggests that an animal eats itself (i.e. \(X\) eats \(X\)).
Otherwise, the statement is regarded as true. Your task is to count the total number of false statements based on the provided input.

inputFormat

The input begins with a line containing two integers (N) and (K) separated by a space. This is followed by (K) lines, each containing three integers. The first integer is either 1 or 2 representing the type of statement, and the next two integers (X) and (Y) correspond to the animal numbers involved in the statement.

outputFormat

Output a single integer representing the total number of false statements.

sample

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

</p>