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

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

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

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

        auto e = ((xi / k % 2) + (yi / k % 2)) % 2;

        if (ci == 'W') {
            w[e][xi%k][yi%k]++;
        }
        else {
            b[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];
                b[e][i][j] += b[e][i][j-1];
            }
        }

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

    debug {
        writefln("%(%(%s %)\n%)", w[0]);
        writeln;
        writefln("%(%(%s %)\n%)", w[1]);
        writeln;
        writefln("%(%(%s %)\n%)", b[0]);
        writeln;
        writefln("%(%(%s %)\n%)", b[1]);
        writeln;
    }

    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] + w[0][i][j]*2;
            int s = w[1][i][k-1] + w[1][k-1][j] - w[1][i][j]*2;
            int r = b[1][k-1][k-1] - b[1][k-1][j] - b[1][i][k-1] + b[1][i][j]*2;
            int u = b[0][i][k-1] + b[0][k-1][j] - b[0][i][j]*2;

            int d = t + s + r + u;
            ans = max(ans, d);
            ans = max(ans, n - d);
        }
    }

    writeln(ans);
}



void scan(T...)(ref T args) {
    string[] line = readln.split;
    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);
        }
    }
}