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++ (GCC 9.2.1) (judge verdict: Accepted)

#include <bits/stdc++.h>

template <typename T, typename = void>
struct is_iterable : std::false_type {};
template <typename T>
struct is_iterable<T, std::void_t<decltype(std::begin(std::declval<T>())),
                                  decltype(std::end(std::declval<T>()))>>
    : std::true_type {};

template <typename T, typename = void>
struct is_pair : std::false_type {};
template <typename T>
struct is_pair<T, std::void_t<decltype(std::declval<T>().first),
                              decltype(std::declval<T>().second)>>
    : std::true_type {};

template <typename T>
void debug(const T& v) {
  if constexpr (is_pair<T>::value) {
    std::cerr << "{";
    debug(v.first);
    std::cerr << ", ";
    debug(v.second);
    std::cerr << "}";
  } else if constexpr (is_iterable<T>::value &&
                       !std::is_same<T, std::string>::value) {
    std::cerr << "{";
    for (auto it = v.begin(); it != v.end(); ++it) {
      if (it != v.begin()) std::cerr << ", ";
      debug(*it);
    }
    std::cerr << "}";
  } else {
    std::cerr << v;
  }
}
template <typename T, typename... Ts>
void debug(const T& value, const Ts&... args) {
  debug(value);
  std::cerr << ", ";
  debug(args...);
}
#if DEBUG
#define dbg(...)                        \
  do {                                  \
    cerr << #__VA_ARGS__ << ": ";       \
    debug(__VA_ARGS__);                 \
    cerr << " (L" << __LINE__ << ")\n"; \
  } while (0)
#else
#define dbg(...)
#endif

void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T& value, Ts&... args) {
  std::cin >> value;
  read_from_cin(args...);
}
#define rd(type, ...) \
  type __VA_ARGS__;   \
  read_from_cin(__VA_ARGS__);
#define ints(...) rd(int, __VA_ARGS__);
#define strings(...) rd(string, __VA_ARGS__);

template <typename T>
void write_to_cout(const T& value) {
  if constexpr (std::is_same<T, bool>::value) {
    std::cout << (value ? "Yes" : "No");
  } else {
    std::cout << value;
  }
}
template <typename T, typename... Ts>
void write_to_cout(const T& value, const Ts&... args) {
  write_to_cout(value);
  std::cout << ' ';
  write_to_cout(args...);
}
#define wt(...)                 \
  do {                          \
    write_to_cout(__VA_ARGS__); \
    cout << '\n';               \
  } while (0)

#define all(x) (x).begin(), (x).end()

#define rep_dispatch(_1, _2, _3, name, ...) name
#define rep3(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep2(i, n) rep3(i, 0, n)
#define rep1(n) rep2(_loop_variable_, n)
#define rep(...) rep_dispatch(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)

#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)

template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& v) {
  for (T& vi : v) is >> vi;
  return is;
}

template <typename T, typename U>
bool chmax(T& a, U b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T, typename U>
bool chmin(T& a, U b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T>
int sz(const T& v) {
  return v.size();
}

#include <boost/hana/functional/fix.hpp>
const auto& Fix = boost::hana::fix;

using ll = long long;

template <typename T>
using low_priority_queue =
    std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <typename T>
using V = std::vector<T>;
template <typename T>
using VV = V<V<T>>;

using namespace std;

int main() {
  ints(n, k);
  V<int> x(n), y(n);
  V<char> c(n);
  rep(i, n) cin >> x[i] >> y[i] >> c[i];

  auto solve = [&]() {
    int k2 = k * 2;
    VV<int> grid(k2, V<int>(k2));
    rep(i, n) {
      int xi = x[i], yi = y[i];
      if (c[i] == 'W') {
        xi += k;
      }
      xi %= k2;
      yi %= k2;
      ++grid[xi][yi];
      xi = (xi + k) % k2;
      yi = (yi + k) % k2;
      ++grid[xi][yi];
    }

    VV<int> sum(k2 + 1, V<int>(k2 + 1));
    rep(i, k2) rep(j, k2) {
      sum[i + 1][j + 1] =
          sum[i][j + 1] + sum[i + 1][j] - sum[i][j] + grid[i][j];
    }

    int ans = 0;
    rep(i, k, k2 + 1) rep(j, k, k2 + 1) chmax(
        ans, sum[i][j] - sum[i - k][j] - sum[i][j - k] + sum[i - k][j - k]);
    return ans;
  };

  auto solve2 = [&]() {
    int k2 = k * 4;
    VV<int> grid(k2, V<int>(k2));
    rep(i, n) {
      int xi = x[i], yi = y[i];
      if (c[i] == 'W') {
        xi += k;
      }
      xi %= k2;
      yi %= k2;
      set<pair<int, int>> seen;
      auto add = [&](int a, int b) {
        if (seen.insert({a, b}).second) {
          ++grid[a][b];
        }
      };
      rep(i, 4) rep(j, 4) if ((i + j) % 2 == 0) {
        add((xi + k * i) % k2, (yi + k * j) % k2);
      }
    }

    VV<int> sum(k2 + 1, V<int>(k2 + 1));
    rep(i, k2) rep(j, k2) {
      sum[i + 1][j + 1] =
          sum[i][j + 1] + sum[i + 1][j] - sum[i][j] + grid[i][j];
    }

    int ans = 0;
    rep(i, k, k2 + 1) rep(j, k, k2 + 1) chmax(
        ans, sum[i][j] - sum[i - k][j] - sum[i][j - k] + sum[i - k][j - k]);
    return ans;
  };

  auto naive = [&]() {
    int ans = 0;
    rep(white, 2) rep(dx, k) rep(dy, k) {
      int sum = 0;
      rep(i, n) {
        int cx = x[i] + dx;
        int cy = y[i] + dy;
        char col = ((cx / k) + (cy / k)) % 2 == white ? 'W' : 'B';
        if (col == c[i]) ++sum;
      }
      chmax(ans, sum);
    }
    return ans;
  };

  wt(solve2());
  return 0;

  rep(trial, 100) {
    n = 100;
    k = 10;
    x.clear();
    y.clear();
    c.clear();
    rep(i, n) {
      x.push_back(rand() % 10000);
      y.push_back(rand() % 10000);
      c.push_back('B');
    }
    if (solve2() != naive()) {
      dbg(trial);
      dbg(n, k);
      dbg(x);
      dbg(y);
      dbg(c);
      dbg(solve(), solve2(), naive());
    }
  }
}