// reader
if (!process.env.LOCAL_DEBUG) {
    require('fs').readFile('/dev/stdin', 'utf8', function (err, data) {
        if (err)
            console.error(err);
        main(data.trim().split('\n'));
    });
}
// main
function main(lines) {
    console.log(solve(lines));
}
// solver
function solve(lines) {
    var _a = parseLines(lines), K = _a.K, requests = _a.requests;
    var grants = [];
    for (var i = 0; i < requests.length; i++) {
        var cnt = 1;
        var _b = requests[i], x = _b[0], y = _b[1];
        for (var j = 0; j < requests.length; j++) {
            if (i !== j) {
                var _c = requests[j], _x = _c[0], _y = _c[1];
                var __x = Math.abs(x - _x) % (2 * K);
                var __y = Math.abs(y - _y) % (2 * K);
                if (__x === __y || Math.abs(__x + __y) % K !== 0) {
                    cnt++;
                }
            }
        }
        grants.push(cnt);
    }
    debug({ grants: grants });
    return Math.max.apply(Math, grants).toString();
}
// helpers
function parseLines(lines) {
    var _a = lines[0].split(' ').map(function (s) { return +s; }), N = _a[0], K = _a[1];
    var requests = lines.slice(1, 1 + N).map(function (line) {
        var _a = line.split(' '), x = _a[0], y = _a[1], c = _a[2];
        return [+x, c === 'B' ? +y : +y + K];
    });
    var obj = { N: N, K: K, requests: requests };
    debug(obj);
    return obj;
}
// general helpers
function debug(message) {
    var optionalParams = [];
    for (var _i = 1; _i < arguments.length; _i++) {
        optionalParams[_i - 1] = arguments[_i];
    }
    if (!!process.env.LOCAL_DEBUG)
        console.log.apply(console, [message].concat(optionalParams));
}
