//#pragma comment(linker, "/STACK:1024000000,1024000000")
//#include<algorithm>
//#include<iostream>
//#include<cstring>
//#include<fstream>
//#include<bitset>
//#include<cstdio>
//#include<string>
//#include<vector>
//#include<cmath>
//#include<queue>
//#include<stack>
//#include<map>
//#include<set>
//#define INF 0X3F3F3F3F
//#define N 200005
//#define M 200005
//#define LL long long
//#define ULL unsigned long long
//#define FF(i, a, b) for(int i = a; i <= b; ++i)
//#define RR(i, a, b) for(int i = a; i >= b; --i)
//#define FJ(i, a, b) for(int i = a; i < b; ++i)
//#define SC(x) scanf("%d", &x)
//#define SCC(x, y) scanf("%d%d", &x, &y)
//#define SCCC(x, y, z) scanf("%d%d%d", &x, &y, &z)
//#define SS(x) scanf("%s", x)
//#define PR(x) printf("%d\n", x)
//#define CL(a, x) memset(a, x, sizeof(a))
//#define _P fd[rt]
//#define _L fd[rt << 1]
//#define _R fd[rt << 1 | 1]
//#define MID int mid = ((l + r) >> 1)
//#define lson rt<<1 ,l, mid
//#define rson rt<<1 | 1, mid + 1, r
//#define VI vector<int>
//#define db double
//#define PII pair<int, int>
//#define PLL pair<unsigned long long, unsigned long long>
//#define MP make_pair
//#define PB push_back
//#define IN freopen("in.txt", "r", stdin)
//#define OUT freopen("out.txt", "w", stdout)
//using namespace std;
//inline void II(int &n){char ch = getchar(); n = 0; bool t = 0;
//for(; ch < '0'; ch = getchar()) if(ch == '-') t = 1;
//for(; ch >= '0'; n = n * 10 + ch - '0', ch = getchar()); if(t) n =- n;}
//inline void OO(int a){if(a < 0) {putchar('-'); a = -a;}
//if(a >= 10) OO(a / 10); putchar(a % 10 + '0');}
//const double EPS = 1e-8;
//bool sgn(double x){return x > EPS - x < EPS;}
//int n, a[N][3], k;
//int c[2222][2222][2];
//char str[10];
//int main(){
//    IN;
//    SCC(n, k);
//    FF(i, 1, n){
//        SCC(a[i][1], a[i][2]);
//        scanf("%s", str);
//        if(str[0] == 'B') a[i][0] = 1;
//        if((a[i][1] / k & 1) == (a[i][2] / k & 1))
//        ++c[a[i][1] % k][a[i][2] % k][a[i][0]];
//        else
//        ++c[a[i][1] % k + k][a[i][2] % k][a[i][0]];
//    }
//    int ans = 0;
//    for(int ver = 0; ver < k; ++ver)
//    for(int hor = 0; hor < k; ++hor){
//        int s = 0;
//        for(int i = 0; i < k * 2; ++i)
//        for(int j = 0; j < k; ++j){
//            if(j < hor && i >= ver && i < ver + k) s += c[i][j][0];
//            else if(j >= hor && (i < ver || i >= ver + k)) s += c[i][j][0];
//            else s += c[i][j][1];
//        }
//        if(s > ans)
//            ans = s;
//        s = 0;
//        for(int i = 0; i < k * 2; ++i)
//        for(int j = 0; j < k; ++j){
//            if(j < hor && i >= ver && i < ver + k) s += c[i][j][1];
//            else if(j >= hor && (i < ver || i >= ver + k)) s += c[i][j][1];
//            else s += c[i][j][0];
//        }
//        if(s > ans)
//        ans = s;
//    }
//    cout << ans << endl;
//    return 0;
//}
//
//
//
//
//



