object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val N, K = sc.nextInt
  val XYCs = List.fill(N)((sc.nextInt, sc.nextInt, sc.next))
  val K2 = 2 * K
  val m = Array.fill[Int](K2 + 1, K2 + 1){0}
  XYCs.foreach { t =>
    val x = t._1 % K2
    val y = (t._2 + (if (t._3 == "W") K else 0)) % K2
    m(x + 1)(y + 1) += 1
    m((x + K) % K2 + 1)((y + K) % K2 + 1) += 1
  }
  for(i <- 1 to K2; j <- 1 to K2) m(i)(j) += m(i - 1)(j) + m(i)(j - 1) - m(i - 1)(j - 1)
  val cs = for(i <- 1 to K; j <- 1 to K) yield m(i + K)(j + K) - m(i)(j + K) - m(i + K)(j) + m(i)(j)
  println(Math.max(cs.max, N - cs.min))
}