fun main(args: Array<String>) {
    val (N, K) = readLine()!!.split(" ").map { it.toInt() }
    val x = Array(N) { 0 }
    val y = Array(N) { 0 }
    val c = Array(N) { 'B' }

    fun flip(c: Char): Char = if (c == 'B') 'W' else 'B'

    for (i in x.indices) {
        val (X, Y, C) = readLine()!!.split(" ")
        x[i] = X.toInt()
        y[i] = Y.toInt()
        c[i] = C[0]

        x[i] %= 2 * K
        if (x[i] >= K) {
            x[i] -= K
            c[i] = flip(c[i])
        }

        y[i] %= 2 * K
        if (y[i] >= K) {
            y[i] -= K
            c[i] = flip(c[i])
        }
    }

    val imosB = Array(K + 2) { Array(K + 2) { 0 } }
    val imosW = Array(K + 2) { Array(K + 2) { 0 } }

    for (i in x.indices) {
        if (c[i] == 'B') {
            imosB[y[i] + 1][x[i] + 1]++
        } else {
            imosW[y[i] + 1][x[i] + 1]++
        }
    }

    for (i in 1 until imosB.size) for (j in 1 until imosB[i].size) {
        imosB[i][j] += imosB[i - 1][j] + imosB[i][j - 1] - imosB[i - 1][j - 1]
        imosW[i][j] += imosW[i - 1][j] + imosW[i][j - 1] - imosW[i - 1][j - 1]
    }

    fun calcSum(si: Int, sj: Int, ei: Int, ej: Int, imos: Array<Array<Int>>): Int {
        if (si > ei || sj > ej) return 0
        return imos[ei][ej] - imos[si - 1][ej] - imos[ei][sj - 1] + imos[si - 1][sj - 1]
    }

    var res = 0

    for (si in 1..K) for (sj in 1..K) {
        fun calc(a: Array<Array<Int>>, b: Array<Array<Int>>): Int {
            return calcSum(1, 1, si - 1, sj - 1, a) + calcSum(si, sj, K, K, a) +
                    calcSum(1, sj, si - 1, K, b) + calcSum(si, 1, K, sj - 1, b)
        }
        res = Math.max(res, calc(imosB, imosW))
        res = Math.max(res, calc(imosW, imosB))
    }

    println(res)
}
