object Main extends App {
  val Array(n, k) = io.StdIn.readLine().split(' ').map(_.toInt)
  val k2 = k * 2
  var (sum, white) = (0, 0)
  val matrix = Array.fill(k2)(Array.fill(k2)(0))
  (0 to k2-1).foreach(i => matrix(i) = new Array[Int](k2))
  (0 to n-1).foreach { i =>
    val temp = io.StdIn.readLine().split(' ')
      if (temp(2) == "B") {
        sum = sum + 1
        matrix(temp(0).toInt % k2)(temp(1).toInt % k2) = 1
      }
      else {
        sum = sum - 1
        white = white + 1
        matrix(temp(0).toInt % k2)(temp(1).toInt % k2) = -1
      }
  }
  // for {
  //   i <- (0 to k2-1)
  //   j <- (1 to k2-1)
  // } matrix(i)(j) += matrix(i)(j-1)
  // for {
  //   j <- (0 to k2-1)
  //   i <- (1 to k2-1)
  // } matrix(i)(j) += matrix(i-1)(j)
  (0 to k2-1).foreach(i => (1 to k2-1).foreach(j => matrix(i)(j) += matrix(i)(j-1)))
  (0 to k2-1).foreach(j => (1 to k2-1).foreach(i => matrix(i)(j) += matrix(i-1)(j)))
  val cnt =
    (matrix(k-1)(k-1)) +
    (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)
}
