#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys,collections,itertools,re,math,fractions,decimal,random,array,bisect,heapq

# decimal.getcontext().prec = 50
# sys.setrecursionlimit(10000)
# MOD = 10**9 + 7

def cumulative_sum_2d(a):
    h, w = len(a), len(a[0])
    for i in range(1, h):
        for j in range(w):
            a[i][j] += a[i-1][j]
    for i in range(h):
        for j in range(1, w):
            a[i][j] += a[i][j-1]

def solve(f):
    n, k = f.read_int_list()
    b = [[0] * k for _ in xrange(k)]
    w = [[0] * k for _ in xrange(k)]
    for i in xrange(n):
        x, y, c = f.read_str().split()
        x, y = int(x)%(2*k), int(y)%(2*k)
        sgn = 1 if c == 'B' else -1
        if x >= k:
            x -= k
            sgn *= -1
        if y >= k:
            y -= k
            sgn *= -1
        if sgn == 1:
            b[x][y] += 1
        else:
            w[x][y] += 1

    cumulative_sum_2d(b)
    cumulative_sum_2d(w)

    ans = 0
    for xi in xrange(k):
        for yi in xrange(k):
            res = 0
            if xi == yi == 0:
                res += b[k-1][k-1]
            elif xi == 0:
                res += b[k-1][k-1] - b[k-1][yi-1]
                res += w[k-1][yi-1]
            elif yi == 0:
                res += b[k-1][k-1] - b[xi-1][k-1]
                res += w[xi-1][k-1]
            else:
                res += b[k-1][k-1] - b[xi-1][k-1] - b[k-1][yi-1] + b[xi-1][yi-1]
                res += b[xi-1][yi-1]
                res += w[xi-1][k-1] - w[xi-1][yi-1]
                res += w[k-1][yi-1] - w[xi-1][yi-1]
            ans = max(res, ans)

    for xi in xrange(k):
        for yi in xrange(k):
            res = 0
            if xi == yi == 0:
                res += w[k-1][k-1]
            elif xi == 0:
                res += w[k-1][k-1] - w[k-1][yi-1]
                res += b[k-1][yi-1]
            elif yi == 0:
                res += w[k-1][k-1] - w[xi-1][k-1]
                res += b[xi-1][k-1]
            else:
                res += w[k-1][k-1] - w[xi-1][k-1] - w[k-1][yi-1] + w[xi-1][yi-1]
                res += w[xi-1][yi-1]
                res += b[xi-1][k-1] - b[xi-1][yi-1]
                res += b[k-1][yi-1] - b[xi-1][yi-1]
            ans = max(res, ans)

    return ans


class Reader(object):
    def __init__(self, filename=None):
        self.file = open(filename) if filename is not None else None
        self.case = 1

    def __readline(self):
        return self.file.next().strip() if self.file else raw_input()

    def next_case(self):
        self.file.next()
        self.case += 1

    def read_int(self): return int(self.__readline())
    def read_float(self): return float(self.__readline())
    def read_long(self): return long(self.__readline())
    def read_decimal(self): return decimal.Decimal(self.__readline())
    def read_str(self): return self.__readline()

    def read_int_list(self): return map(int, self.__readline().split())
    def read_float_list(self): return map(float, self.__readline().split())
    def read_long_list(self): return map(long, self.__readline().split())
    def read_decimal_list(self): return map(decimal.Decimal, self.__readline().split())
    def read_str_list(self): return self.__readline().split()

if __name__ == '__main__':
    filename = sys.argv[1] if len(sys.argv) > 1 else None
    f = Reader(filename)
    if f.file:
        while True:
            print "Case #%d\n"%f.case, solve(f)
            try:
                f.next_case()
            except StopIteration:
                break
    else:
        print solve(f)


Measured memory_bytes: 50148