### BUGGY CODE ###

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


import numpy as np

n,k = list(map(int, input().split()))
# v0 = np.zeros((4*k, 4*k), dtype=int)
# v1 = np.zeros((4*k, 4*k), dtype=int)
v0 = [[0]*(4*k) for _ in range(4*k)]
v1 = [[0]*(4*k) for _ in range(4*k)]

for i in range(n):
    x,y,c = input().split()
    x = int(x)
    y = int(y)
    x %= 2*k
    y %= 2*k
    if c=="W":
        v = v0
    else:
        v = v1
    v[x][y] += 1
    v[x][y+2*k] += 1
    v[x+2*k][y] += 1
    v[x+2*k][y+2*k] += 1
v0 = np.array(v0)
v1 = np.array(v1)

def cumsum2(v):
    # v: h*wの2次元リスト
    v = np.cumsum(v, axis=0)
    v = np.cumsum(v, axis=1)
    c = np.zeros((v.shape[0]+1, v.shape[1]+1), dtype=int)
    c[1:, 1:] = v
    return v
c0 = cumsum2(v0)
c1 = cumsum2(v1)
ans = -1
i0 = []
i1 = []
for i in range(2*k):
    for j in range(2*k):
        i0.append([[i+k,j+k], [i+k,j], [i,j+k], [i,j], [i+2*k, j+2*k], [i+2*k,j+k], [i+k, j+2*k], [i+k, j+k]])
        i1.append([[i+2*k, j+k], [i+2*k,j], [i+k,j+k], [i+k,j], [i+k, j+2*k], [i+k,j+k], [i,j+2*k], [i,j+k]])
#         val0 = c0[i+k][j+k]-c0[i+k][j]-c0[i][j+k]+c0[i][j] + c0[i+2*k][j+2*k]-c0[i+2*k][j+k]-c0[i+k][j+2*k]+c0[i+k][j+k]
#         val1 = c1[i+2*k][j+k]-c1[i+2*k][j]-c1[i+k][j+k]+c1[i+k][j] + c1[i+k][j+2*k]-c1[i+k][j+k]-c1[i][j+2*k]+c1[i][j+k]
#         ans = max(ans, val0+val1)
i0 = np.array(i0)
i1 = np.array(i1)
val = c0[i0[:,[0,3,4,7],0], i0[:,[0,3,4,7],1]].sum(axis=1) - c0[i0[:,[1,2,5,6],0], i0[:,[1,2,5,6],1]].sum(axis=1) + c1[i1[:,[0,3,4,7],0], i1[:,[0,3,4,7],1]].sum(axis=1) - c1[i1[:,[1,2,5,6],0], i1[:,[1,2,5,6],1]].sum(axis=1)
print(val.max())

### FIXED CODE ###

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")



n,k = list(map(int, input().split()))
v = [[0]*(2*k) for _ in range(2*k)]

for i in range(n):
    x,y,c = input().split()
    x = int(x)
    y = int(y)
    if c=="B":
        x += k
    x %= 2*k
    y %= 2*k
    v[x][y] += 1
#     v[x][y+2*k] += 1
#     v[x+2*k][y] += 1
#     v[x+2*k][y+2*k] += 1
def cumsum2(v):
    # v: h*wの2次元リスト
    h,w = len(v), len(v[0])
    v.insert(0, [0]*w)
    for l in v:
        l.insert(0, 0)
    for i in range(1,h+1):
        for j in range(1,w+1):
            v[i][j] = v[i-1][j] + v[i][j-1] - v[i-1][j-1] + v[i][j]
    return v
cumsum2(v)

def sub(c, i, j):
    i %= 2*k
    j %= 2*k
    l = []
    if i<=k and j<=k:
        l = [[i,i+k,j,j+k]]
    elif i>k and j<=k:
        l = [[i,2*k,j,j+k], [0,i-k,j,j+k]]
    elif i<=k and j>k:
        l = [[i,i+k,j,2*k], [i,i+k,0,j-k]]
    else:
        l = [[i,2*k,j,2*k], [0,i-k,j,2*k], [i,2*k,0,j-k], [0,i-k,0,j-k]]
    return sum((c[z][w] - c[z][y] - c[x][w] + c[x][y]) for x,z,y,w in l)
ans = -1
for i in range(2*k):
    for j in range(2*k-i):
        val0 = sub(v, i,j) + sub(v, i+k, j+k)
#         val0 = v[i+k][j+k]-v[i+k][j]-v[i][j+k]+v[i][j] + v[i+2*k][j+2*k]-v[i+2*k][j+k]-v[i+k][j+2*k]+v[i+k][j+k]
#         val1 = v[i+2*k][j+k]-v[i+2*k][j]-v[i+k][j+k]+v[i+k][j] + v[i+k][j+2*k]-v[i+k][j+k]-v[i][j+2*k]+v[i][j+k]
        ans = max(ans, val0)
print(ans)