#include <cstdio>

int N = 1, K = 1;
bool is_in_black(int x, int y, int orig_x, int orig_y);

// #define __DEBUG__

int main()
{
    int max_possible = 0, possible = 0;

    scanf("%d%d", &N,&K);
    int x[N] = {0}, y[N] = {0};
    char c[N] = {};
    for (int i = 0; i < N; ++i) {
        // Input each time and stop
        scanf("%d%d %c", &x[i], &y[i], &c[i]);
        // Ambiguity : 2K * 2K
        if ( x[i] < 0 || x[i] >= 2 * K) {
            x[i] %= (2 * K);
        }
        if ( y[i] < 0 || y[i] >= 2 * K) {
            y[i] %= (2 * K);
        }
    }
#ifdef __DEBUG__
    for (int i = 0; i < N; ++i)
        printf("> %d,%d,%c\n", x[i], y[i], c[i]);
    printf("-----\n");
#endif
    for (int orig_x = 0; orig_x < 2 * K; ++orig_x) // Search X-direction
    {
        for (int orig_y = 0; orig_y < 2 * K; ++orig_y) // Search Y-direction
        { 
            for (int k = 0; k < N; ++k) // Check requests
            {
                if (c[k] == 'B')
                {
                    if (is_in_black(x[k], y[k], orig_x, orig_y))
                    {
#ifdef __DEBUG__
                        printf("OK %d,%d,%c\n", x[k], y[k], c[k]);
#endif
                        ++possible;
                    }
                    else
                    {
#ifdef __DEBUG__
                        printf("NG %d,%d,%c\n", x[k], y[k], c[k]);
#endif
                    }
                                }
                else
                {
                    if (!is_in_black(x[k], y[k], orig_x, orig_y))
                    {
#ifdef __DEBUG__
                        printf("OK %d,%d,%c\n", x[k], y[k], c[k]);
#endif
                        ++possible;
                    }
                    else
                    {
#ifdef __DEBUG__
                        printf("NG %d,%d,%c\n", x[k], y[k], c[k]);
#endif
                    }
                }
            }
            if (possible > max_possible)
            {
                max_possible = possible;
            }
#ifdef __DEBUG__
            printf("%d, %d | %d , %d\n", orig_x, orig_y, possible, max_possible);
#endif
            possible = 0; // Clear
        }
    }

    // Output
    printf("%d\n", max_possible);
    return 0;
}

bool is_in_black(int x, int y, int orig_x, int orig_y)
{
    if (((x >= 0 && x < orig_x) && // 1-1
         (y >= 0 && y < orig_y)) ||
        ((x >= 0 && x < orig_x) && // 1-3
         (y >= orig_y + K && y < orig_y + 2 * K)) ||
        ((x >= orig_x && x < orig_x + K) && // 2-2
         (y >= orig_y && y < orig_y + K)) ||
        ((x >= orig_x + K && x < orig_x + 2 * K) && // 3-1
         (y >= 0 && y < orig_y)) ||
        ((x >= orig_x + K && x < orig_x + 2 * K) && // 3-3
         (y >= orig_y + K && y < orig_y + 2 * K)))
    {
        return true;
    }
    return false;
}