import scala.language.postfixOps

object Main {
  def main(args: Array[String]): Unit = {
    val Array(n, k) = io.StdIn.readLine().split(' ') map { _.toInt }

    val inputs = Stream.continually( io.StdIn.readLine().split(' ') ) take n
    val whites = inputs.filter( x => x(2) == "W" ) map { _.take(2) map { _.toInt } }
    val blacks = inputs.filter( x => x(2) == "B" ) map { _.take(2) map { _.toInt } }

    def eval_offset(x:Int, y:Int):Int =
      return (whites map { a: Array[Int] =>
        (Array((a(0) + x) / k, (a(1) + y) / k) sum) % 2
      } filter ({
        _ == 1
      }) length) +
        (blacks map { a: Array[Int] =>
        (Array((a(0) + x) / k, (a(1) + y) / k) sum) % 2
      } filter ({
        _ == 0
      }) length)

    var max = 0
    for (x <- 0 to k-1; y <- 0 to 2*k-1) {
      val a = eval_offset(x, y)
      if (a > max)
        max = a
    }

    println(max)
  }
}