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