#include <algorithm> //min, max, sort, unique, fill
#include <iostream>
#include <numeric>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility> //pair, swap,
#include <vector>
#include <cstdio>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using VI = vector<ll>;
using VB = vector<bool>;
using VP = vector<P>;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) for (ll i = 0; i < (n); i++)

static const ll kIntMax = numeric_limits<ll>::infinity();
static const double kFloatMax = numeric_limits<double>::infinity();

int main()
{

  int n, k;
  cin >> n >> k;
  int x, y;
  char c;

  vector<vector<int>> table_w(k, vector<int>(k, 0));
  vector<vector<int>> table_b(k, vector<int>(k, 0));
  bool odd;
  for (int i = 0; i < n; i++)
  {
    cin >> x >> y >> c;
    odd = ((x / k) + (y / k)) % 2 == 1;
    if ((c == 'W') ^ odd)
      table_w[y % k][x % k]++;
    else
      table_b[y % k][x % k]++;
  }

  vector<vector<int>> ruiseki_w(k, vector<int>(k, 0));
  vector<vector<int>> ruiseki_b(k, vector<int>(k, 0));

  for (int i = 0; i < k; i++)
  {
    for (int j = 0; j < k; j++)
    {
      ruiseki_w[i][j] += table_w[i][j];
      if (i > 0)
        ruiseki_w[i][j] += ruiseki_w[i - 1][j];
      if (j > 0)
        ruiseki_w[i][j] += ruiseki_w[i][j - 1];
      if (i > 0 && j > 0)
        ruiseki_w[i][j] -= ruiseki_w[i - 1][j - 1];

      ruiseki_b[i][j] += table_b[i][j];
      if (i > 0)
        ruiseki_b[i][j] += ruiseki_b[i - 1][j];
      if (j > 0)
        ruiseki_b[i][j] += ruiseki_b[i][j - 1];
      if (i > 0 && j > 0)
        ruiseki_b[i][j] -= ruiseki_b[i - 1][j - 1];

      // printf("%2d%c", table[i][j], ",\n"[(int)(j == k - 1)]);
    }
  }
  // cout << endl;

  // cout << "w" << endl;
  // for (int i = 0; i < k; i++)
  // {
  //   for (int j = 0; j < k; j++)
  //   {
  //     printf("%2d%c", table_w[i][j], ",\n"[(int)(j == k - 1)]);
  //   }
  // }
  // cout << endl;
  // cout << "b" << endl;
  // for (int i = 0; i < k; i++)
  // {
  //   for (int j = 0; j < k; j++)
  //   {
  //     printf("%2d%c", table_b[i][j], ",\n"[(int)(j == k - 1)]);
  //   }
  // }
  // cout << endl;

  int tmp = 0;

  for (int i = 0; i < k; i++)
  {
    for (int j = 0; j < k; j++)
    {
      int A11 = ruiseki_w[i][j];
      // int A12 = ruiseki_w[i][k - 1] - A11;
      int A12 = ruiseki_w[i][k - 1] - ruiseki_w[i][j];
      // int A21 = ruiseki_w[k-1][j] - A11;
      int A21 = ruiseki_w[k - 1][j] - ruiseki_w[i][j];

      int A22 = ruiseki_w[k - 1][k - 1] - A12 - A21 - A11;
      // int A22 = ruiseki_w[k - 1][k - 1] - ruiseki_w[i][k - 1] - ruiseki_w[k - 1][j] + ruiseki_w[i][j];

      int B11 = ruiseki_b[i][j];
      // int B12 = ruiseki_b[i][k - 1] - B11;
      int B12 = ruiseki_b[i][k - 1] - ruiseki_b[i][j];
      // int B21 = ruiseki_b[k-1][j] - B11;
      int B21 = ruiseki_b[k - 1][j] - ruiseki_b[i][j];

      int B22 = ruiseki_b[k - 1][k - 1] - B12 - B21 - B11;
      // int B22 = ruiseki_b[k - 1][k - 1] - ruiseki_b[i][k - 1] - ruiseki_b[k - 1][j] + ruiseki_b[i][j];

      // cout << "(i,j)="
      //      << "(" << i << "," << j << "):" << max(A11 + A22 + B12 + B21, A12 + A21 + B11 + B12) << endl;
      // cout << "A11: " << A11 << ",A12: " << A12 << ",A21: " << A21 << ",A22: " << A22 << endl;
      // cout << "B11: " << B11 << ",B12: " << B12 << ",B21: " << B21 << ",B22: " << A22 << endl;

      // tmp = max(tmp, abs(A11 + A22 - A12 - A21));
      tmp = max(tmp, max(A11 + A22 + B12 + B21, A12 + A21 + B11 + B22));
    }
  }
  cout << tmp << endl;

  return 0;
}


Measured memory_bytes: 16000