# -*- coding: utf-8 -*-

import sys

def color(x, y, k):
    r = 0
    if ((x % (2*k)) / k) != 0:
        r = -1
    
    if ((y % (2*k)) / k) != 0:
        r ^= -1

    return 'B' if  r == 0 else 'W'

def nSatisfied(n, k, tl):
    maxH = 0
    for yi in xrange(0, 2*k):
        for xi in xrange(0, 2*k):
            h = 0
            for e in tl:
                x, y, c = e
                if color(x + xi, y + yi, k) == c:
                    h += 1
            if h > maxH:
                maxH = h
            if maxH == n:
                return maxH
    return maxH


n, k = [int(e) for e in raw_input().split()]
tl = []
for i in range(0, n):
    x, y, c = [e for e in raw_input().split()]
    tl.append([int(x), int(y), c])

# print
# print n, k
# print tl

# for i in range(0, 2*k):
#     print color(i, k),

# print

maxH = nSatisfied(n, k, tl)

sys.stdout.write('%d\n' % maxH)
sys.stdout.flush()
