#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<vector<int>> w(k, vector<int>(k, 0)), b(k, vector<int>(k, 0));
  for (int i = (0); i < (int)(n); i++) {
    int x, y;
    char c;
    scanf("%d %d %c", &x, &y, &c);
    if (bool((x / k % 2) ^ (y / k % 2)) ^ (c == 'W')) {
      w[x % k][y % k]++;
    } else {
      b[x % k][y % k]++;
    }
  }
  int ans = 0, al = 0;
  vector<int> X(k, 0), sX(k, 0);
  for (int i = (0); i < (int)(k); i++) {
    for (int j = (0); j < (int)(k); j++) {
      sX[i] += w[i][j] + b[i][j];
      X[i] += w[i][j];
    }
  }
  for (int i = (0); i < (int)(k); i++) {
    int now = 0;
    for (int j = (0); j < (int)(k); j++) {
      now += X[j];
    }
    for (int j = (0); j < (int)(k); j++) {
      ans = max(ans, max(now, n - now));
      now += sX[j] - X[j] * 2;
    }
    for (int j = (0); j < (int)(k); j++) {
      X[j] += b[j][i] - w[j][i];
    }
  }
  cout << ans << endl;
}
