#include <bits/stdc++.h>
using namespace std;
///////////////////////////////////////////
const long long int INF = 1LL<<60;
const long long int Mod = 1000000007;
using ll = long long int; using ci = const int;
using vi = vector<int>;  using Vi = vector<long long int>;
using P = pair<int, int>;  using PLL = pair<ll, ll>;
using matrix = vector<vector<ll>>;
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define rp(i,N) for(ll i = 0; i < (ll)N; i++)
#define repi(i,a,b) for(ll i = ll(a); i < ll(b); ++i)
template<class T>bool chmax(T &former, const T &b) { if (former<b) { former=b; return true; } return false; }
template<class T>bool chmin(T &former, const T &b) { if (b<former) { former=b; return true; } return false; }
template<class T>T sqar(T x){ return x*x; }//sqrt(x)は平方根;
#define Sort(v) std::sort(v.begin(), v.end(), std::greater<decltype(v[0])>()) //降順でVをソート
#define p_queue(v) priority_queue<v, vector<v>, greater<v> >
template<class T> inline void princ(T x){cout<<x<<" ";}; 
template<class T> inline void print(T x){cout<<x<<"\n";};
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
///////////////////////////////////////////////////////////////////////////////////

void solve(){
    ll n,k;
    cin >> n >> k;
    matrix bl(2*k,Vi(2*k,0)),wh(2*k,Vi(2*k,0));
    rp(i,n){
        ll x,y;
        char c;
        cin >> x >> y >> c;
        if(c=='B'){
            bl[x%(2*k)][y%(2*k)]++;
        }else{
            wh[x%(2*k)][y%(2*k)]++;
        }
    }
    matrix syn(2*k,Vi(2*k));
    ll base=0;
    rp(i,2*k){
        rp(j,2*k){
            syn[i][j]=bl[i][j]-wh[i][j];
            base+=bl[i][j]+wh[i][j];
        }
    }
    ll start=0;
    Vi vsumsyn(2*k,0),hsumsyn(2*k,0);
    rp(i,2*k){
        rp(j,2*k){
            if(i>=k&&j>=k||i<k&&j<k) {
                start+=syn[i][j];
                vsumsyn[i]+=syn[i][j];
                hsumsyn[j]+=syn[i][j];
            }
            else {
                start-=syn[i][j];
                vsumsyn[i]-=syn[i][j];
                hsumsyn[j]-=syn[i][j];
            }
        }
    }
    ll score=-100000000;
    rp(i,2*k){
        if(i>0){
            rp(j,2*k){
                if(i<=k&&j<k){
                    vsumsyn[j]-=syn[j][i-1]*2-syn[j][i+k-1]*2;
                    start-=syn[j][i-1]*2-syn[j][i+k-1]*2;
                }else if(i<=k&&j>=k){
                    vsumsyn[j]+=syn[j][i-1]*2-syn[j][i+k-1]*2;
                    start+=syn[j][i-1]*2-syn[j][i+k-1]*2;
                }
            }
        }
        ll cur=start;
        /*if(i==3){
            for(auto v:vsumsyn){
                princ(v);
            }
            cout << endl;
        }*/
        rp(j,2*k){
            if(j==0){
                chmax(score,start);
                continue;
            }
            if(j<=k){
                cur-=vsumsyn[j-1]*2;
                cur-=vsumsyn[j+k-1]*2;
            }else{
                cur+=vsumsyn[j-1]*2;
                cur+=vsumsyn[(j+k-1)%(2*k)]*2;
            }
            chmax(score,cur);
        }
        if(score==10) princ(i);
    }
    print((base+score)/2);
    return;
}
int main(){
    cin.tie(0);ios::sync_with_stdio(false);
    std::cout<<std::fixed<<std::setprecision(30);
    solve();
    return 0;
}

Measured memory_bytes: 97092