#include<bits/stdc++.h>

#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple

using namespace std;

int matW[2010][2010];
int matB[2010][2010];
int prefW[2010][2010];
int prefB[2010][2010];

int k;

bool moze(int x, int y)
{
    return x > 0 && y > 0 && x <= 2*k && y <= 2*k;
}

bool ispitaj(int x, int y)
{
    return moze(x,y) || moze(x+k-1, y) || moze(x, y + k -1) || moze(x + k - 1, y + k - 1);
}

int cnt(int x, int y, bool belo)
{

    int gX = x, gY = y;
    int dX = x + k - 1, dY = y + k - 1;
    if(gX < 1) gX = 1;
    if(gY < 1) gY = 1;
    if(dX > 2*k) dX = 2*k;
    if(dY > 2*k) dY = 2*k;
    /*if(ispis)
    {
        cout << gX << " " << gY << " " << dX << " " << dY << "\n";
        cout << prefB[dX][dY] << " " << prefB[dX][gY-1] << " " << prefB[gX-1][dY] << " " << prefB[gX-1][gY-1] << " evo\n";
        cout << prefB[dX][dY] - prefB[dX][gY-1] - prefB[gX-1][dY] + prefB[gX-1][gY-1] << "tooo \n";
    }*/
    if(belo)
        return prefW[dX][dY] - prefW[dX][gY-1] - prefW[gX-1][dY] + prefW[gX-1][gY-1];
    else
    {
        return prefB[dX][dY] - prefB[dX][gY-1] - prefB[gX-1][dY] + prefB[gX-1][gY-1];
    }

}

int izracunaj(int x, int y)
{
    int sum = 0;
    for(int i = -2; i <= 2; i++)
        for(int j = -2; j <= 2; j++)
        {
            if(ispitaj(x + i*k, y + j*k))
            {

                bool belo = false;
                if((i + j) % 2 == 0) belo = true;
                sum += cnt(x + i*k, y + j*k, belo);
                /*if(x == 1 && y == 1 && x + i*k == 1 && y + j*k == 2)
                {
                    cout << cnt(x + i*k, y + j*k, belo, true) << "waa\n";
                    cout << x + i*k << " " << y + j*k << " " << sum << "\n";
                }*/
            }
        }
    return sum;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;

    cin >> n >> k;
    for(int i = 0; i < n; i++)
    {
        int a, b;
        char c;
        cin >> a >> b;
        cin >> c;
        a %= 2*k;
        b %= 2*k;
        if(c == 'W') matW[a+1][b+1]++;
        else matB[a+1][b+1]++;
    }
    prefB[1][1] = matB[1][1];
    prefW[1][1] = matW[1][1];
    for(int i = 2; i <= 2*k; i++)
    {
        prefB[i][1] = prefB[i-1][1] + matB[i][1];
        prefW[i][1] = prefW[i-1][1] + matW[i][1];
    }
    for(int j = 2; j <= 2*k; j++)
    {
        prefB[1][j] = prefB[1][j-1] + matB[1][j];
        prefW[1][j] = prefW[1][j-1] + matW[1][j];
    }

    for(int i = 2; i <= 2*k; i++)
        for(int j = 2; j <= 2*k; j++)
        {
            prefB[i][j] = prefB[i][j-1] + prefB[i-1][j] - prefB[i-1][j-1] + matB[i][j];
            prefW[i][j] = prefW[i][j-1] + prefW[i-1][j] - prefW[i-1][j-1] + matW[i][j];
        }

    int res = 0;

    /*for(int i = 0; i <= 2*k; i++)
    {
        for(int j = 0; j <= 2*k; j++)
            cout << prefB[i][j] << " ";
        cout << "\n";
    }
    cout << "\n";
    for(int i = 0; i <= 2*k; i++)
    {
        for(int j = 0; j <= 2*k; j++)
            cout << prefW[i][j] << " ";
        cout << "\n";
    }*/

    for(int i = 1; i <= 2*k; i++)
        for(int j = 1; j <= 2*k; j++)
            res = max(res, izracunaj(i,j));

    cout << res;


    return 0;
}

/*
5 1
0 0 W
0 0 B
0 1 W
0 1 W
0 1 B
*/


Measured memory_bytes: 63360