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

enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;


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

    auto a = new int[][](2*k + 1, 2*k + 1);

    foreach (i ; 0 .. n) {
        int xi, yi; char ci;
        scan(xi, yi, ci);
        if (ci == 'W') yi += k;
        xi %= 2*k;
        yi %= 2*k;
        a[xi + 1][yi + 1]++;
    }

    debug {
        //writefln("%(%s\n%)", a);
        writeln;
    }

    foreach (i ; 1 .. 2*k + 1) {
        foreach (j ; 1 .. 2*k + 1) {
            a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
        }
    }

    debug {
        //writefln("%(%s\n%)", a);
    }

    int query(int xs, int xt, int ys, int yt) {
        if (xt == 0) xt = 2*k;
        if (yt == 0) yt = 2*k;
        int res;

        if (xs < xt && ys < yt) {
            res = a[xt][yt] - a[xs][yt] - a[xt][ys] + a[xs][ys];
        }
        else if (xs >= xt && ys < yt) {
            res = a[xt][yt] - a[xt][ys];
            res += a[2*k][yt] - a[xs][yt] - a[2*k][ys] + a[xs][ys];
        }
        else if (xs < xt && ys >= yt) {
            res = a[xt][yt] - a[xs][yt];
            res += a[xt][2*k] - a[xt][ys] - a[xs][2*k] + a[xs][ys];
        }
        else {
            res = a[xt][yt];
            res += a[xt][2*k] - a[xt][ys];
            res += a[2*k][yt] - a[xs][yt];
            res += a[2*k][2*k] - a[xs][2*k] - a[2*k][ys] + a[xs][ys];
        }

        return res;
    }

    int ans;

    foreach (x ; 0 .. k) {
        foreach (y ; 0 .. k) {
            int t = query(x, x + k, y, y + k);
            t += query(x + k, x, y + k, y);
            debug {
                writeln("t: ", t);
            }
            ans = max(ans, t, n - t);
        }
    }

    writeln(ans);
}








void yes(bool b) {writeln(b ? "Yes" : "No");}
void YES(bool b) {writeln(b ? "YES" : "NO");}
T[] readArr(T)() {return readln.split.to!(T[]);}

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);
        }
    }
}
