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 JavaScript (node.js v5.12) (judge verdict: Time Limit Exceeded)

function block(c,k,i,j) {
  if(0<=i && i<k && 0<=j && j<k){
    return c;
  }else if(k<=i && i<2*k && k<=j && j<2*k){
    return c;
  }
  return c=="B" ? "W":"B";
}

function Main(input) {
 	var head = input[0].split(" ");
	var N = parseInt(head[0], 10);
	var k = parseInt(head[1], 10);
  
  // var result;
  
   var check=[];
  
   //console.log(check);
  
   for(var i=1;i<=N;i++){
      var tmp = input[i].split(" ");
      var x = parseInt(tmp[0], 10)
      var y = parseInt(tmp[1], 10)
      var c = tmp[2]
           
//	  console.log('ORG %s %s %s',x,y,c);
      var new_x = x%k;
      var new_y = y%k;

// 	  console.log('/3 %s %s',((x-new_x)/3)%2,((y-new_y)/3)%2);


      var pattern = (
        			  ((x-new_x)/k)%2
                     +((y-new_y)/k)%2
                     )%2 == 1 ? "R":"N";

//     console.log('pattern %s',pattern);

     
      var new_c = c;
     
      if(pattern == "R"){
        new_c = new_c == "B" ? "W":"B"
      }
        
//      console.log('NEW %s %s %s',new_x,new_y,new_c);
      check[i-1]=[]
      check[i-1][0]=new_x;
      check[i-1][1]=new_y;
      check[i-1][2]=new_c;
   }
  
//    console.log(check);

    var max_match = 0;
  
  　//チェック
    for(var m=0;m<k;m++){
      for(var n=0;n<k;n++){
        
	    var match = 0;
        
        for(var g=0;g<N;g++){
          var i= check[g][0];
          var j= check[g][1];
          if(check[g][2]==block("B",k,m+i,n+j)){
//             console.log('i:%s j:%s m+i:%s n+j:%s',i,j,m+i,n+j);
              match++;
          }
        }
//     	console.log('match:%s m:%s n:%s',match,m,n);

        if (max_match <match){
          max_match = match;
        }
        if(max_match == N){
          break;
        }
      }
      if(max_match == N){
        break;
      }
    }

    if(max_match != N){
      //反転後チェック
      for(var m=0;m<k;m++){
        for(var n=0;n<k;n++){

          var match = 0;

          for(var g=0;g<N;g++){
            var i= check[g][0];
            var j= check[g][1];
            if(check[g][2]==block("W",k,m+i,n+j)){
  //             console.log('i:%s j:%s m+i:%s n+j:%s',i,j,m+i,n+j);

                match++;
            }
          }
  //     	console.log('match:%s m:%s n:%s',match,m,n);

          if (max_match <match){
            max_match = match;
          }
          if(max_match == N){
            break;
          }
        }
        if(max_match == N){
          break;
        }
      }
    }
  
	//出力
//	console.log('max_match %s',max_match);

	console.log('%s',max_match);
}

Main(require("fs").readFileSync("/dev/stdin", "utf8").split("\n"));