#include <bits/stdc++.h>
#define M_PI       3.14159265358979323846   // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;

#define rep(a,n) for(ll a = 0;a < n;a++)
#define repi(a,b,n) for(ll a = b;a < n;a++)

using namespace std;

static const ll INF = 1e15;

template<typename T>
static inline void chmin(T & ref, const T  value) {
	if (ref > value) ref = value;
}

template<typename T>
static inline void chmax(T & ref, const T value) {
	if (ref < value) ref = value;
}

int main() {
    ll n,k;
    cin >> n >> k;
    const ll k2 = k * 2;
    vector<vector<int>> bs(3 * k+1, vector<int>(3 * k+1, 0));
    vector<vector<int>> ws(3 * k+1, vector<int>(3 * k+1, 0));
    rep(i, n) {
        ll x, y;
        char c;
        cin >> x >> y >> c;
        x %= k2;
        y %= k2;
        if (c == 'B') {
            bs[y][x]++;
            if (y <= k) {
                bs[y + k2][x]++;
            }
            if (x <= k) {
                bs[y][x+k2]++;
            }
            if (y <= k) {
                if (x <= k) {
                    bs[y+k2][x + k2]++;
                }
            }
        }
        else {
            ws[y][x]++;
            if (y <= k) {
                ws[y + k2][x]++;
            }
            if (x <= k) {
                ws[y][x + k2]++;
            }
            if (y <= k) {
                if (x <= k) {
                    ws[y + k2][x + k2]++;
                }
            }
        }
    }
    vector<vector<int>> bsum(3 * k + 2, vector<int>(3 * k + 2, 0));
    vector<vector<int>> wsum(3 * k + 2, vector<int>(3 * k + 2, 0));

    rep(y, 3 * k+1) {
        rep(x, 3 * k+1) {
            bsum[y + 1][x + 1] = bsum[y + 1][x] + bsum[y][x + 1] - bsum[y][x] + bs[y][x];
            wsum[y + 1][x + 1] = wsum[y + 1][x] + wsum[y][x + 1] - wsum[y][x] + ws[y][x];
        }
    }
    ll ans = 0;
    rep(y, k+1) {
        rep(x, k+1) {
            ll w1 = wsum[y + k2][x + k2] 
                - wsum[y + k2][x + k] 
                - wsum[y + k][x + k2] 
                + wsum[y + k][x + k];
            ll w2 = wsum[y + k][x + k] 
                - wsum[y + k][x] 
                - wsum[y][x + k] 
                + wsum[y][x];
            ll b1 = bsum[y + k2][x + k] 
                - bsum[y + k][x + k] 
                - bsum[y + k2][x] 
                + bsum[y + k][x];
            ll b2 = bsum[y + k][x + k2] 
                - bsum[y + k][x + k] 
                - bsum[y][x + k2] 
                + bsum[y][x + k];
            ll s = w1 + w2 + b1 + b2;
            chmax(ans, s);
        }
    }
    swap(bsum, wsum);
    rep(y, k + 1) {
        rep(x, k + 1) {
            ll w1 = wsum[y + k2][x + k2]
                - wsum[y + k2][x + k]
                - wsum[y + k][x + k2]
                + wsum[y + k][x + k];
            ll w2 = wsum[y + k][x + k]
                - wsum[y + k][x]
                - wsum[y][x + k]
                + wsum[y][x];
            ll b1 = bsum[y + k2][x + k]
                - bsum[y + k][x + k]
                - bsum[y + k2][x]
                + bsum[y + k][x];
            ll b2 = bsum[y + k][x + k2]
                - bsum[y + k][x + k]
                - bsum[y][x + k2]
                + bsum[y][x + k];
            ll s = w1 + w2 + b1 + b2;
            chmax(ans, s);
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 144472