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

#define NDEBUG
#ifdef DEBUG
#include "../cout11.h"
#undef NDEBUG
#endif
#include <cassert>

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef pair<ll,ll> llll;
typedef pair<double,double> dd;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<ll> vll;

#define sz(a)  int((a).size())
#define pb  push_back
#define FOR(var,from,to) for(int var=(from);var<=(to);++var)
#define rep(var,n)  for(int var=0;var<(n);++var)
#define rep1(var,n)  for(int var=1;var<=(n);++var)
#define repC2(vari,varj,n)  for(int vari=0;vari<(n)-1;++vari)for(int varj=vari+1;varj<(n);++varj)
#define ALL(c)  (c).begin(),(c).end()
#define RALL(c)  (c).rbegin(),(c).rend()
#define tr(i,c)  for(auto i=(c).begin(); i!=(c).end(); ++i)
#define found(s,e)  ((s).find(e)!=(s).end())
#define mset(arr,val)  memset(arr,val,sizeof(arr))
#define mid(x,y) ((x)+((y)-(x))/2)
#define IN(x,a,b) ((a)<=(x)&&(x)<=(b))


int N,K;
vector<vector<ll>> imos;

inline ll sub(int left, int top, int right, int bottom){
    // [left, right], [top, bottom]
    ll a = imos[1+right][1+bottom] - imos[1+right][1+top-1]
         - imos[1+left-1][1+bottom] + imos[1+left-1][1+top-1];
    // printf("sub(%d,%d,%d,%d) = %lld\n", left,top,right,bottom, a);
    return a;
}
ll imos_count1(int left, int top){
    left %= K*2; top %= K*2;
    if (left < K) {
        if (top < K) {
            return sub(left,top, left+K-1,top+K-1);
        } else {
            // K <= top
            return sub(left,top, left+K-1,  K*2-1)
                 + sub(left,  0, left+K-1,top-K-1);
        }
    } else {
        // K <= left
        if (top < K) {
            return sub(left,top,    K*2-1,top+K-1)
                 + sub(   0,top, left-K-1,top+K-1);
        } else {
            return sub(left,top,    K*2-1,  K*2-1)
                 + sub(   0,top, left-K-1,  K*2-1)
                 + sub(left,  0,    K*2-1,top-K-1)
                 + sub(   0,  0, left-K-1,top-K-1);
        }
    }
}
ll imos_count(int left, int top){
    return imos_count1(left,top)
         + imos_count1(left+K, top+K);
}


ll solve(vi& x,vi& y,vi& c){
    // vi rx(N), ry(N);
    imos.assign(K*2+1, vector<ll>(K*2+1, 0));
    // vvi imos(K*2+1, vi(K*2+1, 0));

    rep(i,N){ //1e5
        int xi = x[i] + (c[i] ? K : 0);
        int yi = y[i]; // (c[i] ? K : 0);
        int rx_i = xi%(K*2); // [0..2K)
        int ry_i = yi%(K*2); // [0..2K)
        ++imos[1+rx_i][1+ry_i];
    }
    rep(r,K*2+1) {
        rep(c,K*2) {
            imos[r][c+1] += imos[r][c];
        }
    }
    rep(c,K*2+1) {
        rep(r,K*2) {
            imos[r+1][c] += imos[r][c];
        }
    }
// #ifdef DEBUG
//     rep(r,K*2+1) {
//         cerr << imos[r] << endl;
//     }
// #endif
    ll best = 0LL;
    rep(left, K*2) {
        rep(top, K*2) {
            ll cnt = imos_count(left, top);
            // printf("(%d,%d)->%lld\n", left,top, cnt);
            best = max(best, cnt);
        }
    }
    return best;

    // 2000 x 2000全シフト試すと 4e6; 4e11で死ぬる

}

int main() {
    // int N, K;
    cin >> N >> K; // 1-1e5, 1-1000
    vi x(N), y(N), c(N); // 0-1e9 0-1e9 B=0,W=1
    rep(i,N) {
        char ch;
        cin >> x[i] >> y[i] >> ch;
        c[i] = (ch == 'W') ? 1 : 0;
    }
    cout << solve(x,y,c) << endl;
    return 0;
}


Measured memory_bytes: 32896