n,k=list(map(int,input().split()))

grid=[[0]*k for _ in range(k)]

for i in range(n):

    x,y,c=[x for x in input().split()]

    x,y=int(x),int(y)

    p=(x//k+y//k)%2

    if (p==0 and c=="B")|(p==1 and c=="W"): grid[x%k][y%k]+=1

    else: grid[x%k][y%k]-=1



for i in range(k):

    for j in range(1,k):

        grid[i][j]+=grid[i][j-1]



for i in range(1,k):

    for j in range(k):

        grid[i][j]+=grid[i-1][j]





ans=0

for i in range(k):

    for j in range(k):

        b=grid[i][j]*4+grid[k-1][k-1]-(grid[i][k-1]+grid[k-1][j])*2

        ans=max(ans,(n+b)//2,(n-b)//2)

print(ans)
n,k=list(map(int,input().split()))

grid=[[0]*k for _ in range(k)]

for i in range(n):

    x,y,c=[x for x in input().split()]

    x,y=int(x),int(y)

    p=(x//k+y//k)%2

    if (p==0 and c=="B")|(p==1 and c=="W"): grid[x%k][y%k]+=1

    else: grid[x%k][y%k]-=1



for i in range(k):

    for j in range(1,k):

        grid[i][j]+=grid[i][j-1]



for i in range(1,k):

    for j in range(k):

        grid[i][j]+=grid[i-1][j]





ans=0

for i in range(k):

    for j in range(k):

        b=grid[i][j]*4+grid[k-1][k-1]-(grid[i][k-1]+grid[k-1][j])*2

        ans=max(ans,n+b,n-b)

print((ans//2))
n, k = list(map(int, input().split()))
grid = [[0] * k for _ in range(k)]
for i in range(n):
    x, y, c = [x for x in input().split()]
    x, y = int(x), int(y)
    p = (x // k + y // k) % 2
    if (p == 0 and c == "B") | (p == 1 and c == "W"):
        grid[x % k][y % k] += 1
    else:
        grid[x % k][y % k] -= 1
for i in range(k):
    for j in range(1, k):
        grid[i][j] += grid[i][j - 1]
for i in range(1, k):
    for j in range(k):
        grid[i][j] += grid[i - 1][j]
ans = 0
for i in range(k):
    for j in range(k):
        b = grid[i][j] * 4 + grid[k - 1][k - 1] - (grid[i][k - 1] + grid[k - 1][j]) * 2
        ans = max(ans, (n + b) // 2, (n - b) // 2)
print(ans)

n, k = list(map(int, input().split()))
grid = [[0] * k for _ in range(k)]
for i in range(n):
    x, y, c = [x for x in input().split()]
    x, y = int(x), int(y)
    p = (x // k + y // k) % 2
    if (p == 0 and c == "B") | (p == 1 and c == "W"):
        grid[x % k][y % k] += 1
    else:
        grid[x % k][y % k] -= 1
for i in range(k):
    for j in range(1, k):
        grid[i][j] += grid[i][j - 1]
for i in range(1, k):
    for j in range(k):
        grid[i][j] += grid[i - 1][j]
ans = 0
for i in range(k):
    for j in range(k):
        b = grid[i][j] * 4 + grid[k - 1][k - 1] - (grid[i][k - 1] + grid[k - 1][j]) * 2
        ans = max(ans, n + b, n - b)
print((ans // 2))

-        ans = max(ans, (n + b) // 2, (n - b) // 2) -print(ans) +        ans = max(ans, n + b, n - b) +print((ans // 2))