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