#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

int main(){
	int n,k; cin>>n>>k;
	vector<vector<int>> B(2*k+1,vector<int>(2*k+1));
	vector<vector<int>> W(2*k+1,vector<int>(2*k+1));
	rep(i,n){
		int x,y;
		char c; cin>>x>>y>>c;
		if(c=='B') B[y%(2*k)+1][x%(2*k)+1]++;
		else       W[y%(2*k)+1][x%(2*k)+1]++;
	}
	rep(i,2*k) rep(j,2*k) {
		B[i+1][j+1]+=B[i+1][j]+B[i][j+1]-B[i][j];
		W[i+1][j+1]+=W[i+1][j]+W[i][j+1]-W[i][j];
	}

	int ans=0;
	rep(i,k+1) rep(j,k+1) {
		int bcnt=0,wcnt=0;
		bcnt+=B[k+i][k+j]-B[i][k+j]-B[k+i][j]+B[i][j];
		bcnt+=B[2*k][2*k]-B[k+i][2*k]-B[2*k][k+j]+B[k+i][k+j];
		bcnt+=B[i][j];
		bcnt+=B[i][2*k]-B[i][k+j];
		bcnt+=B[2*k][j]-B[k+i][j];
		wcnt+=W[k+i][2*k]-W[k+i][k+j]-W[i][2*k]+W[i][k+j];
		wcnt+=W[2*k][k+j]-W[k+i][k+j]-W[2*k][j]+W[k+i][j];
		wcnt+=W[i][k+j]-W[i][j];
		wcnt+=W[k+i][j]-W[i][j];
		ans=max({ans,bcnt+wcnt,n-bcnt-wcnt});
	}
	cout<<ans<<endl;

	return 0;
}
