#include <bits/stdc++.h>
using namespace std;
pair<pair<int, int>, pair<int, int> > new_rect(int x1, int y1, int x2, int y2) {
  pair<int, int> p1 = pair<int, int>(x1, y1);
  pair<int, int> p2 = pair<int, int>(x2, y2);
  return pair<pair<int, int>, pair<int, int> >(p1, p2);
}
vector<pair<pair<int, int>, pair<int, int> > > points(int cx, int cy, int K) {
  vector<pair<pair<int, int>, pair<int, int> > > res;
  int x1 = max(0, cx - K);
  int y1 = max(0, cy - K);
  int x2 = cx;
  int y2 = cy;
  res.push_back(new_rect(x1, y1, x2, y2));
  x2 = min(2 * K, cx + K), y2 = min(2 * K, cy + K);
  x1 = cx, y1 = cy;
  res.push_back(new_rect(x1, y1, x2, y2));
  if (K < cy) {
    x2 = min(2 * K, cx + K), y2 = cy - K;
    x1 = cx, y1 = max(0, y2 - K);
    res.push_back(new_rect(x1, y1, x2, y2));
  }
  if (cx < K) {
    x2 = min(2 * K, cx + 2 * K), y2 = cy;
    x1 = cx + K, y1 = max(0, cy - K);
    res.push_back(new_rect(x1, y1, x2, y2));
  }
  if (K < cx) {
    x2 = cx - K, y2 = min(2 * K, cy + K);
    x1 = max(0, x2 - K), y1 = cy;
    res.push_back(new_rect(x1, y1, x2, y2));
  }
  if (K < cx && K < cy) {
    x2 = cx - K, y2 = cy - K;
    x1 = max(0, x2 - K), y1 = max(0, y2 - K);
    res.push_back(new_rect(x1, y1, x2, y2));
  }
  return res;
}
int white[2001][2001];
int checker(int N, int K, vector<pair<pair<int, int>, char> > C) {
  memset(white, 0, sizeof(white));
  for (pair<pair<int, int>, char> pc : C) {
    pair<int, int> p = pc.first;
    int x = p.first, y = p.second;
    char c = pc.second;
    if (c == 'B') x += K;
    int tx = (x % (2 * K)) + 1;
    int ty = (x % (2 * K)) + 1;
    white[tx][ty] += 1;
  }
  for (int ky = 0; ky < 2 * K; ky++)
    for (int kx = 0; kx < 2 * K; kx++)
      white[ky + 1][kx + 1] += white[ky + 1][kx];
  for (int ky = 0; ky < 2 * K; ky++)
    for (int kx = 0; kx < 2 * K; kx++)
      white[ky + 1][kx + 1] += white[ky][kx + 1];
  int max_count = 0;
  for (int ky = K; ky < 2 * K; ky++) {
    for (int kx = 0; kx < 2 * K; kx++) {
      int count = 0;
      for (pair<pair<int, int>, pair<int, int> > r : points(kx, ky, K)) {
        int x1 = r.first.first;
        int y1 = r.first.second;
        int x2 = r.second.first;
        int y2 = r.second.second;
        count += white[y2][x2] - white[y2][x1] - white[y1][x2] + white[y1][x1];
      }
      max_count = max(max_count, count);
      if (max_count == N) break;
    }
  }
  return max_count;
}
int main(int argc, char** argv) {
  int N, K;
  cin >> N >> K;
  vector<pair<pair<int, int>, char> > C;
  for (int n = 0; n < N; n++) {
    int x, y;
    char c;
    cin >> x >> y >> c;
    C.push_back(pair<pair<int, int>, char>(pair<int, int>(x, y), c));
  }
  int ans = checker(N, K, C);
  cout << ans << endl;
  return 0;
}
