/*#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define RI(a) scanf("%d",&a)
#define RII(a,b) scanf("%d%d",&a,&b)
#define RIII(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)
#define ALL(a) a.begin(),a.end()
#define F first
#define S second
#define REP(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#ifdef leowang
#define debug(...) do{\
	fprintf(stderr,"%s - %d : (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);\
	_DO(__VA_ARGS__);\
}while(0)
template<typename I> void _DO(I&&x){cerr<<x<<endl;}
template<typename I,typename...T> void _DO(I&&x,T&&...tail){cerr<<x<<", ";_DO(tail...);}
#else
#define debug(...)
#endif
//}}}
const ll maxn=2005;
const ll maxlg=__lg(maxn)+2;
const ll INF64=8000000000000000000LL;
const int INF=2000000000;
const ll mod=ll(1e9+7);

int x[100005],y[100005];
string s[100005];
int grid[maxn][maxn];

int sufB[maxn][maxn];
int sufW[maxn][maxn];

int main()
{
	IOS;
	int n,k;
	cin>>n>>k;
	k*=2;
	REP(i,n) cin>>x[i]>>y[i]>>s[i];
	REP(i,n){
		if(s[i][0]=='B') grid[x[i]%k][y[i]%k]=1;
		else grid[x[i]%k][y[i]%k]=-1;
	}

	REP(i,k) REP(j,k){
		if(grid[i][j]==1)  sufB[i][j]++;
		if(grid[i][j]==-1) sufW[i][j]++;
		if(!i&&!j);
		else if(!i){
			sufB[i][j]+=sufB[i][j-1];
			sufW[i][j]+=sufW[i][j-1];
		}
		else if(!j){
			sufB[i][j]+=sufB[i-1][j];
			sufW[i][j]+=sufW[i-1][j];
		}
		else{
			sufB[i][j]+=sufB[i][j-1]+sufB[i-1][j]-sufB[i-1][j-1];
			sufW[i][j]+=sufW[i][j-1]+sufW[i-1][j]-sufW[i-1][j-1];
		}
	}

	REP(i,k) REP(j,k) cout<<sufB[i][j]<<" \n"[j==k-1];
	REP(i,k) REP(j,k) cout<<sufW[i][j]<<" \n"[j==k-1];

	int ans=0;

	REP(i,k/2) REP(j,k/2){
		int tmp=sufB[k-1][k-1]-sufB[k-1][j+k/2]-sufB[i+k/2][k-1]+sufB[i][k-1]+sufB[k-1][j]+2*sufB[i+k/2][j+k/2]-2*sufB[i+k/2][j]-2*sufB[i][j+k/2]+2*sufB[i][j]
			   +sufW[k-1][j+k/2]+sufW[i+k/2][k-1]-sufW[i][k-1]-sufW[k-1][j]-2*sufW[i+k/2][j+k/2]+2*sufW[i+k/2][j]+2*sufW[i][j+k/2]-2*sufW[i][j];
		int tmp2=sufW[k-1][k-1]-sufW[k-1][j+k/2]-sufW[i+k/2][k-1]+sufW[i][k-1]+sufW[k-1][j]+2*sufW[i+k/2][j+k/2]-2*sufW[i+k/2][j]-2*sufW[i][j+k/2]+2*sufW[i][j]
			   +sufB[k-1][j+k/2]+sufB[i+k/2][k-1]-sufB[i][k-1]-sufB[k-1][j]-2*sufB[i+k/2][j+k/2]+2*sufB[i+k/2][j]+2*sufB[i][j+k/2]-2*sufB[i][j];

		cout<<tmp<<' '<<tmp2<<'\n';
		//debug(tmp,tmp2);
		ans=max(ans,tmp);
		ans=max(ans,tmp2);
	}
	cout<<ans;
	return 0;
}*/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define RI(a) scanf("%d",&a)
#define RII(a,b) scanf("%d%d",&a,&b)
#define RIII(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)
#define ALL(a) a.begin(),a.end()
#define F first
#define S second
#define REP(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#ifdef leowang
#define debug(...) do{\
	fprintf(stderr,"%s - %d : (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);\
	_DO(__VA_ARGS__);\
}while(0)
template<typename I> void _DO(I&&x){cerr<<x<<endl;}
template<typename I,typename...T> void _DO(I&&x,T&&...tail){cerr<<x<<", ";_DO(tail...);}
#else
#define debug(...)
#endif
//}}}
const ll maxn=1005;
const ll maxlg=__lg(maxn)+2;
const ll INF64=8000000000000000000LL;
const int INF=2000000000;
const ll mod=ll(1e9+7);
 
int x[100005],y[100005];
string s[100005];
int grid[maxn][maxn][2];
 
int sufB[maxn][maxn];
int sufW[maxn][maxn];
 
int main()
{
	IOS;
	int n,k;
	cin>>n>>k;
	REP(i,n) cin>>x[i]>>y[i]>>s[i];
	REP(i,n){
		if((s[i][0]=='B')^(x[i]%(2*k)<k)^(y[i]%(2*k)<k)) grid[x[i]%k][y[i]%k][0]++;
		else grid[x[i]%k][y[i]%k][1]++;
	}
 
	REP(i,k) REP(j,k){
		sufB[i][j]+=grid[i][j][0];
		sufW[i][j]+=grid[i][j][1];
		if(!i&&!j);
		else if(!i){
			sufB[i][j]+=sufB[i][j-1];
			sufW[i][j]+=sufW[i][j-1];
		}
		else if(!j){
			sufB[i][j]+=sufB[i-1][j];
			sufW[i][j]+=sufW[i-1][j];
		}
		else{
			sufB[i][j]+=sufB[i][j-1]+sufB[i-1][j]-sufB[i-1][j-1];
			sufW[i][j]+=sufW[i][j-1]+sufW[i-1][j]-sufW[i-1][j-1];
		}
	}
 
 
	int ans=0;
 
	REP(i,k) REP(j,k){
		/*int tmp=sufB[k-1][k-1]-sufB[k-1][j+k/2]-sufB[i+k/2][k-1]+sufB[i][k-1]+sufB[k-1][j]+2*sufB[i+k/2][j+k/2]-2*sufB[i+k/2][j]-2*sufB[i][j+k/2]+2*sufB[i][j]
			   +sufW[k-1][j+k/2]+sufW[i+k/2][k-1]-sufW[i][k-1]-sufW[k-1][j]-2*sufW[i+k/2][j+k/2]+2*sufW[i+k/2][j]+2*sufW[i][j+k/2]-2*sufW[i][j];
		int tmp2=sufW[k-1][k-1]-sufW[k-1][j+k/2]-sufW[i+k/2][k-1]+sufW[i][k-1]+sufW[k-1][j]+2*sufW[i+k/2][j+k/2]-2*sufW[i+k/2][j]-2*sufW[i][j+k/2]+2*sufW[i][j]
			   +sufB[k-1][j+k/2]+sufB[i+k/2][k-1]-sufB[i][k-1]-sufB[k-1][j]-2*sufB[i+k/2][j+k/2]+2*sufB[i+k/2][j]+2*sufB[i][j+k/2]-2*sufB[i][j];
 		*/
		//debug(tmp,tmp2);
		int tmp=sufB[k-1][k-1]-sufB[i][k-1]-sufB[k-1][j]+2*sufB[i][j]+sufW[i][k-1]+sufW[k-1][j]-2*sufW[i][j];
		int tmp2=sufW[k-1][k-1]-sufW[i][k-1]-sufW[k-1][j]+2*sufW[i][j]+sufB[i][k-1]+sufB[k-1][j]-2*sufB[i][j];


		ans=max(ans,tmp);
		ans=max(ans,tmp2);
	}
	cout<<ans;
	return 0;
}

Measured memory_bytes: 22272