#include <bits/stdc++.h>

#define LL long long
#define VI vector<int>
#define VB vector<bool>
#define VL vector<long long>
#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EPS 1e-14

const std::string YES = "YES";
const std::string Yes = "Yes";
const std::string NO = "NO";
const std::string No = "No";

using namespace std;

template <class T> int find_index(T x, const std::vector<T> &v) {
    auto it = std::find(v.begin(), v.end(), x);
    if (it != v.end()) { return (int)std::distance(v.begin(), it); }
    return -1;
}
template<class T> T ceil(T a, T b) { return (a + b - 1) / b; }

template <class T> void print(T x) {
    std::cout << x << std::endl;
}
// 4近傍（右, 下, 左, 上）
const vector<int> dy = { 0, -1, 0, 1 };
const vector<int> dx = { 1, 0, -1, 0 };

class CumulativeSum {

public:
    vector<vector<int>> memo;

    // boardの累積和を作成
    CumulativeSum(const vector<vector<int>> &board) {
        int height = board.size();
        int width = board[0].size();
        this->memo = vector<vector<int>>(height + 1, vector<int>(width + 1, 0));

        for (int y = 0; y < height; ++y) {
            for (int x = 0; x < width; ++x) {
                memo[y + 1][x + 1] = board[y][x] + memo[y + 1][x];
            }
            for (int x = 0; x < width; ++x) {
                memo[y + 1][x + 1] += memo[y][x + 1];
            }
        }
    }

    /*
    (y1, x1)から(y2, x2)の合計を返す．(y2, x2)は含まない
    座標はmemoを作成したboard準拠
    (ex, sum(0, 0, 2, 2)なら(0, 0), (0, 1), (1, 0), (1, 1)の合計を返す)
    */
    int sum(int y1, int y2, int x1, int x2) {
        assert(0 <= y1 && y1 <= y2 && y2 < memo.size());
        assert(0 <= x1 && x1 <= x2 && x2 < memo.size());
        return this->memo[y2][x2] - this->memo[y2][x1] - this->memo[y1][x2] + this->memo[y1][x1];
    }

};

void solve(long long N, long long K, vector<long long> &X, vector<long long> &Y, vector<char> &C){
    vector<vector<int>> black(2 * K, vector<int>(2 * K, 0));
    FOR(i, 0, N) {
        LL x = X[i];
        LL y = Y[i];
        char c = C[i];

        if (c == 'W') {
            y += K;
        }
        black[y % (2 * K)][x % (2 * K)] += 1;

    }

    CumulativeSum cl(black);

    LL ans = 0;
    FOR(y1, 0, K) {
        FOR(x1, 0, K) {
            int y2 = min(y1 + K, 2 * K);
            int x2 = min(x1 + K, 2 * K);
            LL num = 0;
            num += cl.sum(0, y1, x1, x2);        // 下
            num += cl.sum(y2, 2 * K, x1, x2);    // 上
            num += cl.sum(y1, y2, 0, x1);        // 左
            num += cl.sum(y1, y2, x2, 2 * K);    // 左
            ans = max(ans, num);
            ans = max(ans, N - num);
        }
    }


    print(ans);

}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    long long N;
    long long K;
    cin >> N >> K;
    vector<long long> y(N);
    vector<long long> x(N);
    vector<char> c(N);

    for(int i = 0 ; i < N ; i++){
        cin >> x[i];
        cin >> y[i];
        cin >> c[i];
    }
    solve(N, K, x, y, c);
    return 0;
}



Measured memory_bytes: 33408