import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;

void main() {
    int n, k;
    scan(n, k);

    auto w = new int[][][](2, k, k);

    foreach (i ; 0 .. n) {
        int xi, yi;
        char ci;
        scan(xi, yi, ci);

        if (ci == 'B') yi += k;

        int e = (xi / k % 2 + yi / k % 2) % 2;
        w[e][xi % k][yi % k]++;
    }

    foreach (e ; 0 .. 2) {
        foreach (i ; 0 .. k) {
            foreach (j ; 1 .. k) {
                w[e][i][j] += w[e][i][j-1];
            }
        }

        foreach (j ; 0 .. k) {
            foreach (i ; 1 .. k) {
                w[e][i][j] += w[e][i-1][j];
            }
        }
    }

    int ans;

    foreach (i ; 0 .. k) {
        foreach (j ; 0 .. k) {
            int t = w[0][k-1][k-1] - w[0][k-1][j] - w[0][i][k-1] + 2*w[0][i][j];
            int s = w[1][i][k-1] + w[1][k-1][j] - 2*w[1][i][j];

            ans = max(ans, t + s);
            ans = max(ans, n - t - s);
        }
    }

    writeln(ans);
}




void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}



void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}