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)

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <queue>

using namespace std;

#define TRACE(x) cerr << #x << " " << x << endl
#define FOR(i, a, b) for (int i = (a); i < int(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define _ << " " <<

#define fst first
#define snd second

typedef long long llint;
typedef pair<int, int> pii;

const int B = 27397, MOD = 1e9 + 7;
const int B1 = 33941, MOD1 = 1e9 + 9;

const int MAXN = 1e5 + 10;
const int MAXK = 1010;

bool want_change[MAXN];

int N, K;
int change[MAXK][MAXK], no_change[MAXK][MAXK];
int pref_change[MAXK][MAXK], pref_no_change[MAXK][MAXK];

void init_pref(int (&A)[MAXK][MAXK], int (&pref_A)[MAXK][MAXK]) {
  for (int i = 1; i <= K + 1; ++i)
    for (int j = 1; j <= K + 1; ++j)
      pref_A[i][j] = pref_A[i - 1][j] + pref_A[i][j - 1] - pref_A[i - 1][j - 1] + A[i][j];
}

int get_sum(int r1, int s1, int r2, int s2, int (&pref_A)[MAXK][MAXK]) {
  return pref_A[r2][s2] - pref_A[r2][s1 - 1] - pref_A[r1 - 1][s2] + pref_A[r1 - 1][s1 - 1];
}

int get_changed(int dr, int ds, int (&pref_A)[MAXK][MAXK]) {
  return get_sum(1, 1, dr, K + 1, pref_A) + get_sum(1, 1, K + 1, ds, pref_A) - get_sum(1, 1, dr, ds, pref_A);
}

int main(void) {
  scanf("%d%d", &N, &K);

  int start = 0, sol = 0;
  for (int i = 0; i < N; ++i) {
    int r, s;
    char col[2];
    scanf("%d%d%s", &r, &s, col);

    bool want_col = col[0] == 'B';
    bool actual_col = (r / K) % 2 != (s / K) % 2;

    want_change[i] = want_col != actual_col;
    start += !want_change[i];

    if (want_change[i])
      ++change[(r % K) + 2][(s % K) + 2];
    else
      ++no_change[(r % K) + 2][(s % K) + 2];
  }

//  for (int i = 0; i <= K + 1; ++i) {
//    for (int j = 0; j <= K + 1; ++j) {
//      printf("%d ", change[i][j]);
//    }
//    printf("\n");
//  }
//  printf("\n");
//
//  for (int i = 0; i <= K + 1; ++i) {
//    for (int j = 0; j <= K + 1; ++j) {
//      printf("%d ", no_change[i][j]);
//    }
//    printf("\n");
//  }


  init_pref(change, pref_change);
  init_pref(no_change, pref_no_change);

  sol = start;
  for (int dr = 0; dr <= K; ++dr) {
    for (int ds = 0; ds <= K; ++ds) {
      int curr = start + get_changed(dr + 1, ds + 1, pref_change) - get_changed(dr + 1, ds + 1, pref_no_change);
      sol = max(sol, max(curr, N - curr));
    }
  }

  printf("%d\n", sol);

  return 0;
}