object Main extends App {
  val Array(n, k) = io.StdIn.readLine().split(' ').map(_.toInt)
  type Record = (Int, Int, Boolean)
  val list = collection.mutable.ListBuffer[Record]()
  (1 to n).foreach { i =>
    val temp = io.StdIn.readLine().split(' ')
    list += ((temp(0).toInt % (k * 2) , temp(1).toInt % (k * 2), temp(2) == "W"))
  }
  def shift(x: Int): Int = if(x == (k * 2)) 0 else x + 1
  def shiftX(p: Record): Record = (shift(p._1), p._2, p._3)
  def shiftY(p: Record): Record = (p._1, shift(p._2), p._3)
  def count(ps: Seq[Record]): Int = ps.count {
    case (x, y, bool) => ( (x < k && y < k) || (x >= k && y >= k) == bool )
  }

  var max = 0
  var shiftList = list

  (0 to k).foreach { i =>
    (0 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)
}