#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<fstream>
#include<bitset>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define INF 0X3F3F3F3F
#define N 200005
#define M 200005
#define LL long long
#define ULL unsigned long long
#define FF(i, a, b) for(int i = a; i <= b; ++i)
#define RR(i, a, b) for(int i = a; i >= b; --i)
#define FJ(i, a, b) for(int i = a; i < b; ++i)
#define SC(x) scanf("%d", &x)
#define SCC(x, y) scanf("%d%d", &x, &y)
#define SCCC(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define SS(x) scanf("%s", x)
#define PR(x) printf("%d\n", x)
#define CL(a, x) memset(a, x, sizeof(a))
#define _P fd[rt]
#define _L fd[rt << 1]
#define _R fd[rt << 1 | 1]
#define MID int mid = ((l + r) >> 1)
#define lson rt<<1 ,l, mid
#define rson rt<<1 | 1, mid + 1, r
#define VI vector<int>
#define db double
#define PII pair<int, int>
#define PLL pair<unsigned long long, unsigned long long>
#define MP make_pair
#define PB push_back
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "w", stdout)
using namespace std;
inline void II(int &n){char ch = getchar(); n = 0; bool t = 0;
for(; ch < '0'; ch = getchar()) if(ch == '-') t = 1;
for(; ch >= '0'; n = n * 10 + ch - '0', ch = getchar()); if(t) n =- n;}
inline void OO(int a){if(a < 0) {putchar('-'); a = -a;}
if(a >= 10) OO(a / 10); putchar(a % 10 + '0');}
const double EPS = 1e-8;
bool sgn(double x){return x > EPS - x < EPS;}
int n, a[N][3], k;
int c[2222][2222][2], s[2222][2222][2];
char str[10];
int get(int x1, int y1, int x2, int y2, int col){
    if(x1 > x2 || y1 > y2) return 0;
    return s[x1][y1][col] - s[x2 + 1][y1][col] - s[x1][y2 + 1][col] + s[x2 + 1][y2 + 1][col];
}
int main(){
//    IN;
    SCC(n, k);
    FF(i, 1, n){
        SCC(a[i][1], a[i][2]);
        scanf("%s", str);
        if(str[0] == 'B') a[i][0] = 1;
        if((a[i][1] / k & 1) == (a[i][2] / k & 1)) ++c[a[i][1] % k][a[i][2] % k][a[i][0]];
        else ++c[a[i][1] % k + k][a[i][2] % k][a[i][0]];
    }
    int tot0 = 0, tot1 = 0;
    for(int i = k * 2 - 1; i >= 0; --i)
        for(int j = k - 1; j >= 0; --j){
            s[i][j][0] = s[i + 1][j][0] + s[i][j + 1][0] - s[i + 1][j + 1][0] + c[i][j][0];
            s[i][j][1] = s[i + 1][j][1] + s[i][j + 1][1] - s[i + 1][j + 1][1] + c[i][j][1];
            tot0 += c[i][j][0];
            tot1 += c[i][j][1];
        }
    int ans = 0, x1, x2, y1, y2;
    for(int ver = 0; ver < k; ++ver)
    for(int hor = 0; hor < k; ++hor){
        int s0, s1;
        s0 = s1 = 0;
        x1 = 0, y1 = hor, x2 = ver - 1, y2 = k - 1;
        s0 += get(x1,y1,x2,y2,0);
        s1 += get(x1,y1,x2,y2,1);
        x1 = ver, y1 = 0, x2 = ver + k - 1, y2 = hor - 1;
        s0 += get(x1,y1,x2,y2,0);
        s1 += get(x1,y1,x2,y2,1);
        x1 = ver + k, y1 = hor, x2 = 2 * k - 1, y2 = k - 1;
        s0 += get(x1,y1,x2,y2,0);
        s1 += get(x1,y1,x2,y2,1);
        if(s0 + tot1 - s1 > ans) ans = s0 + tot1 - s1;
        if(s1 + tot0 - s0 > ans) ans = s1 + tot0 - s0;
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 73856