#include <bits/stdc++.h>
using namespace std;
int cometrue(int x, int y, char c, int offset_x, int offset_y, int K) {
  char this_is_in;
  int amari_x = ((x - offset_x) % (2 * K));
  if (amari_x < 0) {
    amari_x += 2 * K;
  }
  int amari_y = ((y - offset_y) % (2 * K));
  if (amari_y < 0) {
    amari_y += 2 * K;
  }
  int xorr = ((amari_x < K) ^ (amari_y < K));
  if (xorr) {
    this_is_in = 'B';
  } else
    this_is_in = 'W';
  return (this_is_in == c);
}
int main() {
  long N;
  int K;
  cin >> N >> K;
  int *x, *y;
  char *c;
  int i;
  x = new int[N];
  y = new int[N];
  c = new char[N];
  int answer = 0;
  for (i = 0; i < N; i++) {
    cin >> x[i] >> y[i] >> c[i];
  }
  int offset_x;
  int offset_y;
  for (offset_x = 0; offset_x < K; offset_x++) {
    for (offset_y = 0; offset_y < K; offset_y++) {
      int tempans = 0;
      for (i = 0; i < N; i++) {
        tempans += cometrue(x[i], y[i], c[i], offset_x, offset_y, K);
      }
      answer = (answer > tempans) ? answer : tempans;
    }
  }
  cout << answer << endl;
  return 0;
}
