function Main(input) {
 	var head = input[0].split(" ");
	var N = parseInt(head[0], 10);
	var k = parseInt(head[1], 10);
  
   var result;
  
   var block=[];
   for(var i=0;i<k;i++){
       block[i]=[];
     for(var j=0;j<k;j++){
       block[i][j]="B";
     }
     for(var j=0;j<k;j++){
       block[i][k+j]="W";
     }
   }
   for(var i=k;i<2*k;i++){
       block[i]=[];
     for(var j=0;j<k;j++){
       block[i][j]="W";
     }
     for(var j=0;j<k;j++){
       block[i][k+j]="B";
     }
   }

   console.log(block);


   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[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;
        }
      }
    }

    //反転
    for(var i=0;i<2*k;i++){
      for(var j=0;j<2*k;j++){
       block[i][j]= block[i][j]=="B"?"W":"B";
      }
    }

	//反転後チェック
    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[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;
        }
      }
    }

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

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