object Main extends App {
  val Array(n, k) = io.StdIn.readLine().split(' ').map(_.toInt)
  val k2 = k * 2
  val list = for (i <- 1 to n) yield {
    val Array(x, y, color) = io.StdIn.readLine().split(' ')
    Array(x.toInt % k2, (y.toInt + (if (color == "B") 0 else k)) % k2)
  }
  val 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)
}
