#!/usr/bin/env python



from collections import deque

import itertools as it

import sys

import math



sys.setrecursionlimit(1000000)



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

DP = [[0] * (K + 1) for i in range(K + 1)]

bias = 0



for i in range(N):

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

    x, y, c = int(x), int(y), (c == 'W')

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

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

    bias += p



for i in range(K):

    for j in range(K + 1):

        DP[i + 1][j] += DP[i][j]



for i in range(K + 1):

    for j in range(K):

        DP[i][j + 1] += DP[i][j]



ans = 0



for i in range(K + 1):

    for j in range(K + 1):

        S1 = DP[i][j] * 2 - DP[i][K] - DP[K][j] + DP[K][K]

        S2 = DP[i][K] + DP[K][j] - DP[i][j] * 2

        ans = max(ans, S1, S2)



print(ans + bias)
#!/usr/bin/env python



from collections import deque

import itertools as it

import sys

import math



sys.setrecursionlimit(1000000)



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

DP = [[0] * (K + 1) for i in range(K + 1)]

bias = 0



for i in range(N):

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

    x, y, c = int(x), int(y), (c == 'W')

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

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

    bias += p



for i in range(K):

    for j in range(K + 1):

        DP[i + 1][j] += DP[i][j]



for i in range(K + 1):

    for j in range(K):

        DP[i][j + 1] += DP[i][j]



ans = 0



for i in range(K + 1):

    for j in range(K + 1):

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

        ans = max(ans, S, DP[K][K] - S)



print(ans + bias)
#!/usr/bin/env python
from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)
N, K = list(map(int, input().split()))
DP = [[0] * (K + 1) for i in range(K + 1)]
bias = 0
for i in range(N):
    x, y, c = input().split()
    x, y, c = int(x), int(y), (c == "W")
    p = (x / K + y / K + c) % 2
    DP[y % K + 1][x % K + 1] += 1 - 2 * p
    bias += p
for i in range(K):
    for j in range(K + 1):
        DP[i + 1][j] += DP[i][j]
for i in range(K + 1):
    for j in range(K):
        DP[i][j + 1] += DP[i][j]
ans = 0
for i in range(K + 1):
    for j in range(K + 1):
        S1 = DP[i][j] * 2 - DP[i][K] - DP[K][j] + DP[K][K]
        S2 = DP[i][K] + DP[K][j] - DP[i][j] * 2
        ans = max(ans, S1, S2)
print(ans + bias)

#!/usr/bin/env python
from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)
N, K = list(map(int, input().split()))
DP = [[0] * (K + 1) for i in range(K + 1)]
bias = 0
for i in range(N):
    x, y, c = input().split()
    x, y, c = int(x), int(y), (c == "W")
    p = (x / K + y / K + c) % 2
    DP[y % K + 1][x % K + 1] += 1 - 2 * p
    bias += p
for i in range(K):
    for j in range(K + 1):
        DP[i + 1][j] += DP[i][j]
for i in range(K + 1):
    for j in range(K):
        DP[i][j + 1] += DP[i][j]
ans = 0
for i in range(K + 1):
    for j in range(K + 1):
        S = DP[i][K] + DP[K][j] - DP[i][j] * 2
        ans = max(ans, S, DP[K][K] - S)
print(ans + bias)

-        S1 = DP[i][j] * 2 - DP[i][K] - DP[K][j] + DP[K][K] -        S2 = DP[i][K] + DP[K][j] - DP[i][j] * 2 -        ans = max(ans, S1, S2) +        S = DP[i][K] + DP[K][j] - DP[i][j] * 2 +        ans = max(ans, S, DP[K][K] - S)