Food Chain

Count records that are invalid or contradict earlier records, treating same-kind and eats relations consistently.

Medium7Union-findGraphNo attempts yetTime limit2sMemory limit512 MB

Problem

N animals live on planet Zeta. Minho numbered them 1, 2, ..., N to tell them apart. Every animal on the planet is one of three kinds, A, B, or C, and A eats B, B eats C, and C eats A.

Minho watched the planet for a long time and now wants to draw an ecology map from the records he kept. Each record is one of two kinds.

  • Type 1: x and y are the same kind.
  • Type 2: x eats y.

Minho reads the records in order, from record 1 to record K, and fills in the map as he goes. Some records name an x or a y that is not an animal number, and some records contradict the part of the map he has already filled in. When Minho meets such a record he skips it and does not put it on the map. A skipped record has no effect on how the following records are judged.

Write a program that counts the records Minho has to skip.

Input

The first line contains N and K separated by a space (1N50,0001 \le N \le 50{,}000, 0K100,0000 \le K \le 100{,}000). N is the number of animals and K is the number of records Minho kept.

Each of the next K lines holds one record as three integers tit_i, xix_i, yiy_i. tit_i is 1 or 2, and xix_i and yiy_i are 32-bit signed integers.

Output

Print the number of bad records Minho has to skip.