//Code by 27.
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<fstream>
#include<stdlib.h>
#include<set>
#include<climits>
#include<cmath>  
#include<memory.h> 
#include<sstream>
#include<time.h>
using namespace std;
const unsigned long long BIGEST=1000000000000000000+1000000000000000000;
const long long BIGER=1000000000000000000;
const int BIG=1000000000;
const int MOD=19260817;
long long x[100001],y[100001];
string st[100001];
long long c1[2001][2001];
long long c2[2001][2001];
long long s1[2001][2001];
long long s2[2001][2001];
long long n,m;
long long get1(long long x1,long long y1,long long x2,long long y2)
{
	long long s=0;
	if(x1>0&&y1>0)s+=s1[x1-1][y1-1];
	if(x1>0)s-=s1[x1-1][y2];
	if(y1>0)s-=s1[x2][y1-1];
	s+=s1[x2][y2];
	return s;
}
long long get2(long long x1,long long y1,long long x2,long long y2)
{
	long long s=0;
	if(x1>0&&y1>0)s+=s2[x1-1][y1-1];
	if(x1>0)s-=s2[x1-1][y2];
	if(y1>0)s-=s2[x2][y1-1];
	s+=s2[x2][y2];
	return s;
}
long long ypa1(long long x1,long long y1,long long x2,long long y2)
{
	if(x1>=2*m)
	{
		x1-=2*m;
		x2-=2*m;
	}
	if(y1>=2*m)
	{
		y1-=2*m;
		y2-=2*m;
	}
	if(y2>=2*m)
	{
		if(x2>=2*m)
		{
			return get1(0,0,x2%((2*m)),y2%((2*m)))+get1(x1,y1,(2*m)-1,(2*m)-1)+get1(x1,0,2*m-1,y2%(2*m))+get1(0,y1,x2%(2*m),2*m-1);
		}
		else
		{
			return get1(x1,y1,x2,2*m-1)+get1(x1,0,x2,y2%(2*m));
		}
	}
	else
	{
		if(x2>=2*m)
		{
			return get1(x1,y1,2*m-1,y2)+get1(0,y1,x2%(2*m),y2);
		}
		else
		{
			return get1(x1,y1,x2,y2);
		}
	}
	
}
long long ypa2(long long x1,long long y1,long long x2,long long y2)
{
	if(x1>=2*m)
	{
		x1-=2*m;
		x2-=2*m;
	}
	if(y1>=2*m)
	{
		y1-=2*m;
		y2-=2*m;
	}
	if(y2>=2*m)
	{
		if(x2>=2*m)
		{
			return get2(0,0,x2%((2*m)),y2%((2*m)))+get2(x1,y1,(2*m)-1,(2*m)-1)+get2(x1,0,2*m-1,y2%(2*m))+get2(0,y1,x2%(2*m),2*m-1);
		}
		else
		{
			return get2(x1,y1,x2,2*m-1)+get2(x1,0,x2,y2%(2*m));
		}
	}
	else
	{
		if(x2>=2*m)
		{
			return get2(x1,y1,2*m-1,y2)+get2(0,y1,x2%(2*m),y2);
		}
		else
		{
			return get2(x1,y1,x2,y2);
		}
	}
}
int main()
{
	cin>>n>>m;
	for(long long i=1;i<=n;i++)
	{
		cin>>x[i]>>y[i]>>st[i];
		x[i]=x[i]%(2*m);
		y[i]=y[i]%(2*m);
		if(st[i][0]=='W')c1[x[i]][y[i]]++;
		else c2[x[i]][y[i]]++;
	}
	for(long long i=0;i<2*m;i++)
	{
		for(long long j=0;j<2*m;j++)
		{
			//s1[i][j]=s1[i-1][j]+s1[i][j-1]-s1[i-1][j-1]+(c[i][j]=='W');
			if(i>0)
			{
				s1[i][j]+=s1[i-1][j];
			}
			if(j>0)
			{
				s1[i][j]+=s1[i][j-1];
			}
			if(i>0&&j>0)
			{
				s1[i][j]-=s1[i-1][j-1];
			}
			s1[i][j]+=c1[i][j];
			//s2[i][j]=s2[i-1][j]+s2[i][j-1]-s2[i-1][j-1]+(c[i][j]=='W');
			if(i>0)
			{
				s2[i][j]+=s2[i-1][j];
			}
			if(j>0)
			{
				s2[i][j]+=s2[i][j-1];
			}
			if(i>0&&j>0)
			{
				s2[i][j]-=s2[i-1][j-1];
			}
			s2[i][j]+=c2[i][j];
		}
	}
	/*
	for(long long i=0;i<m*2;i++)
	{
		for(long long j=0;j<2*m;j++)
		{
			cout<<s1[i][j]<<" ";
		}
		cout<<endl;
	}
	cout<<endl;
	for(long long i=0;i<m*2;i++)
	{
		for(long long j=0;j<2*m;j++)
		{
			cout<<s2[i][j]<<" ";
		}
		cout<<endl;
	}
	cout<<endl;
	*/
	long long mx=0;
	for(long long i=0;i<=m;i++)
	{
		for(long long j=0;j<=m;j++)
		{
			//cout<<mx<<endl;
			mx=max(mx,ypa1(i,j,i+m-1,j+m-1)+ypa1(i+m,j+m,i+m+m-1,j+m+m-1)+ypa2(i,j+m,i+m-1,j+m+m-1)+ypa2(i+m,j,i+m+m-1,j+m-1));
                         mx=max(mx,ypa2(i,j,i+m-1,j+m-1)+ypa2(i+m,j+m,i+m+m-1,j+m+m-1)+ypa1(i,j+m,i+m-1,j+m+m-1)+ypa1(i+m,j,i+m+m-1,j+m-1));
		}
	}
	cout<<mx;
	return 0;
}

Measured memory_bytes: 132352