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

void main()
{
    auto nk = readln.split.to!(int[]);
    auto N = nk[0];
    auto K = nk[1];
    auto bs = new int[][](K*2, K*2);
    auto ws = new int[][](K*2, K*2);
    foreach (_; 0..N) {
        auto xyc = readln.split;
        auto x = xyc[0].to!int % (K*2);
        auto y = xyc[1].to!int % (K*2);
        if (xyc[2] == "B") {
            bs[y][x] += 1;
        } else {
            ws[y][x] += 1;
        }
    }
    foreach (i; 0..K*2) {
        foreach (j; 0..K*2) {
            if (i > 0) {
                bs[i][j] += bs[i-1][j];
                ws[i][j] += ws[i-1][j];
            }
            if (j > 0) {
                bs[i][j] += bs[i][j-1];
                ws[i][j] += ws[i][j-1];
            }
            if (i > 0 && j > 0) {
                bs[i][j] -= bs[i-1][j-1];
                ws[i][j] -= ws[i-1][j-1];
            }
        }
    }

    int r;
    foreach (i; 0..K) {
        foreach (j; 0..K) {
            r = max(r, bs[i][j]*2 + bs[K*2-1][K*2-1] - bs[i][K*2-1] - bs[K*2-1][j] + ws[i][K*2-1] + ws[K*2-1][j] - ws[i][j]*2);
            r = max(r, ws[i][j]*2 + ws[K*2-1][K*2-1] - ws[i][K*2-1] - ws[K*2-1][j] + bs[i][K*2-1] + bs[K*2-1][j] - bs[i][j]*2);
        }
    }
    writeln(r);
}