atcoder#ARC089B. [ABC086D] Checker

[ABC086D] Checker

Score : 500500 points

Problem Statement

AtCoDeer is thinking of painting an infinite two-dimensional grid in a checked pattern of side K. Here, a checked pattern of side KK is a pattern where each square is painted black or white so that each connected component of each color is a KK ×\times KK square. Below is an example of a checked pattern of side 33:

cba927b2484fad94fb5ff7473e9aadef.png

AtCoDeer has NN desires. The ii-th desire is represented by xix_i, yiy_i and cic_i. If cic_i is B, it means that he wants to paint the square (xi,yi)(x_i,y_i) black; if cic_i is W, he wants to paint the square (xi,yi)(x_i,y_i) white. At most how many desires can he satisfy at the same time?

Constraints

  • 11 \leq NN \leq 10510^5
  • 11 \leq KK \leq 10001000
  • 00 \leq xix_i \leq 10910^9
  • 00 \leq yiy_i \leq 10910^9
  • If ii \neq jj, then (xi,yi)(x_i,y_i) \neq (xj,yj)(x_j,y_j).
  • cic_i is B or W.
  • NN, KK, xix_i and yiy_i are integers.

Input

Input is given from Standard Input in the following format:

NN KK

x1x_1 y1y_1 c1c_1

x2x_2 y2y_2 c2c_2

::

xNx_N yNy_N cNc_N

Output

Print the maximum number of desires that can be satisfied at the same time.

4 3
0 1 W
1 2 W
5 3 B
5 4 B
4

He can satisfy all his desires by painting as shown in the example above.

2 1000
0 0 B
0 1 W
2
6 2
1 2 B
2 1 W
2 2 B
1 0 B
0 6 W
4 5 W
4