fun main(args: Array<String>) {
    val (N, K) = listOfInt()
    val (k2, k4) = listOf(K * 2, K * 4)
    val B = Array(k2 + 1) { IntArray(k2 + 1) }
    repeat(N) {
        val xyc = listOfString()
        val c = xyc[0].toInt() % k2
        val r = xyc[1].toInt() % k2
        if (xyc[2] == "B") B[r][c] += 1 else B[r][(c + K) % k2] += 1
    }
    val S = Array(k4 + 1) { IntArray(k4 + 1) }
    for (r in 0 until k4) for (c in 0 until k4) S[r][c + 1] = S[r][c] + B[r % k2][c % k2]
    for (r in 0 until k4) for (c in 0 .. k4) S[r + 1][c] += S[r][c]
    var ans = 0
    fun num(r: Int, c: Int) = S[r + K][c + K] - S[r][c + K] - S[r + K][c] + S[r][c]
    for (r in 0 .. k2) for (c in 0 .. k2) {
        val b = num(r, c) + num(r + K, c + K)
        ans = Math.max(ans, b)
    }
    println(ans)
}

fun next() = readLine()!!
fun nextInt() = Integer.parseInt(next())
fun nextInt(offset: Int = 0) = nextInt() + offset
fun nextLong() = java.lang.Long.parseLong(next())
fun nextLong(offset: Long = 0L) = nextLong() + offset
fun nextDouble() = next().toDouble()
fun listOfString() = next().split(" ")
fun listOfInt() = listOfString().map { Integer.parseInt(it) }

