#include <bits/stdc++.h>
using namespace std;

void solve() {
    int n, k;
    cin >> n >> k;
    k *= 2;
    vector<vector<pair<int, int>>> a(k, vector<pair<int, int>>(k, {0, 0}));
    for (int i = 0; i < n; ++i) {
        int x, y;
        char c;
        cin >> x >> y >> c;
        x %= (k);
        y %= (k);
        if (x >= k && y >= k) {
            x -= k;
            y -= k;
        } else {
            if (x >= k) x -= k;
            else if (y >= k) y -= k;
        }
        a[y][x].first += (c == 'W');
        a[y][x].second += (c == 'B');
    }
    //cout << "ok1" << endl;
    vector<vector<int>> b1(k, vector<int>(k, 0)), b2(k, vector<int>(k, 0));
    for (int i = 0; i < k; ++i) {
        for (int j = 0; j < k; ++j) {
            b1[i][j] = a[i][j].first;
            b2[i][j] = a[i][j].second;
            //b1[i][j] = i * k + j;
            //b2[i][j] = i * k + j;
        }
    }
    //cout << "ok2" << endl;
    vector<vector<int>> pred1(k + 1, vector<int>(k + 1, 0)), pred2(k + 1, vector<int>(k + 1, 0));
    for (int i = 1; i < k + 1; ++i) {
        for (int j = 1; j < k + 1; ++j) {
            pred1[i][j] = pred1[i - 1][j] + pred1[i][j - 1] - pred1[i - 1][j - 1] + b1[i - 1][j - 1];
        }
    }
    for (int i = 1; i < k + 1; ++i) {
        for (int j = 1; j < k + 1; ++j) {
            pred2[i][j] = pred2[i - 1][j] + pred2[i][j - 1] - pred2[i - 1][j - 1] + b2[i - 1][j - 1];
        }
    }
    //cout << "ok3" << endl;
    /*for (auto it : b1) {
        for (auto x : it) {
            cout << x << " ";
        }
        cout << "\n";
    }
    cout << "\n";
    for (auto it : b2) {
        for (auto x : it) {
            cout << x << " ";
        }
        cout << "\n";
    }
    cout << "\n";
    for (auto it : pred1) {
        for (auto x : it) {
            cout << x << " ";
        }
        cout << "\n";
    }
    cout << "\n";
    for (auto it : pred2) {
        for (auto x : it) {
            cout << x << " ";
        }
        cout << "\n";
    }
    cout << "\n";*/
    int ans = 0;
    for (int i = 0; i < k / 2; ++i) {
        for (int j = 0; j < k / 2; ++j) {
            int ik = min(i + k / 2 + 1, k), jk = min(j + k / 2 + 1, k);
            //cout << ik << " " << jk << "\n";
            //cout << pred1[i + 1][j + 1] << " " << pred1[i + 1][k] - pred1[i + 1][jk] << " " << pred1[k][j + 1] - pred1[ik][j + 1] << " " << pred1[k][k] - pred1[k][jk] - pred1[ik][k] + pred1[ik][jk] << " " << pred1[ik][jk] - pred1[ik][j + 1] - pred1[i + 1][jk] + pred1[i + 1][j + 1] << "\n";
            //cout << pred2[i + 1][j + 1] << " " << pred2[i + 1][k] - pred2[i + 1][jk] << " " << pred2[k][j + 1] - pred2[ik][j + 1] << " " << pred2[k][k] - pred2[k][jk] - pred2[ik][k] + pred2[ik][jk] << " " << pred2[ik][jk] - pred2[ik][j + 1] - pred2[i + 1][jk] + pred2[i + 1][j + 1] << "\n";
            int t1 = (pred1[i + 1][j + 1]) + (pred1[i + 1][k] - pred1[i + 1][jk]) + (pred1[k][j + 1] - pred1[ik][j + 1]) + (pred1[k][k] - pred1[k][jk] - pred1[ik][k] + pred1[ik][jk]) + (pred1[ik][jk] - pred1[ik][j + 1] - pred1[i + 1][jk] + pred1[i + 1][j + 1]);
            int t2 = (pred2[i + 1][j + 1]) + (pred2[i + 1][k] - pred2[i + 1][jk]) + (pred2[k][j + 1] - pred2[ik][j + 1]) + (pred2[k][k] - pred2[k][jk] - pred2[ik][k] + pred2[ik][jk]) + (pred2[ik][jk] - pred2[ik][j + 1] - pred2[i + 1][jk] + pred2[i + 1][j + 1]);
            //cout << pred1[ik][j + 1] - pred1[i + 1][j + 1] << " " << pred1[i + 1][jk] - pred1[i + 1][j + 1] << " " << pred1[ik][k] - pred1[ik][jk] - pred1[i + 1][k] + pred1[i + 1][jk] << " " << pred1[k][jk] - pred1[k][j + 1] - pred1[ik][jk] + pred1[ik][j + 1] << "\n";
            //cout << pred2[ik][j + 1] - pred2[i + 1][j + 1] << " " << pred2[i + 1][jk] - pred2[i + 1][j + 1] << " " << pred2[ik][k] - pred2[ik][jk] - pred2[i + 1][k] + pred2[i + 1][jk] << " " << pred2[k][jk] - pred2[k][j + 1] - pred2[ik][jk] + pred2[ik][j + 1] << "\n";
            int t3 = (pred1[ik][j + 1] - pred1[i + 1][j + 1]) + (pred1[i + 1][jk] - pred1[i + 1][j + 1]) + (pred1[ik][k] - pred1[ik][jk] - pred1[i + 1][k] + pred1[i + 1][jk]) + (pred1[k][jk] - pred1[k][j + 1] - pred1[ik][jk] + pred1[ik][j + 1]);
            int t4 = (pred2[ik][j + 1] - pred2[i + 1][j + 1]) + (pred2[i + 1][jk] - pred2[i + 1][j + 1]) + (pred2[ik][k] - pred2[ik][jk] - pred2[i + 1][k] + pred2[i + 1][jk]) + (pred2[k][jk] - pred2[k][j + 1] - pred2[ik][jk] + pred2[ik][j + 1]);
            //cout << i << " " << j << " || " << t1 << " " << t2 << " " << t3 << " " << t4 << "\n";
            ans = max(ans, max(t1 + t4, t2 + t3));

        }
    }
    //cout << "ok4" << endl;
    cout << ans << "\n";
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
    //cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}



Measured memory_bytes: 94464