#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_poizcy.hpp>
typedef long long  ll;
#define pb                push_back
#define mp                make_pair
#define all(a)            (a).begin(), (a).end()
#define clr(a,h)          memset(a, (h), sizeof(a))
#define mem(a,h)          memset(a, (h), sizeof(a))
#define fi first
#define se second
#define por(a,b) (((a%MOD) * (b%MOD))%MOD)
#define forg(i, b, e, c)    for (ll i = (ll)b; i < (ll)e; i+=c)
#define forr(i, b, e)    for (ll i = b; i < e; i++)
 
 
using namespace std;
//using namespace __gnu_pbds;
typedef double lldb;
typedef pair<ll, ll>  ii;
typedef pair<double, double>  iidb;
typedef pair<ll, ii>  iii;
typedef vector<ll>     vi;
typedef vector<vi>     vvi;
typedef vector<ii>      vii;
typedef vector<ll>      vll;
//typedef tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
const ll INF = 1e9;
const double PI = acos(-1);
#define offset 
const ll MOD= 1e9+7;
#define initseg ll new_nodo=(nodo*2),mid=(iz+der)/2;
 
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define tam 3099
ll MAXANS=0;
ll n,k;
void calc(ll num)
{
	assert(num>=0);
	MAXANS=max(MAXANS,max(num,n-num));
}
ll cantsX[tam],cantsY[tam],totX[tam],totY[tam],cumplenY[tam],cumplenX[tam];
ll v[tam][tam][3];
ll totcumplen[tam][tam],totpuntos[tam][tam];

ll getY(ll num)
{
	if (num==0)return 0;
	return (cantsY[num-1]-cumplenY[num-1]);

}
ll getX(ll num)
{
	if (num==0)return 0;
		return (cantsX[num-1]-cumplenX[num-1]);
}
int main()
{
	
	cin>>n>>k;
	string txt;
	ll x,y,c;
	forr(i,0,n)
	{
		cin>>x>>y>>txt;
		if (txt=="W")c=0;else c=1;
		ll auxxx=x/k;auxxx+=y/k;
		if (auxxx%2==c)
		v[x%k][y%k][0]++;
		else
		v[x%k][y%k][1]++;
	}	
	/*forr(i,0,3)
	{
		forr(j,0,3)
			//cout<<v[i][j][0]<<","<<v[i][j][1]<<" ";
		//cout<<endl;
	}*/
	//cout<<endl;
	ll R0=0;
	forr(i,0,k)
	{
		ll anty=0;
		forr(j,0,k)
		{
			R0+=v[i][j][0];

			cantsY[i]+=v[i][j][0]+v[i][j][1];
			cumplenY[i]+=v[i][j][0];

			//cout<<cantsY[i]<<","<<cumplenY[i]<<" ";

			totcumplen[i][j]=(((i-1)<0)?0:totcumplen[i-1][j])+cumplenY[i];
			totpuntos[i][j]=(((i-1)<0)?0:totpuntos[i-1][j])+cantsY[i];

		}
		//cout<<endl;
		cantsY[i]+=(i>0)?cantsY[i-1]:0;
		cumplenY[i]+=(i>0)?cumplenY[i-1]:0;
		//cout<<cantsY[i]<<","<<cumplenY[i]<<endl;
	}
	//cout<<R0<<endl;
	//cout<<endl;
	// forr(i,0,k)
	// {
	// 	forr(j,0,k)
	// 	{
	// 		//cout<<totcumplen[i][j]<<","<<totpuntos[i][j]<<" ";
	// 	}
	// 	//cout<<endl;
	// }
	forr(j,0,k)
	{
		ll antx=0;
		forr(i,0,k)
		{
			cantsX[j]+=v[i][j][0]+v[i][j][1];
			cumplenX[j]+=v[i][j][0];
			//cout<<cantsX[j]<<","<<cumplenX[j]<<" ";
		}
		//cout<<endl;
		cantsX[j]+=(j>0)?cantsX[j-1]:0;
		cumplenX[j]+=(j>0)?cumplenX[j-1]:0;
		//cout<<cantsX[j]<<","<<cumplenX[j]<<endl;
	}
	//cout<<"asdff";
	//cout<<R0<<endl;
	calc(R0);

	forr(i,0,k)
	{
		forr(j,0,k)
		{
			//if (i+j==0)continue;
			ll ans=R0;
			ans+=getX(j)+getY(i);

			if (i==0|| j==0){
				if (i>0) ans-=cumplenY[i-1];
				if (j>0) ans-=cumplenX[j-1];
				calc(ans);
			}
			else
			{
				if (i>0) ans-=cumplenY[i-1];
				if (j>0) ans-=cumplenX[j-1];
				if (i>0 && j>0){
					////cout<<totcumplen[i-1][j-1]<<'-';
					ans+=totcumplen[i-1][j-1];
				}
				//cout<<ans<<' ';
				
				//cout<<ans<<" ";
				ans-=2*(totpuntos[i-1][j-1]-totcumplen[i-1][j-1]);
				ans+=totcumplen[i-1][j-1];
				//cout<<ans<<" "<<i<<" "<<j<<endl;
				calc(ans);
			}
		}
	}
	cout<<MAXANS<<endl;
}				

Measured memory_bytes: 54248