#include <iostream>

using ulong = unsigned long int;

class Check {
    ulong **B;
    ulong **W;

    ulong k;
public:
    explicit Check(ulong k) {
        this->k = k;
        this->B = new ulong *[k + 1];
        this->W = new ulong *[k + 1];

        for (ulong i = 0; i < this->k + 1; ++i) {
            this->B[i] = new ulong[k + 1]();
            this->W[i] = new ulong[k + 1]();
        }

    }

    void addDesire(ulong x, ulong y, const std::string &c) {
        ulong cx = x + 1;
        ulong cy = y + 1;

        if (c == "W") {
            this->W[cy][cx]++;
        } else {
            this->B[cy][cx]++;
        }
    }

    void itg() {
        for (ulong i = 1; i < this->k + 1; ++i) {
            for (ulong j = 1; j < this->k + 1; ++j) {
                this->B[i][j] += this->B[i - 1][j] + this->B[i][j - 1] - this->B[i - 1][j - 1];
                this->W[i][j] += this->W[i - 1][j] + this->W[i][j - 1] - this->W[i - 1][j - 1];
            }
        }

    }

    ulong count(ulong min_x, ulong max_x, ulong min_y, ulong max_y, ulong **mx) {
        if ((max_x < min_x) or (max_y < min_y)) {
            return 0;
        }

        ulong _min_x = min_x;
        ulong _max_x = max_x + 1;
        ulong _min_y = min_y;
        ulong _max_y = max_y + 1;

        ulong D = mx[_max_y][_max_x];
        ulong A = mx[_min_y][_min_x];
        ulong C = mx[_max_y][_min_x];
        ulong B = mx[_min_y][_max_x];

        return D - B - C + A;
    }

    ulong getMax() {
        this->itg();

        ulong _max = 0;
        ulong k = this->k;
        for (ulong i = 0; i < k+1; ++i) {
            for (ulong j = 0; j < k+1; ++j) {
                ulong count = 0;
                count += this->count(j, k - 1, i, k - 1, this->W);
                count += this->count(0, j - 1, 0, i - 1, this->W);
                count += this->count(0, j - 1, i, k - 1, this->B);
                count += this->count(j, k - 1, 0, i - 1, this->B);
                _max = count > _max ? count : _max;

                count = 0;
                count += this->count(j, k - 1, i, k - 1, this->B);
                count += this->count(0, j - 1, 0, i - 1, this->B);
                count += this->count(0, j - 1, i, k - 1, this->W);
                count += this->count(j, k - 1, 0, i - 1, this->W);
                _max = count > _max ? count : _max;
            }
        }

        return _max;
    }

    ~Check() {
        for (ulong i = 0; i < this->k + 1; ++i) {
            delete[](this->B[i]);
            delete[](this->W[i]);
        }
        delete[](this->B);
        delete[](this->W);
    }
};

int main() {
    ulong n, k;
    std::cin >> n >> k;
    Check checkW = Check(k);

    for (ulong i = 0; i < n; ++i) {
        ulong x, y;
        std::string c;
        std::cin >> x >> y >> c;

        x = x < 0 ? -1 * x + k - 1 : x;
        y = y < 0 ? -1 * y + k - 1 : y;

        ulong bigX = x / k;
        ulong bigY = y / k;

        ulong localX = x % k;
        ulong localY = y % k;
        std::string localC = c;

        if ((bigX + bigY) % 2 != 0) {
            localC = localC == "W" ? "B" : "W";
        }

        checkW.addDesire(localX, localY, localC);
    }

    std::cout << checkW.getMax() << std::endl;

    return 0;
}

Measured memory_bytes: 15872