#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for (int i=0;i<(n);i++)
#define REP2(i,m,n) for (int i=m;i<(n);i++)
typedef long long ll;

int N, K;
int B[4040][4040];
int W[4040][4040];

int acmb(int x1, int y1, int x2, int y2) {
    return B[x2+1][y2+1] - B[x2+1][y1] - B[x1][y2+1] + B[x1][y1];
}

int acmw(int x1, int y1, int x2, int y2) {
    return W[x2+1][y2+1] - W[x2+1][y1] - W[x1][y2+1] + W[x1][y1];
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0);

    cin >> N >> K;
    REP(i, N) {
        int x, y; string c;
        cin >> x >> y >> c;
        x = x % (2 * K);
        y = y % (2 * K);
        if (c == "B") {
            B[x+1][y+1] += 1;
            B[x+1+2*K][y+1+2*K] += 1;
        } else {
            W[x+1][y+1] += 1;
            W[x+1+2*K][y+1+2*K] += 1;
        }
    }


    REP(i, 4*K+1) REP(j, 4*K) B[i][j+1] += B[i][j];
    REP(j, 4*K+1) REP(i, 4*K) B[i+1][j] += B[i][j];
    REP(i, 4*K+1) REP(j, 4*K) W[i][j+1] += W[i][j];
    REP(j, 4*K+1) REP(i, 4*K) W[i+1][j] += W[i][j];


    int ans = 0;

    REP(i, 2*K) REP(j, 2*K) {
        int b = acmb(i, j, i+K-1, j+K-1) + acmb(i+K, j+K, i+2*K-1, j+2*K-1);
        int w = acmw(i, j+K, i+K-1, j+2*K-1) + acmw(i+K, j, i+2*K-1, j+K-1);
        ans = max(ans, b + w);
        b = acmb(i, j+K, i+K-1, j+2*K-1) + acmb(i+K, j, i+2*K-1, j+K-1);
        w = acmw(i, j, i+K-1, j+K-1) + acmw(i+K, j+K, i+2*K-1, j+2*K-1);
        ans = max(ans, b + w);
    }

    cout << ans << endl;
}
