#include<bits/stdc++.h>
using namespace std;

int main(){
    int n,k;
    cin>>n>>k;
    vector<int> x(n),y(n);
    vector<char> c(n);
    for(int i=0;i<n;i++){
        cin>>x[i]>>y[i]>>c[i];
        x[i]%=(2*k),y[i]%=(2*k);
    }
    vector<vector<int>> bf(2*k,vector<int>(2*k,0));
    vector<vector<int>> wf(2*k,vector<int>(2*k,0));
    for(int i=0;i<n;i++){
        if(c[i]=='B') bf[x[i]][y[i]]++;
        if(c[i]=='W') wf[x[i]][y[i]]++;
    }
    //cout<<"set bf and wf."<<endl;
    vector<vector<int>> bfsum(2*k+1,vector<int>(2*k+1,0)); //bfsum[i][j] = cnt([0,i)[0,j)]])
    vector<vector<int>> wfsum(2*k+1,vector<int>(2*k+1,0));
    for(int i=0;i<2*k;i++){
        for(int j=0;j<2*k;j++){
            bfsum[i+1][j+1]=bfsum[i][j+1]+bfsum[i+1][j]-bfsum[i][j]+bf[i][j];
            wfsum[i+1][j+1]=wfsum[i][j+1]+wfsum[i+1][j]-wfsum[i][j]+wf[i][j];
        }
    }
    /*
    for(int i=0;i<2*k;i++){
        for(int j=0;j<2*k;j++){
            cout<<bf[i][j]<<" ";
        }
        cout<<endl;
    }
    
    for(int i=0;i<2*k+1;i++){
        for(int j=0;j<2*k+1;j++){
            cout<<bfsum[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<"#w"<<endl;
    for(int i=0;i<2*k;i++){
        for(int j=0;j<2*k;j++){
            cout<<wf[i][j]<<" ";
        }
        cout<<endl;
    }
    for(int i=0;i<2*k+1;i++){
        for(int j=0;j<2*k+1;j++){
            cout<<wfsum[i][j]<<" ";
        }
        cout<<endl;
    }
    */

    auto getBlack=[&bfsum](int x1,int y1,int x2,int y2){
        return bfsum[x2][y2]-bfsum[x1][y2]-bfsum[x2][y1]+bfsum[x1][y1];
    };
    auto getWhite=[&wfsum](int x1,int y1,int x2,int y2){
        return wfsum[x2][y2]-wfsum[x1][y2]-wfsum[x2][y1]+wfsum[x1][y1];
    };
    int ans=0;
    for(int i=0;i<2*k;i++){
        for(int j=0;j<k;j++){
            int res=getWhite(0,0,2*k,2*k);
            int prei=i,prej=j;

            if(i+k>=2*k && j+k>=2*k){
                res+=getBlack(i,j,2*k,2*k);
                res-=getWhite(i,j,2*k,2*k);

                res+=getBlack(0,0,i+k-2*k,j+k-2*k);
                res-=getWhite(0,0,i+k-2*k,j+k-2*k);

                res+=getBlack(0,j,i+k-2*k,2*k);
                res-=getWhite(0,j,i+k-2*k,2*k);

                res+=getBlack(i,0,2*k,j+k-2*k);
                res-=getWhite(i,0,2*k,j+k-2*k);
            }
            else if(i+k>=2*k){
                res+=getBlack(i,j,2*k,j+k);
                res-=getWhite(i,j,2*k,j+k);

                res+=getBlack(0,j,i+k-2*k,j+k);
                res-=getWhite(0,j,i+k-2*k,j+k);
            }
            else if(j+k>=2*k){
                res+=getBlack(i,j,i+k,2*k);
                res-=getWhite(i,j,i+k,2*k);

                res+=getBlack(i,0,i+k,j+k-2*k);
                res-=getWhite(i,0,i+k,j+k-2*k);
            }
            else{
                res+=getBlack(i,j,i+k,j+k);
                res-=getWhite(i,j,i+k,j+k);
            }
            
            i=(i+k)%(2*k),j=(j+k)%(2*k);

            if(i+k>=2*k && j+k>=2*k){
                res+=getBlack(i,j,2*k,2*k);
                res-=getWhite(i,j,2*k,2*k);

                res+=getBlack(0,0,i+k-2*k,j+k-2*k);
                res-=getWhite(0,0,i+k-2*k,j+k-2*k);

                res+=getBlack(0,j,i+k-2*k,2*k);
                res-=getWhite(0,j,i+k-2*k,2*k);

                res+=getBlack(i,0,2*k,j+k-2*k);
                res-=getWhite(i,0,2*k,j+k-2*k);
            }
            else if(i+k>=2*k){
                res+=getBlack(i,j,2*k,j+k);
                res-=getWhite(i,j,2*k,j+k);

                res+=getBlack(0,j,i+k-2*k,j+k);
                res-=getWhite(0,j,i+k-2*k,j+k);
            }
            else if(j+k>=2*k){
                res+=getBlack(i,j,i+k,2*k);
                res-=getWhite(i,j,i+k,2*k);

                res+=getBlack(i,0,i+k,j+k-2*k);
                res-=getWhite(i,0,i+k,j+k-2*k);
            }
            else{
                res+=getBlack(i,j,i+k,j+k);
                res-=getWhite(i,j,i+k,j+k);
            }
            
            ans=max(ans,res);
            i=prei,j=prej;
        }
    }
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 64000