R,r=range,raw_input

N,K=list(map(int,r().split()))

L=K+1

P,Q=R(K),R(L)

D=[L*[0]for i in Q]

a=b=0

for i in R(N):

 x,y,c=r().split()

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

 p=(x/K+y/K+ord(c))%2

 D[y%K+1][x%K+1]+=1-2*p

 b+=p

for k in R(2):

 for i in P:

  for j in Q:D[(i+1,j)[k]][(j,i+1)[k]]+=D[(i,j)[k]][(j,i)[k]]

for i in Q:

 for j in Q:

  S=D[i][K]+D[K][j]-D[i][j]*2

  a=max(a,S,D[K][K]-S)

print(a+b)
R,r=range,raw_input;N,K=list(map(int,r().split()));L=K+1;P,Q=R(K),R(L);D=[L*[0]for i in Q];a=b=0

for i in R(N):x,y,c=r().split();x,y=int(x),int(y);p=(x/K+y/K+ord(c))%2;D[y%K+1][x%K+1]+=1-2*p;b+=p

for i in P:

 for j in Q:D[i+1][j]+=D[i][j]

for i in P:

 for j in Q:D[j][i+1]+=D[j][i]

for i in Q:

 for j in Q:

  S=D[i][K]+D[K][j]-D[i][j]*2;a=max(a,S,D[K][K]-S)

print(a+b)
R, r = range, raw_input
N, K = list(map(int, r().split()))
L = K + 1
P, Q = R(K), R(L)
D = [L * [0] for i in Q]
a = b = 0
for i in R(N):
    x, y, c = r().split()
    x, y = int(x), int(y)
    p = (x / K + y / K + ord(c)) % 2
    D[y % K + 1][x % K + 1] += 1 - 2 * p
    b += p
for k in R(2):
    for i in P:
        for j in Q:
            D[(i + 1, j)[k]][(j, i + 1)[k]] += D[(i, j)[k]][(j, i)[k]]
for i in Q:
    for j in Q:
        S = D[i][K] + D[K][j] - D[i][j] * 2
        a = max(a, S, D[K][K] - S)
print(a + b)

R, r = range, raw_input
N, K = list(map(int, r().split()))
L = K + 1
P, Q = R(K), R(L)
D = [L * [0] for i in Q]
a = b = 0
for i in R(N):
    x, y, c = r().split()
    x, y = int(x), int(y)
    p = (x / K + y / K + ord(c)) % 2
    D[y % K + 1][x % K + 1] += 1 - 2 * p
    b += p
for i in P:
    for j in Q:
        D[i + 1][j] += D[i][j]
for i in P:
    for j in Q:
        D[j][i + 1] += D[j][i]
for i in Q:
    for j in Q:
        S = D[i][K] + D[K][j] - D[i][j] * 2
        a = max(a, S, D[K][K] - S)
print(a + b)

-for k in R(2): -    for i in P: -        for j in Q: -            D[(i + 1, j)[k]][(j, i + 1)[k]] += D[(i, j)[k]][(j, i)[k]] +for i in P: +    for j in Q: +        D[i + 1][j] += D[i][j] +for i in P: +    for j in Q: +        D[j][i + 1] += D[j][i]