#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;

constexpr ll infl = 1000000000000000007LL;
constexpr int inf = 1000000007;

int k;

bool inside(int p) {
    return 0 <= p && p < 2 * k;
}

int calc_sum(int y1, int x1, int y2, int x2, const vector<vector<int>> &grid) {
    if (inside(y1) && inside(x1) && inside(y2) && inside(x2)) {
        int ret = grid[y2][x2];
        if (y1 > 0)
            ret -= grid[y1 - 1][x2];
        if (x1 > 0)
            ret -= grid[y2][x1 - 1];
        if (y1 > 0 && x1 > 0)
            ret += grid[y1 - 1][x1 - 1];
        return ret;
    } else
        return 0;
}

int request_num(const vector<vector<int>> &grid, int y, int x) {
    int ret = calc_sum(y, x, min(y + k - 1, 2 * k - 1), min(x + k - 1, 2 * k - 1), grid);
    return ret + calc_sum(0, 0, y - 1, x - 1, grid) + calc_sum(y + k, x + k, 2 * k - 1, 2 * k - 1, grid) + calc_sum(0, x + k, y - 1, 2 * k - 1, grid) + calc_sum(y + k, 0, 2 * k - 1, x - 1, grid);
}

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

    // 愚直解
    // int ans_exact = 0;
    // for (int i = 0; i < 2 * k; ++i) {
    //     for (int j = 0; j < 2 * k; ++j) {
    //         // i, jを黒になる左上とする
    //         int req_black = 0;
    //         int req_white = 0;
    //         for (int y = 0; y < 2 * k; ++y) {
    //             for (int x = 0; x < 2 * k; ++x) {
    //                 int ny = (y - i + 2 * k) % (2 * k);
    //                 int nx = (x - j + 2 * k) % (2 * k);
    //                 if ((ny < k && nx < k) || (ny >= k && nx >= k)) {
    //                     req_black += black[y][x];
    //                     req_white += white[y][x];
    //                 }
    //             }
    //         }
    //         ans_exact = max(ans_exact, req_black - req_white + all_req_white);
    //     }
    // }

    for (int i = 0; i < 2 * k; ++i) {
        for (int j = 1; j < 2 * k; ++j) {
            black[i][j] += black[i][j - 1];
            white[i][j] += white[i][j - 1];
        }
    }
    for (int j = 0; j < 2 * k; ++j) {
        for (int i = 1; i < 2 * k; ++i) {
            black[i][j] += black[i - 1][j];
            white[i][j] += white[i - 1][j];
        }
    }

    int ans = 0;
    for (int i = 0; i <= k; ++i) {
        for (int j = 0; j <= k; ++j) {
            int req_black = request_num(black, i, j);
            int req_white = request_num(white, i, j);
            ans = max({ans, req_black - req_white + all_req_white, -req_black + req_white + all_req_black});
        }
    }

    cout << ans << endl;
    //cout << ans_exact << endl;
    return 0;
}

Measured memory_bytes: 31616