#define R(i,n) for (int i=0;i<(n);i++)
#include <bits/stdc++.h>
using namespace std;int n,k,x,y,b,h[2001][2001];char c;int main(){cin>>n>>k;R(i,n){cin>>x>>y>>c;b=c=='B';x%=2*k;if(x>=k){x-=k;b^=1;}y%=2*k;if(y>=k){y-=k;b^=1;}if(b){h[0][0]++;h[x][0]--;h[0][y]--;h[x][y]+=2;}else{h[0][y]++;h[x][y]-=2;h[x][0]++;}}R(i,k)R(j,k)h[i][j+1]+=h[i][j];R(i,k)R(j,k){h[i+1][j]+=h[i][j];b=max({b,n-h[i][j],h[i][j]});}cout<<b<<endl;}