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 count(ps: Seq[Record], i: Int, j: Int): Int = ps.count {
    case (x, y) =>
      val xi = (x + i) % k2
      val yj = (y + j) % k2
      ( (xi < k && yj < k) || (xi >= k && yj >= k))
  }
  var max = 0
  (1 to k).foreach { i =>
    (1 to k).foreach { j => 
      val cnt = count(list, i, j)
      max = Math.max(Math.max(cnt, n - cnt), max)
    }
  }
  print(max)
}
