#include <bits/stdc++.h>
#define FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(),a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORE(i,a,b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define WHATIS(x) cout << #x << " is " << x << endl;
#define ERROR(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x

using namespace std;
using ll = long long;
using ld = double;
typedef pair<int, int> II;
typedef pair<II, ll> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cout << *it << " = " << a << endl;
    err(++it, args...);
}

const ll MODBASE = 1000000007LL;
const int MAXN = 110;
const int MAXM = 35;
const int MAXK = 510;
const int MAXQ = 200010;

int n, k, b[2010][2010], w[2010][2010];

bool isOutside(int x, int y) {
    return x > 2*k || x < 1 || y > 2*k || y < 1;
}

int getB(int x, int y, int u, int v) {
    if (x > u || y > v) return 0;
    if (isOutside(x, y) && isOutside(u, v)) return 0;
    x = max(x, 1); x = min(x, 2*k);
    y = max(y, 1); y = min(y, 2*k);
    u = max(u, 1); u = min(u, 2*k);
    v = max(v, 1); v = min(v, 2*k);
    return b[u][v] - b[x-1][v] - b[u][y-1] + b[x-1][y-1];
}

int getW(int x, int y, int u, int v) {
    if (x > u || y > v) return 0;
    if (isOutside(x, y) && isOutside(u, v)) return 0;
    x = max(x, 1); x = min(x, 2*k);
    y = max(y, 1); y = min(y, 2*k);
    u = max(u, 1); u = min(u, 2*k);
    v = max(v, 1); v = min(v, 2*k);
    return w[u][v] - w[x-1][v] - w[u][y-1] + w[x-1][y-1];
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    cin >> n >> k;
    FOR(i,1,n) {
        int x, y;
        char c;
        cin >> x >> y >> c;
        x %= k * 2;
        y %= k * 2;
        x++;
        y++;
        if (c == 'B') b[x][y]++;
        else w[x][y]++;
    }
    // FOR(i,1,2*k) {
    //     FOR(j,1,2*k) cout << b[i][j] << ' ';
    //     cout << endl;
    // }
    // cout << endl;
    // FOR(i,1,2*k) {
    //     FOR(j,1,2*k) cout << w[i][j] << ' ';
    //     cout << endl;
    // }
    // cout << endl;
    FOR(i,1,2*k)
        FOR(j,1,2*k) {
            b[i][j] = b[i-1][j] + b[i][j-1] - b[i-1][j-1] + b[i][j];
            w[i][j] = w[i-1][j] + w[i][j-1] - w[i-1][j-1] + w[i][j];
        }
    int res = 0;
    FOR(i,0,k)
        FOR(j,0,k) {
            res = max(res, getW(1,1,i,j) + getB(i+1,1,i+k,j) + getW(i+k+1,1,i+k+k,j) + getB(1,j+1,i,j+k) + getW(i+1,j+1,i+k,j+k) + getB(i+k+1,j+1,i+k+k,j+k) + getW(1,j+k+1,i,j+k+k) + getB(i+1,j+k+1,i+k,j+k+k) + getW(i+k+1,j+k+1,i+k+k,j+k+k));
            res = max(res, getB(1,1,i,j) + getW(i+1,1,i+k,j) + getB(i+k+1,1,i+k+k,j) + getW(1,j+1,i,j+k) + getB(i+1,j+1,i+k,j+k) + getW(i+k+1,j+1,i+k+k,j+k) + getB(1,j+k+1,i,j+k+k) + getW(i+1,j+k+1,i+k,j+k+k) + getB(i+k+1,j+k+1,i+k+k,j+k+k));
        }
    cout << res;
    return 0;
}

Measured memory_bytes: 31744