fun main(args: Array<String>) {
  val (n, k) = readLine()!!.split(" ").map { it.toInt() }
  val board = Array(2 * k, { IntArray(2 * k) })

  repeat(n) {
    val (xString, yString, c) = readLine()!!.split(" ")
    val x = xString.toInt()
    val y = yString.toInt()
    if (c == "B") {
      board[y % (2 * k)][x % (2 * k)]++
    } else {
      board[y % (2 * k)][(x + k) % (2 * k)]++
    }
  }
  // board.forEach { println(it.asList()) }

  val acc = Array(2 * k + 1, { IntArray(2 * k + 1 )})
  acc[0][0] = board[0][0]
  for (y in 1 until 2 * k + 1) {
    for (x in 1 until 2 * k + 1) {
      acc[y][x] = acc[y][x - 1] + acc[y - 1][x] - acc[y - 1][x - 1] + board[y - 1][x - 1]
    }
  }
  // acc.forEach { println(it.asList()) }

  fun getRectangle(ix: Int, iy: Int, ex: Int, ey: Int): Int =
      acc[ey][ex] - acc[ey][ix] - acc[iy][ex] + acc[iy][ix]

  var max = 0
  for (iy in 0 until k) {
    for (ix in 0 until 2 * k) {
      if (ix < k) {
        var count = 0
        count += getRectangle(0, 0, ix, iy)
        count += getRectangle(0, iy + k, ix, 2 * k)
        count += getRectangle(ix, iy, ix + k, iy + k)
        count += getRectangle(ix + k, 0, 2 * k, iy)
        count += getRectangle(ix + k, iy + k, 2 * k, 2 * k)
        max = Math.max(max, count)
      } else {
        var count = 0
        count += getRectangle(0, iy, ix - k, iy + k)
        count += getRectangle(ix - k, 0, ix, iy)
        count += getRectangle(ix - k, iy + k, ix, 2 * k)
        count += getRectangle(ix, iy, 2 * k, iy + k)
        max = Math.max(max, count)
      }
    }
  }
  println(max)
}
