object Main extends App {
  val Array(n, k) = io.StdIn.readLine().split(' ').map(_.toInt)
  val k2 = k * 2
  val list = collection.mutable.ListBuffer[Array[Int]]()
  var (sum, white) = (0, 0)
  (1 to n).foreach { i =>
    val temp = io.StdIn.readLine().split(' ')
    list += (
      if (temp(2) == "B") {
        sum = sum + 1
        Array(temp(0).toInt % k2, temp(1).toInt % k2, 1)
      }
      else {
        sum = sum - 1
        white = white + 1
        Array(temp(0).toInt % k2, temp(1).toInt % k2, -1)
      }
    )
  }
  val matrix = new Array[Array[Int]](k2)
  (0 to k2 - 1).foreach(i => matrix(i) = new Array[Int](k2))
  def inc(x: Int, y: Int, z: Int) = {
    for {
      i <- (x to k2-1)
      j <- (y to k2-1)
    } {
      matrix(i)(j) = matrix(i)(j) + z
    }
  }
  list.foreach(arr => inc(arr(0), arr(1), arr(2)))
  val cnt =
      (matrix(k-1)(k-1)) + matrix(i+k)(j) +
      (matrix(k2-1)(k2-1) - matrix(k-1)(k2-1) - matrix(k2-1)(k-1) + matrix(k-1)(k-1))

  var max = Math.max(cnt, sum - cnt)
  for { 
    i <- (0 to k-1)
    j <- (0 to k-1)
  } {
    val cnt = 
      (matrix(i)(j)) +
      (matrix(i+k)(j+k) - matrix(i+k)(j) - matrix(i)(j+k) + matrix(i)(j)) +
      (matrix(k2-1)(j) - matrix(i+k)(j)) +
      (matrix(i)(k2-1) - matrix(i)(j+k)) +
      (matrix(k2-1)(k2-1) - matrix(k2-1)(j+k) - matrix(i+k)(k2-1) + matrix(i+k)(j+k))
    max = Math.max(Math.max(cnt, sum - cnt), max)
  }
  print(max + white)
}
