#include <bits/stdc++.h>
using namespace std;
int xx;
int yy;
int lt[2050][2050] = {0};
int main() {
  int n, k, x, y;
  char c;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    scanf("%d %d %c", &xx, &yy, &c);
    if (c == 'W') {
      yy += k;
    }
    xx %= 2 * k;
    yy %= 2 * k;
    lt[xx][yy]++;
  }
  for (int i = 0; i <= 2 * k; i++)
    for (int j = 1; j <= 2 * k; j++) lt[i][j] += lt[i][j - 1];
  for (int i = 1; i <= 2 * k; i++)
    for (int j = 0; j <= 2 * k; j++) lt[i][j] += lt[i - 1][j];
  int ans = 0;
  for (int a = 0; a <= k; a++) {
    for (int b = 0; b <= k; b++) {
      int c1 = lt[b][a];
      int c2 = lt[b][2 * k] - lt[b][k + a];
      int c3 = lt[2 * k][a] - lt[k + b][a];
      int c4 = lt[2 * k][2 * k] - lt[k + b][2 * k] - lt[2 * k][k + a] +
               lt[k + b][a + k];
      int c5 = lt[k + b][a + k] - lt[b][a + k] - lt[b + k][a] + lt[b][a];
      ans = max(ans, c1 + c2 + c3 + c4 + c5);
      ans = max(ans, n - (c1 + c2 + c3 + c4 + c5));
    }
  }
  printf("%d\n", ans);
  return 0;
}
