/**
 *    author  : 𝒌𝒚𝒐𝒎𝒖𝒌𝒚𝒐𝒎𝒖𝒑𝒖𝒓𝒊𝒏
 *    created : 2020-02-05 18:05:32
**/

#include <iostream>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <complex>
#include <numeric>
#include <bitset>
#include <cassert>
#include <cctype>
#include <iomanip>
#include <random>
#include <utility>
#include <chrono>

using namespace std;

using int64 = long long;

template<class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) { os << '{'; size_t n = vec.size(); for (size_t i = 0; i < n; ++i) { os << vec[i]; if (i != n - 1) os << ','; } os << '}'; return os; }
template<class T, class U> std::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p) { return os << '{' << p.first << " " << p.second << '}'; }
template<class T, class U> std::ostream& operator<<(std::ostream& os, const std::map<T, U>& mp) { os << '{'; for (auto it = mp.begin(); it != mp.end(); ++it) { os << '{' << it->first << ':' << it->second << '}'; if (it != --mp.end()) os << ','; } os << '}'; return os; }
template<class T> std::ostream& operator<<(std::ostream& os, const std::set<T>& st) { os << '{'; for (auto it = st.begin(); it != st.end(); ++it) { os << *it; if (it != --st.end()) os << ','; } os << '}'; return os; }
template<class T> std::istream& operator>>(std::istream& is, std::vector<T>& vec) { size_t n = vec.size(); for (size_t i = 0; i < n; ++i) is >> vec[i]; return is; }

#define all(_) begin(_), end(_)
#define rall(_) rbegin(_), rend(_)

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#else
#define debug(...) 1728
#endif

void debug_out() { std::cerr << '\n'; }

template <class Head, class... Tail>
void debug_out(Head&& head, Tail&&... tail) {
  std::cerr << head;
  if (sizeof...(Tail) != 0) std::cerr << ", ";
  debug_out(std::forward<Tail>(tail)...);
}

template <class T>
class CumulativeSum2D {
 public:
  CumulativeSum2D(const std::vector<vector<T>>& vec)
      : vec_(vec), height_(vec.size()), width_(vec[0].size()) {
    Build();
  }

  // return sum of [sx, gx] * [sy, gy]
  T Get(size_t sx, size_t sy, size_t gx, size_t gy) {
    assert(sx <= gx && sy <= gy);
    assert(0 <= sx && sx < height_ && 0 <= gx && gx < height_ && 0 <= sy &&
          sy < width_ && 0 <= gy && gy < width_);
    return sum_[gx + 1][gy + 1] - sum_[gx + 1][sy] - sum_[sx][gy + 1] +
          sum_[sx][sy];
  }

 private:
  size_t height_;
  size_t width_;
  std::vector<vector<T>> vec_;
  std::vector<vector<T>> sum_;

  void Build() {
    sum_.assign(height_ + 1, vector<T>(width_ + 1, static_cast<T>(0)));
    for (size_t i = 0; i < height_; ++i) {
      for (size_t j = 0; j < width_; ++j) {
        sum_[i + 1][j + 1] = sum_[i + 1][j] + vec_[i][j];
      }
    }
    for (size_t i = 0; i < width_; ++i) {
      for (size_t j = 0; j < height_; ++j) {
        sum_[j + 1][i + 1] += sum_[j][i + 1];
      }
    }
  }
};


