#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, max = 0;
  cin >> n >> k;
  long long int ox, oy;
  long long int a[n][2];
  char c[n], oc, ocp;
  for (int i = 0; i < n; i++) {
    cin >> a[i][0] >> a[i][1] >> c[i];
  }
  ox = a[0][0];
  oy = a[0][1];
  oc = c[0];
  (oc == 'W') ? ocp = 'B' : ocp = 'W';
  for (int x = 0; x < k; x++) {
    for (int y = 0; y < k; y++) {
      int ans = 0;
      for (int i = 0; i < n; i++) {
        int x1 = (a[i][0] - ox - x) % (2 * k);
        int y1 = (a[i][1] - oy - y) % (2 * k);
        if (x1 < 0) x1 += 2 * k;
        if (y1 < 0) y1 += 2 * k;
        char bx, by;
        if (x1 < k)
          bx = oc;
        else
          bx = ocp;
        if (y1 < k)
          by = oc;
        else
          by = ocp;
        char color;
        if (bx == by)
          color = 'W';
        else
          color = 'B';
        if (color == c[i]) {
          ans++;
          if (max < ans) max = ans;
        }
      }
    }
  }
  cout << max << "\n";
}
