object Main{
  def main(args: Array[String]) {
    val sc = new java.util.Scanner(System.in)
    val N,K = sc.next.toInt
    val imos = new Imos(K)

    for (i <- 0 until N) {
      var x = sc.next.toInt
      var y = sc.next.toInt
      if (sc.next == "B") x += K
      x %= 2*K; y %= 2*K

      val x_ = x % K
      val y_ = y % K

      if ((x < K && y < K) || (x >= K && y >= K)) {
        imos.update(0, 0, x_ + 1, y_ + 1)
        imos.update(x_ + K + 1, 0, 2 * K, y_ + 1)
        imos.update(0, y_ + K + 1, x_ + 1, 2 * K)
        imos.update(x_ + K + 1, y_ + K + 1, 2 * K, 2 * K)
        imos.update(x_ + 1, y_ + 1, x_ + K + 1, y_ + K + 1)
      }
      else {
        imos.update(x_ + 1, 0, x_ + K + 1, y_ + 1)
        imos.update(x_ + 1, y_ + K + 1, x_ + K + 1, 2 * K)
        imos.update(0, y_ + 1, x_ + 1, y_ + K + 1)
        imos.update(x_ + K + 1, y_ + 1, 2 * K, y_ + K + 1)
      }
    }
    println(imos.result)
  }

  class Imos(K : Int) {
    val mat = Array.ofDim[Int](K*2+1, K*2+1)

    def max(a:Int, b:Int) : Int = if (a > b) a else b

    def update(x:Int, y:Int, x2:Int, y2:Int) {
      mat(x)(y) += 1
      mat(x)(y2) -= 1
      mat(x2)(y) -= 1
      mat(x2)(y2) += 1
    }

    def result : Int = {
      for (i <- 0 until K; j <- 1 until K) {
        mat(i)(j) += mat(i)(j - 1)
      }

      for (i <- 1 until K; j <- 0 until K) {
        mat(i)(j) += mat(i - 1)(j)
      }

      var m = 0
      for (i <- 0 until 2*K; j <- 0 until 2*K) {
        m = max(m, mat(i)(j))
      }
      m
    }

  }
}
