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 C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

/*Coded by::
    **Avinash Tiwary**
    **BE/10298/2015**
    **Production Engineer**
    **Producing <code>**
*/
#include<bits/stdc++.h>
#define buf ios_base::sync_with_stdio (0), cin.tie (0)
typedef long long ll;
typedef double dob;
#define MAX 50010
#define M5 100009
#define M6 2000009
#define M 1000000007
#define inf LLONG_MAX
using namespace std;
typedef vector<ll> V;
typedef queue<ll > Q;
typedef stack<ll> S;
typedef pair<ll,ll> P;
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define pb push_back
ll fac[M6],not_prime[M6];
ll power(ll x,ll n){
    if(n==0) return 1;
    /*while(n){
        if(n%2) x=(((x*x)%M)*x)%M; 
        else x=(x*x)%M;
        n/=2;
    }*/
    if(n%2 == 0) return power((x*x),n/2);
    else return (x*power((x*x),(n-1)/2));
}
void fact(){
    fac[0]=1;
    for(ll i=1;i<M6;i++) fac[i]=(i*fac[i-1])%M;
}
void sieve(){
    not_prime[1]=1; not_prime[0]=1;
    for(ll p=2;p*p<M6;p++){
        if(!not_prime[p]){
            for(ll i=p*2;i<=M6;i+=p) not_prime[i]=1;
        }
    }
}
ll modi(ll a, ll m){
    ll m0 = m, t, q; ll x0 = 0, x1 = 1;
    if (m == 1) return 0;
    while (a > 1){  q = a / m; t = m; m = a % m, a = t;t = x0;x0 = x1 - q * x0; x1 = t;}
    if (x1 < 0) x1 += m0;return x1;
}
ll n,m,ar[M6],x[M6],y[M6];
char col[M6];
int main(){
    buf; 
    //fact();
    //sieve();
    ll i,j,k,l,test,flag,ans,a,b; string s1,s2;
    //cin>>test;
    test=1;
    while(test--){
    	cin>>n>>k;
    	for(i=1;i<=n;i++) cin>>x[i]>>y[i]>>col[i];
    	ans=0;
    	a=x[1],b=y[1];
    	flag=1;
    	for(j=2;j<=n;j++){
    		if(abs(x[j]-a)%(k*2)>k-1&&abs(y[j]-b)%(k*2)>k-1){
    			if(col[1]==col[j]) flag++;
    		}
    		else if(abs(x[j]-a)%(k*2)<k&&abs(y[j]-b)%(k*2)<k){
    			if(col[1]==col[j]) flag++;
    		}
    		else{
    			if(col[1]!=col[j]) flag++;
    		}
    	}
    	ans=max(ans,flag);
    	flag=0;
    	for(j=2;j<=n;j++){
    		if(abs(x[j]-a)%(k*2)>k-1&&abs(y[j]-b)%(k*2)>k-1){
    			if(col[1]!=col[j]) flag++;
    		}
    		else if(abs(x[j]-a)%(k*2)<k&&abs(y[j]-b)%(k*2)<k){
    			if(col[1]!=col[j]) flag++;
    		}
    		else{
    			if(col[1]==col[j]) flag++;
    		}
    	}
    	ans=max(ans,flag);
    	
    	a=x[1],b=y[1]-k+1;
    	flag=1;
    	for(j=2;j<=n;j++){
    		if(abs(x[j]-a)%(k*2)>k-1&&abs(y[j]-b)%(k*2)>k-1){
    			if(col[1]==col[j]) flag++;
    		}
    		else if(abs(x[j]-a)%(k*2)<k&&abs(y[j]-b)%(k*2)<k){
    			if(col[1]==col[j]) flag++;
    		}
    		else{
    			if(col[1]!=col[j]) flag++;
    		}
    	}
    	ans=max(ans,flag);
    	flag=0;
    	for(j=2;j<=n;j++){
    		if(abs(x[j]-a)%(k*2)>k-1&&abs(y[j]-b)%(k*2)>k-1){
    			if(col[1]!=col[j]) flag++;
    		}
    		else if(abs(x[j]-a)%(k*2)<k&&abs(y[j]-b)%(k*2)<k){
    			if(col[1]!=col[j]) flag++;
    		}
    		else{
    			if(col[1]==col[j]) flag++;
    		}
    	}
    	ans=max(ans,flag);

    	a=x[1]-k+1,b=y[1];
    	flag=1;
    	for(j=2;j<=n;j++){
    		if(abs(x[j]-a)%(k*2)>k-1&&abs(y[j]-b)%(k*2)>k-1){
    			if(col[1]==col[j]) flag++;
    		}
    		else if(abs(x[j]-a)%(k*2)<k&&abs(y[j]-b)%(k*2)<k){
    			if(col[1]==col[j]) flag++;
    		}
    		else{
    			if(col[1]!=col[j]) flag++;
    		}
    	}
    	ans=max(ans,flag);
    	flag=0;
    	for(j=2;j<=n;j++){
    		if(abs(x[j]-a)%(k*2)>k-1&&abs(y[j]-b)%(k*2)>k-1){
    			if(col[1]!=col[j]) flag++;
    		}
    		else if(abs(x[j]-a)%(k*2)<k&&abs(y[j]-b)%(k*2)<k){
    			if(col[1]!=col[j]) flag++;
    		}
    		else{
    			if(col[1]==col[j]) flag++;
    		}
    	}
    	ans=max(ans,flag);
    	
    	a=x[1]-k+1,b=y[1]-k+1;
    	flag=1;
    	for(j=2;j<=n;j++){
    		if(abs(x[j]-a)%(k*2)>k-1&&abs(y[j]-b)%(k*2)>k-1){
    			if(col[1]==col[j]) flag++;
    		}
    		else if(abs(x[j]-a)%(k*2)<k&&abs(y[j]-b)%(k*2)<k){
    			if(col[1]==col[j]) flag++;
    		}
    		else{
    			if(col[1]!=col[j]) flag++;
    		}
    	}
    	ans=max(ans,flag);
    	flag=0;
    	for(j=2;j<=n;j++){
    		if(abs(x[j]-a)%(k*2)>k-1&&abs(y[j]-b)%(k*2)>k-1){
    			if(col[1]!=col[j]) flag++;
    		}
    		else if(abs(x[j]-a)%(k*2)<k&&abs(y[j]-b)%(k*2)<k){
    			if(col[1]!=col[j]) flag++;
    		}
    		else{
    			if(col[1]==col[j]) flag++;
    		}
    	}
    	ans=max(ans,flag);
    	cout<<ans;

    }
    return 0;
}