import std.stdio, std.algorithm, std.range, std.array, std.conv, std.string, std.math, std.container, std.typecons;

void main() {
    int N, K;
    readf("%d %d\n", &N, &K);
    long[][][] cell = new long[][][](2*K,2*K,2);
    long white, black;
    foreach (i; 0..N) {
        int x, y;
        char c;
        readf("%d %d %c\n", &x, &y, &c);
        x %= 2 * K;
        y %= 2 * K;
        cell[x][y][c == 'B' ? 0 : 1]++;
        if (c == 'B') black++;
        else white++;
    }
    foreach (i; 0..2*K) {
        foreach (j; 0..2*K-1) {
            cell[i][j+1][0] += cell[i][j][0];
            cell[i][j+1][1] += cell[i][j][1];
        }
    }
    foreach (j; 0..2*K) {
        foreach (i; 0..2*K-1) {
            cell[i+1][j][0] += cell[i][j][0];
            cell[i+1][j][1] += cell[i][j][1];
        }
    }
    long maxScore = 0;
    foreach (i; 0..K) {
        foreach (j; 0..K) {
            auto blackScore = cell[i][j][0] + cell[i+K][j+K][0] - cell[i][j+K][0] - cell[i+K][j][0] + cell[i][j][0];
            auto whiteScore = cell[i][j][1] + cell[i+K][j+K][1] - cell[i][j+K][1] - cell[i+K][j][1] + cell[i][j][1];
            auto blackScore2 = black - blackScore;
            auto whiteScore2 = white - whiteScore;
            auto score = max(blackScore + whiteScore2, blackScore2 + whiteScore);
            maxScore = max(maxScore, score);
        }
    }
    writeln(maxScore);
}
