#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

typedef long long ll, lint;
typedef long double ldouble;

#define sz(v) ((int) (v).size())
#define FOR(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define For(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define all(v) (v).begin(), (v).end()

#define mp make_pair
#define pb push_back
#define fst first
#define snd second

template<class T>
string to_string(T arr, int len = 1e9) {
    stringstream ss;
    int i = 0;
    for (auto item : arr) {
        ss << item << ' ';
        if (++i == len)
            break;
    }
    return ss.str();
}

#ifdef LOCAL
    #define dbg(expr) cout << "[line " << __LINE__ << "] " << (#expr) << ": " << (expr) << endl;
#else
    #define dbg(expr) (void) 0;
#endif


const double pi = acos(-1);
lint mod;

struct zint {
	lint value;

	zint(): value() {}

	zint(lint val) {
		value = (val % mod + mod) % mod;
	}

	zint operator +(const zint & other) const {
		return zint(value + other.value);
	}

	zint operator -(const zint & other) const {
		return zint(value - other.value);
	}

	zint operator *(const zint & other) const {
		return zint(value * other.value);
	}

	zint operator +=(const zint & other) {
		this->value = (*this + other).value;
		return *this;
	}

	zint operator -=(const zint & other) {
		this->value = (*this - other).value;
		return *this;
	}

	zint operator *=(const zint & other) {
		this->value = (*this * other).value;
		return *this;
	}

    zint & operator=(zint other) {
        this->value = other.value;
        return *this;
    }

    friend ostream & operator <<(ostream & os, const zint & number) {
        os << number.value;
        return os;
    }

    friend istream & operator >>(istream & is, zint & number) {
        is >> number.value;
        number = zint(number.value);
        return is;
    }
};

struct Point {
    typedef zint T;
    T x, y;

    Point() {}

    Point(T x, T y): x(x), y(y) {}

    Point operator +(Point other) const {
        return Point(x + other.x, y + other.y);
    }

    Point operator -(Point other) const {
        return Point(x - other.x, y - other.y);
    }

    Point operator *(double k) const {
        return Point(x * k, y * k);
    }

    T scl(Point other) const {
        return x * other.x + y * other.y;
    }

    T vect(Point other) const {
        return x * other.y - y * other.x;
    }
    T len2() const {
        return this->scl(*this);
    }

    friend istream & operator >>(istream & input, Point & cur) {
        input >> cur.x >> cur.y;
        return input;
    }

    friend ostream & operator <<(ostream & output, const Point & cur) {
        output << cur.x << ' ' << cur.y;
        return output;
    }
};

int main() {
#ifndef LOCAL
    cin.tie(0);
    ios_base::sync_with_stdio(false);
#endif

    int n, k;
    cin >> n >> k;
    mod = 2 * k;
    vector<Point> points(n);
    vector<int> color_id(n);
    static int values[2][2007][2007];
    For (i, n) {
        char c;
        cin >> points[i] >> c;
        color_id[i] = 0;
        if (c == 'B')
            color_id[i] = 1;
        values[color_id[i]][points[i].y.value][points[i].x.value]++;
    }
    int max_value = 0;
    vector<vector<int>> w(2), b(2);
    For (i, 2) {
        w[i].resize(mod);
        b[i].resize(mod);
    }
    For (i, n) {
        int group = points[i].y.value / k;
        if (color_id[i] == 0)
            w[group][points[i].x.value]++;
        else
            b[group][points[i].x.value]++;
    }

    For (y, mod) {
        int cur = 0;
        For (x, k) {
            cur += b[0][x];
            cur += w[1][x];
            cur += w[0][x + k];
            cur += b[1][x + k];
        }
        max_value = max(max_value, cur);
        for (int x = 1; x < mod; x++) {
            cur -= b[0][x - 1];
            cur += w[0][x - 1];
            cur -= w[1][x - 1];
            cur += b[1][x - 1];

            cur += b[0][zint(x + k - 1).value];
            cur -= w[0][zint(x + k - 1).value];
            cur += w[1][zint(x + k - 1).value];
            cur -= b[1][zint(x + k - 1).value];

            max_value = max(max_value, cur);
        }
        For (x, mod) {
            w[0][x] -= values[0][y][x];
            w[1][x] += values[0][y][x];

            b[0][x] -= values[1][y][x];
            b[1][x] += values[1][y][x];

            w[1][x] -= values[0][(y + k) % mod][x];
            w[0][x] += values[0][(y + k) % mod][x];

            b[1][x] -= values[1][(y + k) % mod][x];
            b[0][x] += values[1][(y + k) % mod][x];
        }
    }
    cout << max_value << endl;
}

Measured memory_bytes: 33664