Problem:
Score : 500 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 K is a pattern where each square is painted black or white so that each connected component of each color is a K × K square.
Below is an example of a checked pattern of side 3:

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

Constraints

1 ≤ N ≤ 10^5

1 ≤ K ≤ 1000

0 ≤ x_i ≤ 10^9

0 ≤ y_i ≤ 10^9

If i ≠ j, then (x_i,y_i) ≠ (x_j,y_j).

c_i is B or W.

N, K, x_i and y_i are integers.

Input

Input is given from Standard Input in the following format:

N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N

Output

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

Sample Input 1

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

Sample Output 1

4

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

Sample Input 2

2 1000
0 0 B
0 1 W

Sample Output 2

2

Sample Input 3

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

Sample Output 3

4

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        int[][] sum;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int N = in.nextInt();
            int K = in.nextInt();

            int[][] grid = new int[4 * K][4 * K];

            for (int i = 0; i < N; i++) {
                int x = in.nextInt();
                int y = in.nextInt();
                char c = in.next().charAt(0);

                if (c == 'B') {
                    grid[x % (2 * K)][y % (2 * K)]++;
                } else {
                    grid[(x + K) % (2 * K)][y % (2 * K)]++;
                }
            }

            for (int i = 0; i < 4 * K; i++) {
                for (int j = 0; j < 4 * K; j++) {
                    grid[i][j] = grid[i % (2 * K)][j % (2 * K)];
                }
            }

//        print(grid);

            sum = new int[4 * K][4 * K];

            for (int i = 0; i < 4 * K; i++) {
                for (int j = 0; j < 4 * K; j++) {
                    sum[i][j] = grid[i][j];
                    if (i > 0) sum[i][j] += sum[i - 1][j];
                    if (j > 0) sum[i][j] += sum[i][j - 1];
                    if (i > 0 && j > 0) sum[i][j] -= sum[i - 1][j - 1];
                }
            }
//        print(sum);

            int ans = 0;

            for (int lx = 0; lx < 2 * K; lx++) {
                for (int ly = 0; ly < 2 * K; ly++) {
                    ans = Math.max(ans, count(lx, ly, lx + K - 1, ly + K - 1) + count(lx + K, ly + K, lx + K + K - 1, ly + K + K - 1));
                }
            }

            out.println(ans);
        }

        private int count(int lx, int ly, int rx, int ry) {
            return sum(rx, ry) - sum(lx - 1, ry) - sum(rx, ly - 1) + sum(lx - 1, ly - 1);
        }

        private int sum(int rx, int ry) {
            if (rx < 0 || ry < 0) return 0;
            return sum[rx][ry];
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}