#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<iostream>

using namespace std;

int main()
{
    int n,k;
    cin>>n>>k;

    vector<vector<int>> masu_b;
    vector<vector<int>> masu_w;

    masu_b.resize(k);
    masu_w.resize(k);
    for(int i=0;i<k;i++){
        masu_b[i].resize(k,0);
        masu_w[i].resize(k,0);
    }

    int x,y; char c;
    
    for(int i=0;i<n;i++){
        cin>>x>>y>>c;
        if(c=='B'){

            if((x/k)%2==0 && (y/k)%2==0){
                masu_b[x%k][y%k]++;
            }
            if((x/k)%2==1 && (y/k)%2==0){
                masu_w[x%k][y%k]++;
            }
            if((x/k)%2==0 && (y/k)%2==1){
                masu_w[x%k][y%k]++;
            }
            if((x/k)%2==1 && (y/k)%2==1){
                masu_b[x%k][y%k]++;
            }
        }
        else{
            if((x/k)%2==0 && (y/k)%2==0){
                masu_w[x%k][y%k]++;
            }
            if((x/k)%2==1 && (y/k)%2==0){
                masu_b[x%k][y%k]++;
            }
            if((x/k)%2==0 && (y/k)%2==1){
                masu_b[x%k][y%k]++;
            }
            if((x/k)%2==1 && (y/k)%2==1){
                masu_w[x%k][y%k]++;
            }
        }
    }

    vector<vector<pair<int,int>>> d;
    d.resize(k);
    for(int i=0;i<k;i++){
        d[i].resize(k);
    }

    d[0][0]=make_pair(masu_b[0][0],masu_w[0][0]);
    for(int i=1;i<k;i++){
        d[i][0].first=d[i-1][0].first+masu_b[i][0];
        d[i][0].second=d[i-1][0].second+masu_w[i][0];
        d[0][i].first=d[0][i-1].first+masu_b[0][i];
        d[0][i].second=d[0][i-1].second+masu_w[0][i];
    }

    for(int i=1;i<k;i++){
        for(int j=1;j<k;j++){
            d[i][j].first=d[i][j-1].first+d[i-1][j].first-d[i-1][j-1].first+masu_b[i][j];
            d[i][j].second=d[i][j-1].second+d[i-1][j].second-d[i-1][j-1].second+masu_w[i][j];
        }
    }
    /*
    cout<<"black"<<endl;
    for(int i=0;i<k;i++){
        for(int j=0;j<k;j++){
            cout<<masu_b[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<"white"<<endl;
    for(int i=0;i<k;i++){
        for(int j=0;j<k;j++){
            cout<<masu_w[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<"black_total"<<endl;
    for(int i=0;i<k;i++){
        for(int j=0;j<k;j++){
            cout<<d[i][j].first<<" ";
        }
        cout<<endl;
    }
    cout<<"white_total"<<endl;
    for(int i=0;i<k;i++){
        for(int j=0;j<k;j++){
            cout<<d[i][j].second<<" ";
        }
        cout<<endl;
    }
    */
    


    int mx=0;
    for(int i=0;i<k;i++){
        for(int j=0;j<k;j++){
            int blk=d[i][j].first+d[k-1][k-1].first-d[i][k-1].first-d[k-1][j].first+d[i][j].first;
            int wht=d[i][k-1].second+d[k-1][j].second-2*d[i][j].second;
           
            mx=max(mx,blk+wht);
        }
    }
    for(int i=0;i<k;i++){
        for(int j=0;j<k;j++){
            int wht=d[i][j].second+d[k-1][k-1].second-d[i][k-1].second-d[k-1][j].second+d[i][j].second;
            int blk=d[i][k-1].first+d[k-1][j].first-2*d[i][j].first;
           
            mx=max(mx,blk+wht);
        }
    }
    cout<<mx<<endl;

    return 0;
}

Measured memory_bytes: 16000