int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, k; cin >> n >> k;
  vector<vector<int>> black(2 * k, vector<int>(2 * k, 0));
  vector<vector<int>> white(2 * k, vector<int>(2 * k, 0));

  for (int i = 0; i < n; ++i) {
    int x, y; cin >> x >> y;
    char c; cin >> c;
    x %= (2 * k); y %= (2 * k);
    if (c == 'B') {
      ++black[x][y];
    } else {
      ++white[x][y];
    }
  }

  CumulativeSum2D<int> csb(black);
  CumulativeSum2D<int> csw(white);

  int ans = 0;

  for (int i = 0; i < k; ++i) {
    for (int j = 0; j < k; ++j) {
      int tb = 0, tw = 0;
      int t1 = 0, t2 = 0;

      t1 += csw.Get(0, 0, i, j);
      if (i != k - 1 && j != k - 1) t1 += csw.Get(i + 1, j + 1, k - 1, k - 1);
      t2 = csb.Get(0, 0, k - 1, k - 1) - csb.Get(0, 0, i, j);
      if (i != k - 1 && j != k - 1) t2 -= csb.Get(i + 1, j + 1, k - 1, k - 1);

      tw += t1, tb += t2;
      t1 = 0, t2 = 0;

      t1 += csw.Get(0 + k, 0 + k, i + k, j + k);
      if (i != k - 1 && j != k - 1) t1 += csw.Get(i + 1 + k, j + 1 + k, k - 1 + k, k - 1 + k);
      t2 = csb.Get(0 + k, 0 + k, k - 1 + k, k - 1 + k) - csb.Get(0 + k, 0 + k, i + k, j + k);
      if (i != k - 1 && j != k - 1) t2 -= csb.Get(i + 1 + k, j + 1 + k, k - 1 + k, k - 1 + k);

      tw += t1, tb += t2;
      t1 = 0, t2 = 0;

      t2 += csb.Get(0 + k, 0, i + k, j);
      if (i != k - 1 && j != k - 1) t2 += csb.Get(i + 1 + k, j + 1, k - 1 + k, k - 1);
      t1 = csw.Get(0 + k, 0, k - 1 + k, k - 1) - csw.Get(0 + k, 0, i + k, j);
      if (i != k - 1 && j != k - 1) t1 -= csw.Get(i + 1 + k, j + 1, k - 1 + k, k - 1);

      tw += t1, tb += t2;
      t1 = 0, t2 = 0;

      t2 += csb.Get(0, 0 + k, i, j + k);
      if (i != k - 1 && j != k - 1) t2 += csb.Get(i + 1, j + 1 + k, k - 1, k - 1 + k);
      t1 = csw.Get(0, 0 + k, k - 1, k - 1 + k) - csw.Get(0, 0 + k, i, j + k);
      if (i != k - 1 && j != k - 1) t1 -= csw.Get(i + 1, j + 1 + k, k - 1, k - 1 + k);      

      tw += t1, tb += t2;
      ans = max(ans, tw + tb);
    }
  }

  for (int i = 0; i < k; ++i) {
    for (int j = 0; j < k; ++j) {
      int tb = 0, tw = 0;
      int t1 = 0, t2 = 0;

      t1 += csb.Get(0, 0, i, j);
      if (i != k - 1 && j != k - 1) t1 += csb.Get(i + 1, j + 1, k - 1, k - 1);
      t2 = csw.Get(0, 0, k - 1, k - 1) - csw.Get(0, 0, i, j);
      if (i != k - 1 && j != k - 1) t2 -= csw.Get(i + 1, j + 1, k - 1, k - 1);

      tw += t1, tb += t2;
      t1 = 0, t2 = 0;

      t1 += csb.Get(0 + k, 0 + k, i + k, j + k);
      if (i != k - 1 && j != k - 1) t1 += csb.Get(i + 1 + k, j + 1 + k, k - 1 + k, k - 1 + k);
      t2 = csw.Get(0 + k, 0 + k, k - 1 + k, k - 1 + k) - csw.Get(0 + k, 0 + k, i + k, j + k);
      if (i != k - 1 && j != k - 1) t2 -= csw.Get(i + 1 + k, j + 1 + k, k - 1 + k, k - 1 + k);

      tw += t1, tb += t2;
      t1 = 0, t2 = 0;

      t2 += csw.Get(0 + k, 0, i + k, j);
      if (i != k - 1 && j != k - 1) t2 += csw.Get(i + 1 + k, j + 1, k - 1 + k, k - 1);
      t1 = csb.Get(0 + k, 0, k - 1 + k, k - 1) - csb.Get(0 + k, 0, i + k, j);
      if (i != k - 1 && j != k - 1) t1 -= csb.Get(i + 1 + k, j + 1, k - 1 + k, k - 1);

      tw += t1, tb += t2;
      t1 = 0, t2 = 0;

      t2 += csw.Get(0, 0 + k, i, j + k);
      if (i != k - 1 && j != k - 1) t2 += csw.Get(i + 1, j + 1 + k, k - 1, k - 1 + k);
      t1 = csb.Get(0, 0 + k, k - 1, k - 1 + k) - csb.Get(0, 0 + k, i, j + k);
      if (i != k - 1 && j != k - 1) t1 -= csb.Get(i + 1, j + 1 + k, k - 1, k - 1 + k);      

      tw += t1, tb += t2;
      ans = max(ans, tw + tb);
    }
  }

  cout << ans << endl;

  return 0;
}

Measured memory_bytes: 94464