#include <bits/stdc++.h>
using namespace std;
int N;
int k;
int ssum[2002][2002];
int mmap[2001][2001];
inline int co(int x1, int y1, int x2, int y2) {
  return ssum[x2 + 1][y2 + 1] - ssum[x1][y2 + 1] - ssum[x2 + 1][y1] +
         ssum[x1][y1];
}
int main() {
  cin >> N >> k;
  for (int i = 0; i < N; i++) {
    int a, b;
    char c;
    cin >> a >> b >> c;
    if (c == 'W') {
      a += k;
    }
    a = a % (2 * k);
    b = b % (2 * k);
    mmap[a][b]++;
  }
  for (int i = 1; i < 2 * k + 2; i++) {
    for (int j = 1; j < 2 * k + 2; j++) {
      ssum[i][j] = mmap[i - 1][j - 1] + ssum[i - 1][j] + ssum[i][j - 1] -
                   ssum[i - 1][j - 1];
    }
  }
  int ma = 0;
  int mi = N;
  for (int i = 0; i < k; i++) {
    for (int j = 0; j < k; j++) {
      int t = 0;
      int x1 = i;
      int y1 = j;
      int x2 = min((i + k - 1), 2 * k);
      int y2 = min((j + k - 1), 2 * k);
      t += co(x1, y1, x2, y2);
      if (i - 1 >= 0 && j - 1 >= 0) {
        x1 = max(0, i - k);
        y1 = max(0, j - k);
        x2 = i - 1;
        y2 = j - 1;
        t += co(x1, y1, x2, y2);
      }
      if (i - 1 >= 0) {
        x1 = max(0, i - k);
        y1 = min(2 * k, j + k);
        x2 = i - 1;
        y2 = min(2 * k, j + 2 * k - 1);
        t += co(x1, y1, x2, y2);
      }
      if (j - 1 >= 0) {
        x1 = min(2 * k, i + k);
        y1 = max(0, j - k);
        x2 = min(2 * k, i + 2 * k - 1);
        y2 = j - 1;
        t += co(x1, y1, x2, y2);
      }
      x1 = min(2 * k, i + k);
      y1 = min(2 * k, j + k);
      x2 = min(2 * k, i + 2 * k - 1);
      y2 = min(2 * k, j + 2 * k - 1);
      t += co(x1, y1, x2, y2);
      ma = max(ma, t);
      mi = min(mi, t);
    }
  }
  ma = max(ma, N - mi);
  cout << ma << endl;
}
