#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<(n);i++)
#define repf(i,j,n) for(int i=(j);i<(n);i++)

signed main(){
	int n,k;cin>>n>>k;
	vector<int> x(n),y(n);
	vector<char> c(n);
	rep(i,n)cin>>x[i]>>y[i]>>c[i];
	rep(i,n){
	x[i]%=2*k;
	y[i]%=2*k;
	if(c[i]=='B')(x[i]+=k)%=2*k;
	}
	vector<vector<int>>s(4*k+10,vector<int>(4*k+10,0));
	rep(i,n){
		s[x[i]+1][y[i]+1]++;
		s[x[i]+2*k+1][y[i]+1]++;
		s[x[i]+1][y[i]+2*k+1]++;
		s[x[i]+2*k+1][y[i]+2*k+1]++;
	}

	rep(i,4*k+1)rep(j,4*k+1)s[i+1][j]+=s[i][j];
	rep(i,4*k+1)rep(j,4*k+1)s[i][j+1]+=s[i][j];
	int ret=0;
	rep(i,2*k)rep(j,2*k){ret=max(ret,s[i+k][j+k]-s[i+k][j]-s[i][j+k]+s[i][j]+s[i+2*k][j+2*k]-s[i+k][j+2*k]-s[i+2*k][j+k]+s[i+k][j+k]);}
	cout<<ret<<endl;
}
