object Main extends App {
  val Array(n, k) = io.StdIn.readLine().split(' ').map(_.toInt)
  val k2 = k * 2
  val list = collection.mutable.ListBuffer[Array[Int]]()
  (1 to n).foreach { i =>
    val temp = io.StdIn.readLine().split(' ')
    list += (
      if (temp(2) == "W")
        Array(temp(0).toInt % k2, temp(1).toInt % k2)
      else
        Array(temp(0).toInt % k2, (temp(1).toInt + k) % k2)
    )
  }
  var max = (for { 
      i <- (0 to k - 1)
      j <- (0 to k - 1)
    } yield {
      val cnt = list.count { arr =>
          val x = (arr(0) + i) % k2
          val y = (arr(1) + j) % k2
          ( (x < k && y < k) || (x >= k && y >= k))
      }
      Math.max(cnt, n - cnt)
    }).max
  print(max)
}
