시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 1024 MB2211947.368%

문제

There are N cities in Bitlandia. Some cities are connected by train or bus, and connections work both ways. Marijonas is preparing for a month-long vacation in Bitlandia. He wants to use trains as much as possible, so he bought a monthly railway ticket. This ticket allows Marijonas unlimited train travel for a month, but it does not cover the cost of bus trips.

Marijonas wants to stay in one city - but is not yet sure in which one. Marijonas would like to stay in a city which allows for a cheap travel to all other cities, because he intends to travel to some of them while staying in the chosen city.

For Marijonas, going from one city to another is cheap if there is a route on which he travels on an unlimited number of trains and at most one bus.

Calculate the number of Bitlandia cities Marijonas could stay in.

입력

Two numbers are given in the first line: the number of cities N, and the number of connections between two cities M. Cities are labeled by 1 up to N, inclusive.

Each of the next M lines consist of two numbers, ai and bi, and a character Ti. The i-th connection connects cities ai and bi, while the character Ti indicates the transportation type of the connection: if Ti is T, then the i-th connection is by train, and if Ti is A, it is by bus.

출력

Output a single integer – the number of cities Marijonas could stay in.

제한

  • 1 ≤ N ≤ 500 000
  • 0 ≤ M ≤ 500 000

서브태스크

번호배점제한
110

There are no bus connections

26

There are no train connections

330

N ≤ 1 000, M ≤ 20 000

435

N ≤ 20 000

519

No additional constraints

예제 입력 1

5 5
3 1 A
3 5 A
4 5 T
2 3 T
2 1 A

예제 출력 1

2

Marijonas could stay cities 2 or 3.

Train connections are marked by continuous lines, and bus connections by dotted lines.

예제 입력 2

7 8
7 5 A
3 7 A
3 1 A
3 4 T
3 5 A
7 1 A
5 6 T
2 7 T

예제 출력 2

4

All cities can be visited cheaply from cities 2, 3, 4 and 7.

채점 및 기타 정보

  • 예제는 채점하지 않는다.