#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:108777216")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <bitset>
#include <queue>
#include <algorithm>
using namespace std;
 
int const MAX_N = 100 * 1024;
int const INT_INF= 1000000000;
long long const MODULE = 1000000007;

struct ppt {
	int x,y;
	char tp;
} s[MAX_N];

char ttt[MAX_N];
int n,k;

void get_near_zero(int x, int y, int & ans_x, int & ans_y) {
	if (x >= 2*k)
		x -= (x/(2*k))*(2*k);
	if (x <= -2*k)
		x -= (x/(2*k))*(2*k);
	ans_x = x;

    if (y >= 2*k)
		y -= (y/(2*k))*(2*k);
	if (y <= -2*k)
		y -= (y/(2*k))*(2*k);
	ans_y = y;

	if (ans_x < 0)
		ans_x += 2*k;
	if (ans_y < 0)
		ans_y += 2*k;

	//while (ans_x < 0)
	//	ans_x += k;
	//while (ans_y < 0)
	//	ans_y += k;
}

//void norm(int & xx1, int & yy1, int & xx2, int & yy2) {
//	if (xx1 > xx2)
//		xx2 += k;
//	if (yy1 > yy2)
//		yy2 += k;
//
//	while (xx1 < 0) {
//		xx1 += k;
//		xx2 += k;
//	}
//	while (yy1 < 0) {
//		yy1 += k;
//		yy2 += k;
//	}
//}

//struct ev_tp {
//	int x, yy1, yy2, index, tp;
//
//	ev_tp():x(-INT_INF), yy1(0), yy2(-1), index(-1), tp(-5) {}
//	ev_tp(int x, int yy1, int yy2, int index, int tp):x(x),yy1(yy1),yy2(yy2),index(index),tp(tp) {}
//} ev[MAX_N * 60 + 100];
//int ev_len = 0;

//bool operator < (const ev_tp & A, const ev_tp & B) {
//	return A.x < B.x || (A.x == B.x && A.tp > B.tp) || (A.x == B.x && A.tp == B.tp && make_pair(A.yy1, A.yy2) < make_pair(B.yy1,B.yy2));
//}

int const MAX_K = 1000;
int const MS_SIZE = MAX_K * 7 + 10;
int ms[MS_SIZE][MS_SIZE];

void add_rect(int add_x1, int add_x2, int add_y1, int add_y2) {
	add_x1 += 2 * MAX_K;
	add_x2 += 2 * MAX_K;
	add_y1 += 2 * MAX_K;
	add_y2 += 2 * MAX_K;

	ms[add_x1][add_y1]++;
	ms[add_x1][add_y2+1]--;
	ms[add_x2+1][add_y1]--;
	ms[add_x2+1][add_y2+1]++;
}

int main() {
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
 
	scanf("%d%d",&n,&k);
	for (int i=0; i<n; i++) {
		scanf("%d%d%s",&s[i].x,&s[i].y,ttt);
		s[i].tp = ttt[0];
	}

	int const QQ = 2;

	for (int i=0; i<n; i++)
		if (s[i].tp == 'B') {
			int xx1,yy1;
			int x_down = s[i].x - k + 1;
			int y_down = s[i].y - k + 1;
			get_near_zero(x_down, y_down, xx1, yy1);

			int xx2 = xx1 + k - 1;
			int yy2 = yy1 + k - 1;

			//ev[ev_len++] = ev_tp(xx1, yy1,yy2, i, +1);
			//ev[ev_len++] = ev_tp(xx2, yy1,yy2, i, -1);

			for (int dx=-QQ; dx<=QQ; dx++)
				for (int dy=-QQ; dy<=QQ; dy++)
					if ((dx+dy)%2==0) {
						add_rect(xx1 + dx*k, xx2 + dx*k, yy1+dy*k, yy2+dy*k);
						/*ev[ev_len++] = ev_tp(xx1 + dx*k, yy1+dy*k,yy2+dy*k, i, +1);
						ev[ev_len++] = ev_tp(xx2 + dx*k, yy1+dy*k,yy2+dy*k, i, -1);*/
					}
		}
		else {
			int xx1,yy1;
		
			int x_down = s[i].x - 2*k + 1;
			int y_down = s[i].y - k + 1;
			get_near_zero(x_down, y_down, xx1, yy1);

			int xx2 = xx1 + k - 1;
			int yy2 = yy1 + k - 1;

			//ev[ev_len++] = ev_tp(xx1, yy1,yy2, i, +1);
			//ev[ev_len++] = ev_tp(xx2, yy1,yy2, i, -1);

			for (int dx=-QQ; dx<=QQ; dx++)
				for (int dy=-QQ; dy<=QQ; dy++)
					if ((dx+dy)%2==0) {
						add_rect(xx1 + dx*k, xx2 + dx*k, yy1+dy*k, yy2+dy*k);
						/*ev[ev_len++] = ev_tp(xx1 + dx*k, yy1+dy*k,yy2+dy*k, i, +1);
						ev[ev_len++] = ev_tp(xx2 + dx*k, yy1+dy*k,yy2+dy*k, i, -1);*/
					}
		}

	//sort(ev, ev+ev_len);

	int mx = 0;
	//for (int i=0; i<ev_len; i++) {
	//	int yy1 = ev[i].yy1 + 7*MAX_K;
	//	int yy2 = ev[i].yy2 + 7*MAX_K;
	//	if (ev[i].tp == +1) {
	//		for (int j=yy1; j<=yy2; j++)
	//			ms[j]++;
	//	}
	//	else {
	//		for (int j=yy1; j<=yy2; j++)
	//			ms[j]--;
	//	}

	//	//for (int j=-20; j<20; j++) printf("%d ",ms[j+7*MAX_K]);
	//	//printf("\n");
	//	for (int j=yy1; j<=yy2; j++)
	//		mx = max(mx, ms[j]);
	//}

	for (int i=0; i<MS_SIZE; i++) {
		int val = ms[i][0];
		for (int j=1; j<MS_SIZE; j++) {
			val += ms[i][j];
			ms[i][j] = val;
		}
	}
	for (int j=0; j<MS_SIZE; j++) {
		int val = ms[0][j];
		for (int i=1; i<MS_SIZE; i++) {
			val += ms[i][j];
			ms[i][j] = val;
		}
	}
	for (int i=0; i<MS_SIZE; i++)
		for (int j=0; j<MS_SIZE; j++)
			if (ms[i][j] > mx)
				mx = ms[i][j];
			

	cout<<mx;
    return 0;
}  


Measured memory_bytes: 193408