#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define R register
#define Max(_A, _B) (_A > _B ? _A : _B)
struct Data{ int x, y; bool c, w; } a[100010];
bool cmp(R Data i, R Data j){ return i.x < j.x; }
std::vector<int> A[2010];
/*void Modify(int node, R int begin, R int end, R int pos, R int x)
{
	if(begin > end) return ;
	if(begin == end)
	{
		S[node] += x;
		return ;
	}
	R int mid = begin + end;
	if(pos <= mid) Modify(node << 1, begin, mid, pos, x);
	else Modify(node << 1 | 1, mid + 1, end, pos, x);
	S[node] = S[node << 1] + S[node << 1 | 1];
}*/
int n, k, d[2010], sum[2010], Ans;
void Work()
{
/*	for(R int i = 1; i <= n; i++)
	{
		R int t = a[i].x % k, tmp = (((a[i].x / k) & 1) == ((a[i].y / k) & 1));
		//printf("[%d (%d = %d)]<%d %d> ;", t, tmp, a[i].c, ((a[i].x / k) & 1),((a[i].y / k) & 1));
		a[i].c = (a[i].c == tmp);
		if(a[i].c == 1) d[0]++, d[t + 1]--;
		else d[t + 1]++, d[t + 1 + k]--;
	}*/
//	puts("");
	for(R int i = 0; i <= k + 5; i++)
	{
		sum[0] = d[0];
		for(R int j = 1; j <= k + 5; j++) sum[j] = sum[j - 1] + d[j];
//		for(R int j = 1; j <= n; j++) printf("%d ", a[j].c); puts("\n");
		for(R int j = 0; j <= k + 5; j++) Ans = Max(sum[j], Ans);
		for(R int j = 0; j < A[i].size(); j++)
		{
			R int u = A[i][j];
			R int t = a[u].x % k;
			if(a[u].c == 1) 
			{
				d[0]--, d[t + 1]++, a[u].c = 0; d[t + 1]++, d[t + 1 + k]--;
		//		printf("<%d %d> ", t + 1, t + k);
			}
			else 
			{
				d[0]++, d[t + 1]--, a[u].c = 1; d[t + 1]--, d[t + 1 + k]++;
		//		printf("<%d %d> ", 0, t);
			}
		}
	}
}
int main()
{
	scanf("%d %d", &n, &k);
	for(R int i = 1; i <= n; i++)
	{
		R char t[4];
		scanf("%d %d %s", &a[i].x, &a[i].y, t);
		a[i].w = a[i].c = (t[0] == 'W');
		
	}
//	std::sort(a + 1, a + 1 + n, cmp);
	for(R int i = 1; i <= n; i++)
	{
		R int t = a[i].y % k;
		A[t].push_back(i);
		A[t + k].push_back(i);
	}
	for(R int i = 1; i <= n; i++)
	{
		R int t = a[i].x % k, tmp = (((a[i].x / k) & 1) == ((a[i].y / k) & 1));
		a[i].c = (a[i].c == tmp);
		if(a[i].c == 1) d[0]++, d[t + 1]--;
		else d[t + 1]++, d[t + 1 + k]--;
	}
//	puts("");
/*	for(R int i = 0; i <= k + 5; i++)
	{
		sum[0] = d[0];
		for(R int j = 1; j <= k + 5; j++) sum[j] = sum[j - 1] + d[j];
		for(R int j = 0; j <= k; j++) printf("%d ,", d[j]); puts("");
//		for(R int j = 1; j <= n; j++) printf("%d ", a[j].c); puts("\n");
		for(R int j = 0; j <= k + 5; j++) Ans = Max(sum[j], Ans);
		for(R int j = 0; j < A[i].size(); j++)
		{
			R int u = A[i][j];
			R int t = a[u].x % k;
			printf("(%d)\n", u);
			if(a[u].c == 1) 
			{
				puts("%");
				d[0]--, d[t + 1]++, a[u].c = 0; d[t + 1]++, d[t + 1 + k]--;
		//		printf("<%d %d> ", t + 1, t + k);
			}
			else 
			{
				d[0]++, d[t + 1]--, a[u].c = 1; d[t + 1]--, d[t + 1 + k]++;
		//		printf("<%d %d> ", 0, t);
			}
		}
	}*/
	Work();
	memset(d, 0, sizeof(d));
	for(R int i = 1; i <= n; i++) a[i].c = !a[i].w;
//		for(R int j = 0; j <= k + 5; j++) printf("%d ", d[j]); puts(";");
	for(R int i = 1; i <= n; i++)
	{
		R int t = a[i].x % k, tmp = (((a[i].x / k) & 1) == ((a[i].y / k) & 1));
		//printf("[%d (%d = %d)]<%d %d> ;", t, tmp, a[i].c, ((a[i].x / k) & 1),((a[i].y / k) & 1));
		a[i].c = (a[i].c == tmp);
		if(a[i].c == 1) d[0]++, d[t + 1]--;
		else d[t + 1]++, d[t + 1 + k]--;
	}
	Work();
	printf("%d\n", Ans);
	return 0;
}
/*
5 3
2 5 W
8 0 B
5 4 W
8 8 W
3 1 B
*/

Measured memory_bytes: 2688