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)
    )
  }
  def shift(x: Int): Int = if(x == k2 - 1) 0 else x + 1
  def shiftX(p: Record): Record = (shift(p._1), p._2)
  def shiftY(p: Record): Record = (p._1, shift(p._2))
  def count(ps: Seq[Record]): Int = ps.count {
    case (x, y) => ( (x < k && y < k) || (x >= k && y >= k))
  }

  var max = 0
  var shiftList = list

  (1 to k).foreach { i =>
    (1 to k).foreach { j => 
      val cnt = count(shiftList)
      max = Math.max(Math.max(cnt, n - cnt), max)
      shiftList =  shiftList.map(shiftX)
    }
    shiftList = shiftList.map(shiftY)
  }
  print(max)
}
