object Main extends App {
  // your code goes here

  val Array(n, k) = scala.io.StdIn.readLine().split(' ').map(_.toInt)

  def score(lst:List[Tuple3[Long, Long, String]], ox:Int, oy:Int):Int = {
    val ret = lst.foldLeft(0)((sum, v) => {
      val (x, y, c) = v
      val c1 = (((x - ox)/k + (y-oy)/k) & 1) match {
        case 0 => "W"
        case 1 => "B"
      }
      c1 match {
        case bw if bw == c => sum + 1
        case bw => sum
      }
    })
    ret match {
      case ret if ret < n/2 => n-ret
      case ret => ret
    }
  }

  def solve(lst:List[Tuple3[Long, Long, String]]):Int = {
    (for(ox <- 0 until k; oy <- 0 until k) yield score(lst, ox, oy)).max
  }

  val lst = (1 to n).toList.map(i => {
    val l = scala.io.StdIn.readLine()
    val Array(x, y, c) = l.split(' ')
    Tuple3(x.toLong % (2*k), y.toLong % (2*k), c)
  }).toList

  println(solve(lst))
}

