import java.io._
import java.util.StringTokenizer
import scala.util.control.Breaks.{breakable,break}

object Main extends App {
  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)

  val N, K = in.next().toInt
  val MOD = 2*K
  val dp = Array.ofDim[Long](2*K, 2*K)

  for(_ <- 1 to N) {
    val _x, _y = in.next().toInt
    val c = in.next()
    var x = _x; var y = _y

    if(c == "W"){
      x += K
    }

    dp(y % MOD)(x % MOD) += 1L
  }

  val imos = Array.ofDim[Long](2*K, 2*K)
  for(y <- 0 until 2*K; x <- 0 until 2*K) {
    if(x == 0)
      imos(y)(x) = dp(y)(x)
    else
      imos(y)(x) = dp(y)(x) + imos(y)(x-1)
  }

  

  for(x <- 0 until 2*K; y <- 0 until 2*K) {
    if(y == 0)
      imos(y)(x) = imos(y)(x)
    else
      imos(y)(x) = imos(y)(x) + imos(y-1)(x)
  }


  def safeImos(x: Int, y: Int): Long = {
    if(x < 0 || y < 0) 0
    else imos(y)(x)
  }

  def calc(leftUp: (Int, Int)): Long = {
    var res: Long = 0L
    val (x, y) = leftUp
    val rightDown = (x + K-1, y + K-1)
    val (rx, ry) = rightDown

    if(rx < 2*K && ry < 2*K) {
      res += (imos(ry)(rx) - safeImos(x-1, ry) - safeImos(rx, y-1) + safeImos(x-1, y-1))
    }
    else if(rx >= 2*K && ry >= 2*K) {
      res +=
      (imos(2*K-1)(2*K-1) - safeImos(x-1, 2*K-1) - safeImos(2*K-1, y-1) + safeImos(x-1, y-1)
        + imos(ry % MOD)(rx % MOD)
        + imos(2*K-1)(rx % MOD) - safeImos(rx % MOD, y-1)
        + imos(ry % MOD)(2*K-1) - safeImos(x-1, ry % MOD)
      )
    }
    else if(rx >= 2*K) {
      val tmp = (imos(ry)(2*K-1) - safeImos(x-1, ry) - safeImos(2*K-1, y-1) + safeImos(x-1, y-1) +
        imos(ry)(rx % MOD) - safeImos(rx % MOD, y-1)
      )
      res += tmp
    }
    else if(ry >= 2*K) {
      val tmp =
        (imos(2*K-1)(rx) - safeImos(x-1, 2*K-1) - safeImos(rx, y-1) + safeImos(x-1, y-1)
          + imos(ry % MOD)(rx) - safeImos(x-1, ry % MOD)
        )
      res += tmp
    }

    res
  }

  var ans = 0L
  for(y <- 0 until 2*K; x <- 0 until 2*K) {
    val tmp = calc(x, y) + calc((x+K)%MOD, (y+K)%MOD)
    ans = Math.max(ans, tmp)
  }

  println(ans)
}


class InputReader(stream: InputStream) {
  private val reader = new BufferedReader(new InputStreamReader(stream))
  private var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}
