#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<utility>
#include<set>
#include<stack>
#include<list>
#include<deque>
#include<bitset>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<climits>
#include<cmath>
#include<cctype>


#define pb push_back
#define mp make_pair
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ren(i,a,b) for(int i=a;i>=b;i--)
#define ff first
#define ss second
#define pll pair<long long int,long long int>
#define pii pair<int,int>
#define vll vector<long long int>
#define vii vector<int>
#define gi(n) scanf("%d",&n)
#define gll(n) scanf("%lld",&n)
#define gstr(n) scanf("%s",n)
#define gl(n) cin >> n
#define oi(n) printf("%d",n)
#define oll(n) printf("%lld",n)
#define ostr(n) printf("%s",n)
#define ol(n) cout << n
#define os cout<<" "
#define on cout<<"\n"
#define o2(a,b) cout<<a<<" "<<b
#define all(n) n.begin(),n.end()
#define present(s,x) (s.find(x) != s.end())
#define cpresent(s,x) (find(all(s),x) != s.end())
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
using namespace std;

typedef unsigned long long int ull;
typedef long long int ll;
typedef long double ld;
typedef vector<vector<ll> > mat;

int b[2005][2005],w[2005][2005],n,k,x,y;

int getb(int l1,int r1,int l2,int r2)
{
	//o2(l1,r1);os;o2(l2,r2);on;
	if(l1>l2)return 0;
	if(r1>r2)return 0;
	int res=b[l2][r2];
	if(l1)res-=b[l1-1][r2];
	if(r1)res-=b[l2][r1-1];
	if(l1>0&&r1>0)
	res+=b[l1-1][r1-1];
	return max(0,res);
}

int getw(int l1,int r1,int l2,int r2)
{
	if(l1>l2)return 0;
	if(r1>r2)return 0;
	int res=w[l2][r2];
	if(l1)res-=w[l1-1][r2];
	if(r1)res-=w[l2][r1-1];
	if(l1>0&&r1>0)
	res+=w[l1-1][r1-1];
	return max(0,res);
}

int main()
{ios_base::sync_with_stdio(false);
cin>>n>>k;
int kk=2*k;
while(n--)
{
	char c;
	cin>>x>>y>>c;
	x%=(2*k);y%=2*k;
	if(c=='B')b[x][y]++;
	else w[x][y]++;
}

rep(i,1,kk-1)
{
	b[0][i]+=b[0][i-1];
	w[0][i]+=w[0][i-1];
}

rep(i,1,kk-1)
{
	rep(j,1,kk-1)
	b[i][j]+=b[i][j-1],w[i][j]+=w[i][j-1];
	rep(j,0,kk-1)
	b[i][j]+=b[i-1][j],w[i][j]+=w[i-1][j];
}
int ans=0;

rep(i,0,kk-1)
{
	rep(j,0,kk-1)
	{
		int s=0;
		vector<pii> x,y;
		if(i==0)x.pb(mp(0,k-1));
		else if(i<=k-1)
		{
			x.pb(mp(kk-i,kk-1));
			x.pb(mp(0,k-1-i));
		}
		else
		{
			x.pb(mp(kk-i,kk-i+k-1));
		}
		if(j==0)y.pb(mp(0,k-1));
		else if(j<=k-1)
		{
			y.pb(mp(kk-j,kk-1));
			y.pb(mp(0,k-1-j));
		}
		else
		{
			y.pb(mp(kk-j,kk-j+k-1));
		}
		tr(x,it)
		{
			int l1=it->ff,l2=it->ss;
			tr(y,it1)
			{
				int r1=it1->ff,r2=it1->ss;
				s+=getb(l1,r1,l2,r2);
			}
		}
		//ol(s);on;
		x.clear();y.clear();
		if(i<=k)x.pb(mp(k-i,k-i+k-1));
		else
		{
			x.pb(mp(0,kk-1-i));
			x.pb(mp(kk-1-(i-k-1),kk-1));
		}
		if(j<=k)y.pb(mp(k-j,k-j+k-1));
		else
		{
			y.pb(mp(0,kk-1-j));
			y.pb(mp(kk-1-(j-k-1),kk-1));
		}
		tr(x,it)
		{
			int l1=it->ff,l2=it->ss;
			tr(y,it1)
			{
				int r1=it1->ff,r2=it1->ss;
				s+=getb(l1,r1,l2,r2);
			}
		}
		//ol(s);on;
		x.clear();y.clear();
		if(i<=k)x.pb(mp(k-i,k-i+k-1));
		else
		{
			x.pb(mp(0,kk-1-i));
			x.pb(mp(kk-1-(i-k-1),kk-1));
		}
		if(j==0)y.pb(mp(0,k-1));
		else if(j<=k-1)
		{
			y.pb(mp(kk-j,kk-1));
			y.pb(mp(0,k-1-j));
		}
		else
		{
			y.pb(mp(kk-j,kk-j+k-1));
		}
		tr(x,it)
		{
			int l1=it->ff,l2=it->ss;
			tr(y,it1)
			{
				int r1=it1->ff,r2=it1->ss;
				s+=getw(l1,r1,l2,r2);
			}
		}
		//ol(s);on;
		x.clear();y.clear();
		if(i==0)x.pb(mp(0,k-1));
		else if(i<=k-1)
		{
			x.pb(mp(kk-i,kk-1));
			x.pb(mp(0,k-1-i));
		}
		else
		{
			x.pb(mp(kk-i,kk-i+k-1));
		}
		if(j<=k)y.pb(mp(k-j,k-j+k-1));
		else
		{
			y.pb(mp(0,kk-1-j));
			y.pb(mp(kk-1-(j-k-1),kk-1));
		}
		tr(x,it)
		{
			int l1=it->ff,l2=it->ss;
			tr(y,it1)
			{
				int r1=it1->ff,r2=it1->ss;
				s+=getw(l1,r1,l2,r2);
			}
		}
		//ol(s);on;
		//o2(i,j);os;ol(s);on;
		ans=max(ans,s);
	}
}
ol(ans);
return 0;
}



Measured memory_bytes: 31616