#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 rep(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; }
///////////////////////////////////////////////////////////////////////////////////
ll n,k;
Vi x,y,c;//c : 1 :Black

ll xx,yy;
char cc;

ll const XMAX=1010;
ll const YMAX=1010;
ll _sum[XMAX][YMAX][2];
ll v[XMAX][YMAX][2]; //arry
void set_sum(ll x,ll y,ll z){
    rep(i,XMAX)rep(j,YMAX)_sum[i][j][z]=0;
    rep(i,y+1)rep(j,x+1){
        _sum[j+1][i+1][z] += _sum[j+1][i][z] + _sum[j][i+1][z] - _sum[j][i][z] + v[j][i][z];
    }
}
ll get(ll x,ll y,ll x2,ll y2,ll z){//[x,y] ~ (x2,y2)
    if(x>=x2 || y>=y2){
        return -1;
    }else{
        return _sum[x2][y2][z] - _sum[x2][y][z] - _sum[x][y2][z] + _sum[x][y][z];
    }
}

int main(){
    cin.tie(0);ios::sync_with_stdio(false);
    std::cout<<std::fixed<<std::setprecision(30);
    cin>>n>>k;
    rep(i,n){
        cin>>xx>>yy>>cc;
        x.pb(xx);
        y.pb(yy);
        if(cc=='B')c.pb(1);
        else c.pb(0);
    }
    ll idx,idy;
    ll ans=0;
    rep(i,1010)rep(j,1010)rep(k,2)v[i][j][k] = 0;
    rep(i,n){
        if((x[i]/k + y[i]/k)%2==0)v[x[i]%k][y[i]%k][c[i]]++;
        else v[x[i]%k][y[i]%k][(c[i]+1)%2]++;
    }
    set_sum(k,k,0);
    set_sum(k,k,1);
    ll tmp = 0;
    rep(i,k){
        rep(j,k){
            rep(l,2){
                ll tp = (l+1)%2;
                tmp = get(0,0,i+1,j+1,l) + get(i+1,j+1,k,k,l);
                tmp += get(0,0,k,k,(l+1)%2) - get(0,0,i+1,j+1,(l+1)%2) - get(i+1,j+1,k,k,(l+1)%2);
                chmax(ans,tmp);
                tmp = get(i,0,k,j+1,l) + get(0,j+1,i,k,l);
                tmp += get(0,0,k,k,(l+1)%2) - get(i,0,k,j+1,(l+1)%2) - get(0,j+1,i,k,tp);
                chmax(ans,tmp);
                tmp = get(0,j,i+1,k,l) + get(i+1,0,k,j,l);
                tmp += get(0,0,k,k,(l+1)%2) - get(0,j,i+1,k,(l+1)%2) - get(i+1,0,k,j,tp);
                chmax(ans,tmp);
                tmp = get(i,j,k,k,l) + get(0,0,i,j,l);
                tmp += get(0,0,k,k,(l+1)%2) - get(i,j,k,k,(l+1)%2) - get(0,0,i,j,tp);
                chmax(ans,tmp);
            }
        }
    }
    print(ans);
    return 0;
}


Measured memory_bytes: 34784