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: Wrong Answer)

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;
		FastScanner in = new FastScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		ProblemD solver = new ProblemD();
		solver.solve(1, in, out);
		out.close();
	}

	static class ProblemD {
		public void solve(int testNumber, FastScanner in, PrintWriter out) {
			int n = in.nextInt();
			int k = in.nextInt();
			int[][] sb = new int[2 * k + 1][2 * k + 1];
			int[][] sw = new int[2 * k + 1][2 * k + 1];
			for (int i = 0; i < n; i++) {
				int x = in.nextInt();
				int y = in.nextInt();
				char c = in.next().charAt(0);
				x %= 2 * k;
				y %= 2 * k;
				++x;
				++y;
				if (c == 'B') {
					++sb[x][y];
				} else {
					++sw[x][y];
				}
			}

			for (int[][] s : new int[][][]{sb, sw}) {
				for (int i = 1; i <= 2 * k; i++) {
					for (int j = 1; j <= 2 * k; j++) {
						s[i][j] += s[i - 1][j];
						s[i][j] += s[i][j - 1];
						s[i][j] -= s[i - 1][j - 1];
					}
				}
			}

			int ans = 0;
			for (int di = 0; di < k; di++) {
				for (int dj = 0; dj < k; dj++) {
					int cur = 0;
					for (int bigI = -1; bigI <= 1; bigI++) {
						for (int bigJ = -1; bigJ <= 1; bigJ++) {
							int i1 = bigI * k + di + 1;
							int i2 = i1 + k - 1;
							int j1 = bigJ * k + dj + 1;
							int j2 = j1 + k - 1;
							int[][] s;
							if ((bigI + bigJ) % 2 == 0) {
								s = sb;
							} else {
								s = sw;
							}
							if (i1 > 2 * k) {
								continue;
							}
							i1 = Math.max(i1, 1);
							if (i2 < 1) {
								continue;
							}
							i2 = Math.min(i2, 2 * k);
							if (j1 > 2 * k) {
								continue;
							}
							j1 = Math.max(j1, 1);
							if (j2 < 1) {
								continue;
							}
							j2 = Math.min(j2, 2 * k);
							--i1;
							--j1;
							if (i1 < i2 && j1 < j2) {
								cur += s[i2][j2];
								cur -= s[i2][j1];
								cur -= s[i1][j2];
								cur += s[i1][j1];
							}
						}
					}
					ans = Math.max(ans, cur);
				}
			}
			out.println(ans);
		}

	}

	static class FastScanner {
		private BufferedReader in;
		private StringTokenizer st;

		public FastScanner(InputStream stream) {
			in = new BufferedReader(new InputStreamReader(stream));
		}

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

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

	}
}