def main():
    N, K = map(int, input().split())
    checklist = [[0]*(2*K) for i in range(2*K)]
    for i in range(N):
        x, y, c = input().split(" ")
        x = int(x)
        y = int(y)
        if c == "B":
            checklist[x % (2*K)][y % (2*K)] += 1
        else:
            checklist[x % (2*K)][(y+K) % (2*K)] += 1
    checksum = [[0]*(2*K) for i in range(2*K)]
    i = 0
    for i in range(2*K):
        if i == 0:
            checksum[0][0] = checklist[0][0]
        else:
            for j in range(i+1):
                if j < i:
                    if j > 0:
                        checksum[j][i] = checksum[j][i-1] + checksum[j-1][i] - checksum[j-1][i-1] + checklist[j][i]
                    else:
                        checksum[j][i] = checksum[j][i-1] + checklist[j][i]
                else:
                    for k in range(i+1):
                        if k > 0:
                            checksum[i][k] = checksum[i-1][k] + checksum[i][k-1] - checksum[i-1][k-1] + checklist[i][k]
                        else:
                            checksum[i][k] = checksum[i-1][k] + checklist[i][k]
    ans = 0
    for i in range(2*K):
        for j in range(2*K-i):
            if i <= K and j <= K:
                sq1 = checksum[i+K-1][j+K-1]
                if i > 0 and j > 0:
                    sq1 = checksum[i+K-1][j+K-1] - checksum[i-1][j+K-1] - checksum[i+K-1][j-1] + checksum[i-1][j-1]
                elif i > 0:
                    sq1 = checksum[i+K-1][j+K-1] - checksum[i-1][j+K-1]
                elif j > 0:
                    sq1 = checksum[i+K-1][j+K-1] - checksum[i+K-1][j-1]
                sq2 = 0
                if i > 0 and j > 0:
                    sq2 = checksum[i-1][j-1]
                sq3 = 0
                if i < 2*K-1 and j > 0:
                    sq3 = checksum[2*K-1][j-1] - checksum[i+K-1][j-1]
                sq4 = 0
                if i > 0 and j < 2*K-1:
                    sq4 = checksum[i-1][2*K-1] - checksum[i-1][j+K-1]
                sq5 = 0
                if i < 2*K-1 and j < 2*K-1:
                    sq5 = checksum[2*K-1][2*K-1] - checksum[2*K-1][j+K-1] - checksum[i+K-1][2*K-1] + checksum[i+K-1][j+K-1]
                ans = max(ans, sq1+sq2+sq3+sq4+sq5)
            elif i <= K:
                sq1 = checksum[i+K-1][2*K-1] - checksum[i+K-1][j-1]
                if i > 0:
                    sq1 = checksum[i+K-1][2*K-1] - checksum[i-1][2*K-1] - checksum[i+K-1][j-1] + checksum[i-1][j-1]
                sq2 = 0
                if i > 0:
                    sq2 = checksum[i-1][j-1] - checksum[i-1][j-K-1]
                sq3 = checksum[2*K-1][j-1] - checksum[i+K-1][j-1] - checksum[2*K-1][j-K-1] + checksum[i+K-1][j-K-1]
                sq4 = checksum[i+K-1][j-K-1]
                if i > 0:
                    sq4 = checksum[i+K-1][j-K-1] - checksum[i-1][j-K-1]
                ans = max(ans, sq1+sq2+sq3+sq4)
            elif j <= K:
                sq1 = checksum[2*K-1][j+K-1] - checksum[i-1][j+K-1]
                if j > 0:
                    sq1 = checksum[2*K-1][j+K-1] - checksum[2*K-1][j-1] - checksum[i-1][j+K-1] + checksum[i-1][j-1]
                sq2 = 0
                if j > 0:
                    sq2 = checksum[i-1][j-1] - checksum[i-K-1][j-1]
                sq3 = checksum[i-1][2*K-1] - checksum[i-1][j+K-1] - checksum[i-K-1][2*K-1] + checksum[i-K-1][j+K-1]
                sq4 = checksum[i-K-1][j+K-1]
                if j > 0:
                    sq4 = checksum[i-K-1][j+K-1] - checksum[i-K-1][j-1]
                ans = max(ans, sq1+sq2+sq3+sq4)
            if ans == N:
                break
        if ans == N:
            break
    print(ans)


if __name__ == "__main__":
    main()