#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
#define rep(i, n) for(ll i = 0; i < (n); i++)
#define repb(i, n) for(ll i = (n)-1; i >= 0; i--)
#define repr(i, a, b) for(ll i = (a); i < (b); i++)
#define reprb(i, a, b) for(ll i = (b)-1; i >= (a); i--)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
const ll MOD = 1000000007;
const ll INF = 100000000000000000LL;
const ld EPS = 1e-12L;
const ld PI = 3.1415926535897932385L;
inline ll GCD(ll a, ll b){ return b?GCD(b, a % b):a; }
inline ll LCM(ll a, ll b){ return a/GCD(a, b)*b; }
inline ll powint(ull x, ll y){ ll r=1; while(y){ if(y&1) r*=x; x*=x; y>>=1; } return r; }
inline ll powmod(ll x, ll y, ll m = MOD){ ll r=1; while(y){ if(y&1) r*=x; x*=x; r%=m; x%=m; y>>=1; } return r; }
template<class S, class T>inline bool chmax(S &a, const T &b){ if(a<b) { a=b; return 1; } return 0; }
template<class S, class T>inline bool chmin(S &a, const T &b){ if(b<a) { a=b; return 1; } return 0; }
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif

int main(){
    cin.tie(0); ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    ll n, k;
    cin >> n >> k;
    vll x(n), y(n), c(n);
    vvll b_req(k+2, vll(k+2, 0));
    vvll w_req(k+2, vll(k+2, 0));
    rep(i, n){
        char a;
        cin >> x[i] >> y[i] >> a;
        c[i] = a == 'W';
        c[i] ^= (x[i]/k) % 2;
        x[i] %= k;
        c[i] ^= (y[i]/k) % 2;
        y[i] %= k;
        if(c[i] == 0){
            b_req[k-y[i]][x[i]+1]++;
        }else{
            w_req[k-y[i]][x[i]+1]++;
        }
    }
    vvll b_pp(k+2, vll(k+2, 0));
    vvll b_mm(k+2, vll(k+2, 0));
    vvll b_mp(k+2, vll(k+2, 0));
    vvll b_pm(k+2, vll(k+2, 0));
    vvll w_pp(k+2, vll(k+2, 0));
    vvll w_mm(k+2, vll(k+2, 0));
    vvll w_mp(k+2, vll(k+2, 0));
    vvll w_pm(k+2, vll(k+2, 0));
    repb(i, k+1)repb(j, k+1){
        b_pp[i][j] = b_pp[i][j+1] + b_req[i][j];
    }
    repb(i, k+1)repb(j, k+1){
        b_pp[i][j] = b_pp[i+1][j] + b_pp[i][j];
    }
    repr(i, 1, k+2)repr(j, 1, k+2){
        b_mm[i][j] = b_mm[i][j-1] + b_req[i][j];
    }
    repr(i, 1, k+2)repr(j, 1, k+2){
        b_mm[i][j] = b_mm[i-1][j] + b_mm[i][j];
    }
    repr(i, 1, k+2)repb(j, k+1){
        w_mp[i][j] = w_mp[i][j+1] + w_req[i][j];
    }
    repr(i, 1, k+2)repb(j, k+1){
        w_mp[i][j] = w_mp[i-1][j] + w_mp[i][j];
    }
    repb(i, k+1)repr(j, 1, k+2){
        w_pm[i][j] = w_pm[i][j-1] + w_req[i][j];
    }
    repb(i, k+1)repr(j, 1, k+2){
        w_pm[i][j] = w_pm[i+1][j] + w_pm[i][j];
    }
    repb(i, k+1)repb(j, k+1){
        w_pp[i][j] = w_pp[i][j+1] + w_req[i][j];
    }
    repb(i, k+1)repb(j, k+1){
        w_pp[i][j] = w_pp[i+1][j] + w_pp[i][j];
    }
    repr(i, 1, k+2)repr(j, 1, k+2){
        w_mm[i][j] = w_mm[i][j-1] + w_req[i][j];
    }
    repr(i, 1, k+2)repr(j, 1, k+2){
        w_mm[i][j] = w_mm[i-1][j] + w_mm[i][j];
    }
    repr(i, 1, k+2)repb(j, k+1){
        b_mp[i][j] = b_mp[i][j+1] + b_req[i][j];
    }
    repr(i, 1, k+2)repb(j, k+1){
        b_mp[i][j] = b_mp[i-1][j] + b_mp[i][j];
    }
    repb(i, k+1)repr(j, 1, k+2){
        b_pm[i][j] = b_pm[i][j-1] + b_req[i][j];
    }
    repb(i, k+1)repr(j, 1, k+2){
        b_pm[i][j] = b_pm[i+1][j] + b_pm[i][j];
    }
    ll ansm = 0;
    repr(i, 1, k+2)repr(j, 1, k+2){
        chmax(ansm, b_pp[i][j]+b_mm[i-1][j-1]+w_mp[i-1][j]+w_pm[i][j-1]);
    }
    repr(i, 1, k+2)repr(j, 1, k+2){
        chmax(ansm, w_pp[i][j]+w_mm[i-1][j-1]+b_mp[i-1][j]+b_pm[i][j-1]);
    }
    /*
    dump(w_req);
    dump(b_req);
    dump(b_pp);
    dump(b_mm);
    dump(w_mp);
    dump(w_pm);
    dump(ans);
    */
    cout << ansm << endl;
    return 0;
}

Measured memory_bytes: 81408