#include "bits/stdc++.h"
using namespace std;

int main() {
	int N, K;
	cin >> N >> K;
	vector<vector<int>> dataW(2 * K, vector<int>(K * 2, 0));
	vector<vector<int>> dataB(2 * K, vector<int>(K * 2, 0));
	for (int i = 0; i < N; ++i) {
		int x, y;
		string c;
		cin >> x >> y >> c;
		if (c == "W") {
			dataW[x % (2 * K)][y % (2 * K)]++;
		}
		else {
			dataB[x % (2 * K)][y % (2 * K)]++;
		}
	}
	vector<vector<int>> sumW(2 * K + 1, vector<int>(K * 2 + 1, 0));
	for (int i = 0; i < 2 * K; ++i) {
		for (int j = 0; j < 2 * K; ++j) {
			sumW[i + 1][j + 1] = sumW[i + 1][j] + sumW[i][j + 1] - sumW[i][j] + dataW[i][j];
		}
	}
	vector<vector<int>> sumB(2 * K + 1, vector<int>(K * 2 + 1, 0));
	for (int i = 0; i < 2 * K; ++i) {
		for (int j = 0; j < 2 * K; ++j) {
			sumB[i + 1][j + 1] = sumB[i + 1][j] + sumB[i][j + 1] - sumB[i][j] + dataB[i][j];
		}
	}
	int ans = 0;
	for (int i = 0; i <= K; ++i) {
		for (int j = 0; j <= K; ++j) {
			int tmp = 0;
			int tmp1 =0;
			//黒
			{
				int x1 = i - 1;
				int y1 = j - 1;
				int x0 = -1;
				int y0 = -1;
				tmp += sumB[x1 + 1][y1 + 1] + sumB[x0 + 1][y0 + 1] - sumB[x1 + 1][y0 + 1] - sumB[x0 + 1][y1 + 1]; 
				tmp1 += sumW[x1 + 1][y1 + 1] + sumW[x0 + 1][y0 + 1] - sumW[x1 + 1][y0 + 1] - sumW[x0 + 1][y1 + 1];
			}
			{
				int x1 = i + K - 1;
				int y1 = j + K - 1;
				int x0 = i - 1;
				int y0 = j - 1;
				tmp += sumB[x1 + 1][y1 + 1] + sumB[x0 + 1][y0 + 1] - sumB[x1 + 1][y0 + 1] - sumB[x0 + 1][y1 + 1];
				tmp1 += sumW[x1 + 1][y1 + 1] + sumW[x0 + 1][y0 + 1] - sumW[x1 + 1][y0 + 1] - sumW[x0 + 1][y1 + 1];
			}
			{
				int x1 = i - 1;
				int y1 = 2 * K - 1;
				int x0 = -1;
				int y0 = j + K - 1;
				tmp += sumB[x1 + 1][y1 + 1] + sumB[x0 + 1][y0 + 1] - sumB[x1 + 1][y0 + 1] - sumB[x0 + 1][y1 + 1];
				tmp1 += sumW[x1 + 1][y1 + 1] + sumW[x0 + 1][y0 + 1] - sumW[x1 + 1][y0 + 1] - sumW[x0 + 1][y1 + 1];
			}
			{
				int x1 = 2 * K - 1;
				int y1 = j - 1;
				int x0 = i + K - 1;
				int y0 = -1;
				tmp += sumB[x1 + 1][y1 + 1] + sumB[x0 + 1][y0 + 1] - sumB[x1 + 1][y0 + 1] - sumB[x0 + 1][y1 + 1];
				tmp1 += sumW[x1 + 1][y1 + 1] + sumW[x0 + 1][y0 + 1] - sumW[x1 + 1][y0 + 1] - sumW[x0 + 1][y1 + 1];
			}
			{
				int x1 = 2 * K - 1;
				int y1 = 2 * K - 1;
				int x0 = i + K - 1;
				int y0 = j + K - 1;
				tmp += sumB[x1 + 1][y1 + 1] + sumB[x0 + 1][y0 + 1] - sumB[x1 + 1][y0 + 1] - sumB[x0 + 1][y1 + 1];
				tmp1 += sumW[x1 + 1][y1 + 1] + sumW[x0 + 1][y0 + 1] - sumW[x1 + 1][y0 + 1] - sumW[x0 + 1][y1 + 1];
			}
			//白
			{
				int x1 = i + K - 1;
				int y1 = j - 1;
				int x0 = i - 1;
				int y0 = -1;
				tmp += sumW[x1 + 1][y1 + 1] + sumW[x0 + 1][y0 + 1] - sumW[x1 + 1][y0 + 1] - sumW[x0 + 1][y1 + 1]; 
				tmp1 += sumB[x1 + 1][y1 + 1] + sumB[x0 + 1][y0 + 1] - sumB[x1 + 1][y0 + 1] - sumB[x0 + 1][y1 + 1];
			}
			{
				int x1 = i + K - 1;
				int y1 = 2 * K - 1;
				int x0 = i - 1;
				int y0 = j + K - 1;
				tmp += sumW[x1 + 1][y1 + 1] + sumW[x0 + 1][y0 + 1] - sumW[x1 + 1][y0 + 1] - sumW[x0 + 1][y1 + 1];
				tmp1 += sumB[x1 + 1][y1 + 1] + sumB[x0 + 1][y0 + 1] - sumB[x1 + 1][y0 + 1] - sumB[x0 + 1][y1 + 1];
			}
			{
				int x1 = i - 1;
				int y1 = j + K - 1;
				int x0 = -1;
				int y0 = j - 1;
				tmp += sumW[x1 + 1][y1 + 1] + sumW[x0 + 1][y0 + 1] - sumW[x1 + 1][y0 + 1] - sumW[x0 + 1][y1 + 1];
				tmp1 += sumB[x1 + 1][y1 + 1] + sumB[x0 + 1][y0 + 1] - sumB[x1 + 1][y0 + 1] - sumB[x0 + 1][y1 + 1];
			}
			{
				int x1 = 2 * K - 1;
				int y1 = j + K - 1;
				int x0 = i + K - 1;
				int y0 = j - 1;
				tmp += sumW[x1 + 1][y1 + 1] + sumW[x0 + 1][y0 + 1] - sumW[x1 + 1][y0 + 1] - sumW[x0 + 1][y1 + 1];
				tmp1 += sumB[x1 + 1][y1 + 1] + sumB[x0 + 1][y0 + 1] - sumB[x1 + 1][y0 + 1] - sumB[x0 + 1][y1 + 1];
			}
			ans = max(ans, max(tmp1,tmp));
		}
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 66100