#include <cstdio>
#include <vector>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <cassert>
#include <cmath>
using namespace std;

struct RectangleSum {
        vector<vector<int>> sum;
        int GetSum(int left, int right, int top, int bottom) { //[left, right], [top, bottom]
                int res = sum[bottom][right];
                if (left > 0) res -= sum[bottom][left - 1];
                if (top > 0) res -= sum[top - 1][right];
                if (left > 0 && top > 0) res += sum[top - 1][left - 1];
                return res;
        }
        void init(const vector<vector<int>> &s, int h, int w) {
                sum.resize(h);
                for (int i = 0; i < h; i ++) sum[i].resize(w, 0);
                for (int y = 0; y < h; y ++) {
                        for (int x = 0; x < w; x ++) {
                                sum[y][x] = s[y][x];
                                if (y > 0) sum[y][x] += sum[y - 1][x];
                                if (x > 0) sum[y][x] += sum[y][x - 1];
                                if (y > 0 && x > 0) sum[y][x] -= sum[y - 1][x - 1];
                        }
                }
        }
};

int main() {
        int n, k;
        scanf("%d %d", &n, &k);
        int h = k, w = 2 * k;
        vector<vector<int>> white(h, vector<int> (w, 0));
        vector<vector<int>> black(h, vector<int> (w, 0));
        for (int i = 0; i < n; i ++) {
                int x, y;
                scanf("%d %d", &x, &y);
                char c;
                cin >> c;
                x %= 2 * k, y %= 2 * k;
                if ((x >= k && y >= k) || (x < k && y < k)) {
                        x %= k, y %= k;
                } else {
                        y %= k;
                        if (x < k) x += k;
                }
                if (c == 'W') {
                        white[y][x] ++;
                } else {
                        black[y][x] ++;
                }
        }
        RectangleSum white_sum;
        white_sum.init(white, h, w);
        RectangleSum black_sum;
        black_sum.init(black, h, w);
        int ans = 0;
        for (int i = 0; i < k; i ++) {
                for (int j = 0; j < k; j ++) {
                        int res1 = 0, res2 = 0;

                        if (j > 0 && i > 0) res1 += black_sum.GetSum(0, j - 1, 0, i - 1);
                        if (j > 0) res1 += white_sum.GetSum(0, j - 1, i, k - 1);
                        res1 += black_sum.GetSum(j, j + k - 1, i, k - 1);
                        if (i > 0) res1 += white_sum.GetSum(j, j + k - 1, 0, i - 1);
                        if (i > 0) res1 += black_sum.GetSum(j + k, 2 * k - 1, 0, i - 1);
                        res1 += white_sum.GetSum(j + k, 2 * k - 1, i, k - 1);
                        ans = max(ans, res1);

                        if (j > 0 && i > 0) res2 += white_sum.GetSum(0, j - 1, 0, i - 1);
                        if (j > 0) res2 += black_sum.GetSum(0, j - 1, i, k - 1);
                        res2 += white_sum.GetSum(j, j + k - 1, i, k - 1);
                        if (i > 0) res2 += black_sum.GetSum(j, j + k - 1, 0, i - 1);
                        if (i > 0) res2 += white_sum.GetSum(j + k, 2 * k - 1, 0, i - 1);
                        res2 += black_sum.GetSum(j + k, 2 * k - 1, i, k - 1);
                        ans = max(ans, res2);
                }
        }
        printf("%d\n", ans);
        return 0;
}


Measured memory_bytes: 31616