def blackSum(x,y,x2,y2):
    return bs[y2][x2] - bs[y2][x-1] - bs[y-1][x2] + bs[y-1][x-1]

n,k = [int(i) for i in input().split()]
black = [[0 for i in range(2*k+2)] for j in range(2*k+2)]
for i in range(n):
    xi,yi,ci = list(input().split())
    x = int(xi)%(2*k)
    if ci == "W":
        y = (int(yi) + k)%(2*k)
    else:
        y = int(yi)%(2*k)
    black[y+2][x+2] += 1
bs = [[0 for i in range(2*k+2)] for j in range(2*k+2)]
for i in range(1,2*k+2):
    for j in range(1,2*k+2):
        bs[i][j] = black[i][j] + bs[i-1][j] + bs[i][j-1] - bs[i-1][j-1]
max = 0
#左上の黒の位置を指定する。
for i in range(2,k+2):
    for j in range(2,k+2):
        sum = blackSum(j,i,j+k-1,i+k-1) + \
              blackSum(1,1,j-1,i-1) + \
              blackSum(1,i+k,j-1,2*k+1) + \
              blackSum(j+k,1,2*k+1,i-1) + \
              blackSum(j+k,i+k,2*k+1,2*k+1)
        if sum>max:
            max=sum
print(max)
        
        
        
    

