#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int Maxn=100000,Maxk=1000;
int x[Maxn+5],y[Maxn+5],c[Maxn+5],y1[10*Maxk+5];
int main()
{
	#ifdef LOACL
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
	#endif
	int N,K,Max=0,cnt=0;
	scanf("%d %d",&N,&K);
	for(int i=1;i<=N;i++)
	{
		char ch;
		scanf("%d %d %c",&x[i],&y[i],&ch);
		if(ch=='W')c[i]=1;
		else c[i]=0;
		x[i]=x[i]%(2*K);
		y[i]=y[i]%(2*K);
	}
	for(int i=0;i<=K;i++)
	{
		cnt=0;
		memset(y1,0,sizeof(y1));
		for(int j=1;j<=N;j++)
		{
			int p=(i<=x[j]&&x[j]<i+K);
			int q=(0<=y[j]&&y[j]<K);
			if(p^q==c[j])
			{
				cnt++;
				y1[y[j]]++;
			}
			else
				y1[y[j]]--;
		}
		for(int j=0;j<=K;j++)
		{
			if(cnt>Max)Max=cnt;
			if(N-cnt>Max)Max=N-cnt;
			if(j!=K)cnt=cnt-y1[j]-y1[K+j];
		}
	}
	printf("%d\n",Max);
	return 0;
}