Problem:
Score : 500 points

Problem Statement

AtCoDeer is thinking of painting an infinite two-dimensional grid in a checked pattern of side K.
Here, a checked pattern of side K is a pattern where each square is painted black or white so that each connected component of each color is a K × K square.
Below is an example of a checked pattern of side 3:

AtCoDeer has N desires.
The i-th desire is represented by x_i, y_i and c_i.
If c_i is B, it means that he wants to paint the square (x_i,y_i) black; if c_i is W, he wants to paint the square (x_i,y_i) white.
At most how many desires can he satisfy at the same time?

Constraints

1 ≤ N ≤ 10^5

1 ≤ K ≤ 1000

0 ≤ x_i ≤ 10^9

0 ≤ y_i ≤ 10^9

If i ≠ j, then (x_i,y_i) ≠ (x_j,y_j).

c_i is B or W.

N, K, x_i and y_i are integers.

Input

Input is given from Standard Input in the following format:

N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N

Output

Print the maximum number of desires that can be satisfied at the same time.

Sample Input 1

4 3
0 1 W
1 2 W
5 3 B
5 4 B

Sample Output 1

4

He can satisfy all his desires by painting as shown in the example above.

Sample Input 2

2 1000
0 0 B
0 1 W

Sample Output 2

2

Sample Input 3

6 2
1 2 B
2 1 W
2 2 B
1 0 B
0 6 W
4 5 W

Sample Output 3

4

Solution in Common Lisp (SBCL 1.1.14) (judge verdict: Wrong Answer)

;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (progn (ql:quickload '(:cl-debug-print :fiveam))
                 (shadow :run)
                 (use-package :fiveam)))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  (declare #.OPT)
  (macrolet ((%read-byte ()
               `(the (unsigned-byte 8)
                     #+swank (char-code (read-char in nil #\Nul))
                     #-swank (sb-impl::ansi-stream-read-byte in nil #.(char-code #\Nul) nil))))
    (let* ((minus nil)
           (result (loop (let ((byte (%read-byte)))
                           (cond ((<= 48 byte 57)
                                  (return (- byte 48)))
                                 ((zerop byte) ; #\Nul
                                  ;; (return-from read-fixnum 0)
                                  (error "Read EOF or #\Nul."))
                                 ((= byte #.(char-code #\-))
                                  (setf minus t)))))))
      (declare ((integer 0 #.most-positive-fixnum) result))
      (loop
        (let* ((byte (%read-byte)))
          (if (<= 48 byte 57)
              (setq result (+ (- byte 48) (* 10 (the (integer 0 #.(floor most-positive-fixnum 10)) result))))
              (return (if minus (- result) result))))))))

(defmacro dbg (&rest forms)
  #+swank
  (if (= (length forms) 1)
      `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

(defconstant +mod+ 1000000007)

;; Body

(declaim (inline get-2dcumul))
(defun get-2dcumul (table y0 x0 y1 x1)
  (+ (- (aref table y1 x1)
        (aref table y0 x1)
        (aref table y1 x0))
     (aref table y0 x0)))

(declaim (inline fast-read-char))
(defun fast-read-char (&optional (in *standard-input*))
  #-swank (declare (inline read-byte)
                   (sb-kernel:ansi-stream in))
  #-swank (code-char (read-byte in))
  #+swank (read-char in))

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (k (read))
         (btable (make-array (list (+ 1 (* 3 k)) (+ 1 (* 3 k)))
                             :element-type 'uint32 :initial-element 0))
         (wtable (make-array (list (+ 1 (* 3 k)) (+ 1 (* 3 k)))
                             :element-type 'uint32 :initial-element 0)))
    (declare (uint32 n k))
    (dotimes (i n)
      (let* ((x (+ 1 (mod (read-fixnum) (* 2 k)))) ;; updated point
             (y (+ 1 (mod (read-fixnum) (* 2 k))))
             (table (if (char= (fast-read-char) #\B) btable wtable)))
        (incf (aref table y x))
        (when (<= (+ x (* 2 k)) (* 3 k))
          (incf (aref table y (+ x (* 2 k))))
          (when (<= (+ y (* 2 k)) (* 3 k))
            (incf (aref table (+ y (* 2 k)) (+ x (* 2 k))))))
        (when (<= (+ y (* 2 k)) (* 3 k))
          (incf (aref table (+ y (* 2 k)) x)))))
    (dotimes (x (+ 1 (* 3 k)))
      (dotimes (y (* 3 k))
        (incf (aref btable (+ y 1) x) (aref btable y x))
        (incf (aref wtable (+ y 1) x) (aref wtable y x))))
    (dotimes (y (+ 1 (* 3 k)))
      (dotimes (x (* 3 k))
        (incf (aref btable y (+ x 1)) (aref btable y x))
        (incf (aref wtable y (+ x 1)) (aref wtable y x))))
    (let ((res 0))
      (declare (uint32 res))
      (dotimes (x k)
        (dotimes (y k)
          (let ((value (+ (get-2dcumul btable y x (+ y k) (+ x k))
                          (get-2dcumul btable (+ y k) (+ x k) (+ y k k) (+ x k k))
                          (get-2dcumul wtable y (+ x k) (+ y k) (+ x k k))
                          (get-2dcumul wtable (+ y k) x (+ y k k) (+ x k)))))
            (setf res (max res value)))))
      (println res))))

#-swank(main)