#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int MAXN = 100000;
int x[MAXN + 5], y[MAXN + 5], c[MAXN + 5];
int y1[MAXN],N,K,Max,cnt;
int main() 
{
	scanf("%d%d",&N,&K);
	for(int i=1;i<=N;i++) 
	{
		char ch;
		scanf("%d %d %c",&x[i],&y[i],&ch);
		x[i]%=(2*K);
		y[i]%=(2*K);
		if(ch=='W' ) c[i] = 1;
	}
	for(int i=0;i<=K;i++) {
		cnt = 0;
		fill(y1,y1+2*K+1,0);
		for(int j=1;j<=N;j++) {
			bool p = (i<=x[j]&&x[j]<i+K);
			bool 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++) 
		{
			Max=std::max(Max,std::max(cnt,N-cnt));
			if( j != K )
				cnt = cnt - y1[j] - y1[K+j];
		}
	}
	printf("%d\n",Max);
}
