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