AtCoDeer is thinking of painting an infinite two-dimensional grid in a checked pattern of side K.
Here, a checked pattern of side K is a pattern where each square is painted black or white so that each connected component of each color is a K × K square.
Below is an example of a checked pattern of side 3:
AtCoDeer has N desires.
The i-th desire is represented by x_i, y_i and c_i.
If c_i is B, it means that he wants to paint the square (x_i,y_i) black; if c_i is W, he wants to paint the square (x_i,y_i) white.
At most how many desires can he satisfy at the same time?

-----Constraints-----
 - 1 ≤ N ≤ 10^5
 - 1 ≤ K ≤ 1000
 - 0 ≤ x_i ≤ 10^9
 - 0 ≤ y_i ≤ 10^9
 - If i ≠ j, then (x_i,y_i) ≠ (x_j,y_j).
 - c_i is B or W.
 - N, K, x_i and y_i are integers.

-----Input-----
Input is given from Standard Input in the following format:
N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N

-----Output-----
Print the maximum number of desires that can be satisfied at the same time.

-----Sample Input-----
4 3
0 1 W
1 2 W
5 3 B
5 4 B

-----Sample Output-----
4

He can satisfy all his desires by painting as shown in the example above.

Difficulty: MEDIUM_HARD

Solutions:
(n, k) = map(int, input().split())
black = 0
imos = [[0 for i in range(k)] for j in range(k)]
for i in range(n):
	(X, Y, C) = input().split()
	x = int(X)
	y = int(Y)
	c = 1 if C == 'W' else 0
	x = x % (2 * k)
	y = y % (2 * k)
	if x > k - 1:
		x -= k
		c += 1
	if y > k - 1:
		y -= k
		c += 1
	c = 1 if c % 2 == 1 else -1
	imos[x][y] += c
	if c == -1:
		black += 1
for i in range(k):
	for j in range(k - 1):
		imos[i][j + 1] += imos[i][j]
for j in range(k):
	for i in range(k - 1):
		imos[i + 1][j] += imos[i][j]
ans = 0
for i in range(k):
	for j in range(k):
		m = imos[k - 1][k - 1] - imos[k - 1][j] - imos[i][k - 1] + 2 * imos[i][j]
		ans = max(ans, max(n - (black + m), black + m))
print(ans)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
from collections import deque
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
mod = 10 ** 9 + 7
DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def query_cumsum2d(s, x1, y1, x2, y2):
	if x2 < x1:
		(x1, x2) = (x2, x1)
	if y2 < y1:
		(y1, y2) = (y2, y1)
	return s[x2][y2] - s[x1][y2] - s[x2][y1] + s[x1][y1]

def main():
	(N, K) = LI()
	G = [[0 for _ in range(2 * K)] for __ in range(2 * K)]
	cumsum2d = [[0 for _ in range(2 * K + 1)] for __ in range(2 * K + 1)]
	xyc = []
	for _ in range(N):
		(x, y, c) = LS()
		x = int(x)
		y = int(y)
		if c == 'W':
			x += K
		x = x % (2 * K)
		y = y % (2 * K)
		G[x][y] += 1
		xyc.append((x, y, c))
	for x in range(2 * K):
		for y in range(2 * K):
			cumsum2d[x + 1][y + 1] = cumsum2d[x + 1][y] + cumsum2d[x][y + 1] - cumsum2d[x][y] + G[x][y]
	ans = 0
	for x in range(K):
		for y in range(K):
			cnt = query_cumsum2d(cumsum2d, 0, 0, x, y) + query_cumsum2d(cumsum2d, x, y, x + K, y + K) + query_cumsum2d(cumsum2d, x + K, 0, 2 * K, y) + query_cumsum2d(cumsum2d, 0, y + K, x, 2 * K) + query_cumsum2d(cumsum2d, 2 * K, 2 * K, x + K, y + K)
			ans = max(ans, cnt, N - cnt)
	print(ans)
main()


import sys
import numpy as np

def main():
	input = sys.stdin.readline

	def gen_pattern(x):
		if x < K - 1:
			return [[0, x + 1], [x + K + 1, 2 * K]]
		else:
			return [[x - K + 1, x + 1]]

	def inputs():
		return [int(x) for x in input().split()]

	def inputstr():
		return list(input().split())
	(N, K) = inputs()
	BB = [[0] * (2 * K + 1) for _ in range(2 * K + 1)]
	for i in range(N):
		(x, y, c) = inputstr()
		if c == 'W':
			y = int(y) + K
		x = int(x) % (2 * K)
		y = int(y) % (2 * K)
		for shift in [0, K]:
			for (b, t) in gen_pattern((y + shift) % (2 * K)):
				for (l, r) in gen_pattern((x + shift) % (2 * K)):
					BB[b][l] += 1
					BB[b][r] -= 1
					BB[t][l] -= 1
					BB[t][r] += 1
	ans = np.max(np.cumsum(np.cumsum(BB, axis=1), axis=0))
	print(int(ans))
main()


import numpy as np

def solve(string):
	(n, k, *xyc) = string.split()
	k = int(k)
	l = 2 * int(k)
	xy = [(int(x) % l, int(y) % l) if c == 'W' else (int(x) % l, (int(y) + k) % l) for (x, y, c) in zip(xyc[::3], xyc[1::3], xyc[2::3])]
	ans = [[0 for j in range(l + 0)] for i in range(l + 0)]
	for (_x, _y) in xy:
		if (_x - k + 0.5) * (_y - k + 0.5) > 0:
			lt = 2
			lte = 1
			ans[0][0] += 1
		else:
			lt = -2
			lte = -1
		_x %= k
		_y %= k
		ans[_x][_y] += lt
		ans[_x + k][_y] -= lt
		ans[_x][_y + k] -= lt
		ans[_x + k][_y + k] += lt
		ans[_x + k][0] += lte
		ans[0][_y + k] += lte
		ans[_x][0] -= lte
		ans[0][_y] -= lte
	return str(np.asarray(ans).cumsum(axis=1).cumsum(axis=0).max())

def __starting_point():
	(n, m) = list(map(int, input().split()))
	print(solve('{} {}\n'.format(n, m) + '\n'.join([input() for _ in range(n)])))
__starting_point()


def main():
	(n, k) = map(int, input().split())
	e = [[0] * (2 * k + 1) for i in range(2 * k + 1)]
	mod = 2 * k
	for i in range(n):
		(x, y, c) = input().split()
		if c == 'W':
			x = int(x) % mod
			y = (int(y) + k) % mod
		else:
			x = int(x) % mod
			y = int(y) % mod
		e[x + 1][y + 1] += 1
	for i in range(mod):
		for j in range(mod):
			e[i + 1][j + 1] += e[i][j + 1] + e[i + 1][j] - e[i][j]

	def count(ax, ay, bx, by):
		return e[ax][ay] + e[bx][by] - e[ax][by] - e[bx][ay]
	ans = 0
	for i in range(k + 1):
		for j in range(k + 1):
			c = count(2 * k, 2 * k, i + k, j + k) + count(2 * k, j, i + k, 0) + count(i, 2 * k, 0, j + k) + count(i, j, 0, 0) + count(i + k, j + k, i, j)
			ans = max(ans, c, n - c)
	print(ans)
main()


import sys
input = sys.stdin.readline
(n, k) = map(int, input().split())
C = [list(input().split()) for i in range(n)]
D = [[0] * (2 * k) for i in range(2 * k)]
E = []
for i in range(n):
	x = int(C[i][0]) % (2 * k)
	y = int(C[i][1]) % (2 * k)
	if C[i][2] == 'W':
		x = (x + k) % (2 * k)
	E.append([x, y])
	x = (x + k) % (2 * k)
	y = (y + k) % (2 * k)
	E.append([x, y])
for i in range(2 * n):
	xx = 0
	yy = 0
	x = E[i][0]
	y = E[i][1]
	D[x][y] += 1
	if x + k > 2 * k:
		xx = 1
	if y + k > 2 * k:
		yy = 1
	if xx == 1 and yy == 1:
		D[0][0] += 1
		D[x - k][0] -= 1
		D[0][y - k] -= 1
		D[x - k][y - k] += 1
		D[x][0] += 1
		D[x][y - k] -= 1
		D[0][y] += 1
		D[x - k][y] -= 1
	elif xx == 1:
		D[0][y] += 1
		D[x - k][y] -= 1
		if y != k:
			D[x][y + k] -= 1
			D[0][y + k] -= 1
			D[x - k][y + k] += 1
	elif yy == 1:
		D[x][0] += 1
		D[x][y - k] -= 1
		if x != k:
			D[x + k][y] -= 1
			D[x + k][0] -= 1
			D[x + k][y - k] += 1
	else:
		if x != k:
			D[x + k][y] -= 1
		if y != k:
			D[x][y + k] -= 1
		if x != k and y != k:
			D[x + k][y + k] += 1
for i in range(1, 2 * k):
	D[0][i] += D[0][i - 1]
	D[i][0] += D[i - 1][0]
for i in range(1, 2 * k):
	for j in range(1, 2 * k):
		D[i][j] += D[i - 1][j] + D[i][j - 1] - D[i - 1][j - 1]
print(max([max(D[i]) for i in range(2 * k)]))


import sys
(n, k) = map(int, sys.stdin.readline().strip().split())
x = []
y = []
mx = []
my = []
for _ in range(n):
	(xx, yy, c) = sys.stdin.readline().strip().split()
	xx = int(xx)
	yy = int(yy)
	if c == 'W':
		xx += k
	xx %= 2 * k
	yy %= 2 * k
	if xx >= k:
		if yy >= k:
			x.append(xx - k)
			y.append(yy - k)
		else:
			mx.append(xx - k)
			my.append(yy)
	elif yy >= k:
		mx.append(xx)
		my.append(yy - k)
	else:
		x.append(xx)
		y.append(yy)
graph = [[0 for _ in range(k)] for __ in range(k)]
lx = len(x)
graph[0][0] += lx
for i in range(lx):
	graph[x[i]][0] -= 1
	graph[0][y[i]] -= 1
	graph[x[i]][y[i]] += 2
lmx = len(mx)
graph[0][0] -= lmx
for i in range(lmx):
	graph[mx[i]][0] += 1
	graph[0][my[i]] += 1
	graph[mx[i]][my[i]] -= 2
for i in graph:
	for j in range(k - 1):
		i[j + 1] += i[j]
for i in range(k):
	for j in range(k - 1):
		graph[j + 1][i] += graph[j][i]
big = max([max(i) for i in graph]) + lmx
small = min([min(i) for i in graph]) - lx
if big + small >= 0:
	print(big)
else:
	print(-small)


import sys
sys.setrecursionlimit(10 ** 6)
readline = sys.stdin.readline
(n, k) = [int(i) for i in readline().split()]
xyc = []
B = [[0] * (k + 1) for i in range(k + 1)]
W = [[0] * (k + 1) for i in range(k + 1)]
for _ in range(n):
	(x, y, c) = [i for i in readline().split()]
	x = int(x)
	y = int(y)
	sgn = 0
	sgn += x // k
	x %= k
	sgn += y // k
	y %= k
	if sgn & 1:
		if c == 'W':
			B[x + 1][y + 1] += 1
		else:
			W[x + 1][y + 1] += 1
	elif c == 'W':
		W[x + 1][y + 1] += 1
	else:
		B[x + 1][y + 1] += 1
for i in range(k):
	for j in range(k):
		B[i + 1][j + 1] += B[i + 1][j] + B[i][j + 1] - B[i][j]
		W[i + 1][j + 1] += W[i + 1][j] + W[i][j + 1] - W[i][j]
ans = 0
for i in range(k):
	for j in range(k):
		resb1 = B[i][j] + (B[-1][-1] - B[-1][j] - B[i][-1] + B[i][j])
		resw1 = W[i][j] + (W[-1][-1] - W[-1][j] - W[i][-1] + W[i][j])
		resb2 = B[i][-1] - B[i][j] + (B[-1][j] - B[i][j])
		resw2 = W[i][-1] - W[i][j] + (W[-1][j] - W[i][j])
		ans = max(ans, resb1 + resw2, resw1 + resb2)
print(ans)


from numpy import *
(N, K) = map(int, input().split())
M = zeros((3 * K, 3 * K), int)
L = 2 * K
O = 0
for n in range(N):
	(x, y, c) = input().split()
	x = int(x) % L
	y = int(y) % L
	t = c == 'W'
	M[x, y] += 1 - 2 * t
	O += t
for n in range(2):
	M[L:] = M[:K]
	M = cumsum(M, axis=0)
	M[:L] = M[K:] - M[:L]
	M = M.T
M = M[:L, :L]
print(O + (M + roll(roll(M, K, axis=0), K, axis=1)).max())


import numpy as np
import sys

def sum2d(i1, j1, i2, j2):
	A[i1][j1] += 1
	A[i1][j2] -= 1
	A[i2][j1] -= 1
	A[i2][j2] += 1
(N, K) = list(map(int, input().split()))
XYC = [list(input().split()) for _ in range(N)]
A = [[0 for _ in range(K * 2 + 1)] for _ in range(K * 2 + 1)]
for (x, y, c) in XYC:
	i = (int(x) if c == 'W' else int(x) + K) % (K * 2)
	j = int(y) % (K * 2)
	i0 = i % K
	j0 = j % K
	if i < K and j < K or (i >= K and j >= K):
		sum2d(0, 0, i0, j0)
		sum2d(i0 + K, 0, K * 2, j0)
		sum2d(i0, j0, i0 + K, j0 + K)
		sum2d(0, j0 + K, i0, K * 2)
		sum2d(i0 + K, j0 + K, K * 2, K * 2)
	else:
		sum2d(i0, 0, i0 + K, j0)
		sum2d(0, j0, i0, j0 + K)
		sum2d(i0 + K, j0, K * 2, j0 + K)
		sum2d(i0, j0 + K, i0 + K, K * 2)
AA = np.array(A)
for i in range(K * 2):
	AA[i + 1, :] += AA[i, :]
for j in range(K * 2):
	AA[:, j + 1] += AA[:, j]
print(AA.max())


ri = lambda : int(input())
rl = lambda : list(map(int, input().split()))
rr = lambda N: [ri() for _ in range(N)]
YN = lambda b: print('YES') if b else print('NO')
INF = 10 ** 18
(N, K) = rl()
W = [[0 for _ in range(K + 3)] for _ in range(K + 3)]
for i in range(N):
	(x, y, c) = input().split()
	x = int(x) % (2 * K)
	y = (int(y) if c == 'W' else int(y) + K) % (2 * K)
	if (x < K) ^ (y < K):
		x %= K
		y %= K
		W[0][y + 1] += 1
		W[x + 1][y + 1] -= 2
		W[0][K + 1] -= 1
		W[x + 1][K + 1] += 1
		W[x + 1][0] += 1
		W[K + 1][0] -= 1
		W[K + 1][y + 1] += 1
	else:
		x %= K
		y %= K
		W[0][0] += 1
		W[x + 1][0] -= 1
		W[0][y + 1] -= 1
		W[x + 1][y + 1] += 2
		W[K + 1][y + 1] -= 1
		W[x + 1][K + 1] -= 1
		W[K + 1][K + 1] += 1
for x in range(0, K + 2):
	for y in range(0, K + 2):
		W[x][y] += W[x][y - 1]
for y in range(0, K + 2):
	for x in range(0, K + 2):
		W[x][y] += W[x - 1][y]
ans = 0
for x in range(K + 1):
	for y in range(K + 1):
		res = W[x][y]
		ans = max(ans, res, N - res)
print(ans)


import sys
sys.setrecursionlimit(10000)
INF = float('inf')
(N, K) = map(int, next(sys.stdin).split())
MOD = K * 2
grid = [[0 for _ in range(MOD)] for _ in range(MOD)]

def draw(sx, sy, ex, ey):
	sx %= MOD
	sy %= MOD
	ex = (ex - 1) % MOD + 1
	ey = (ey - 1) % MOD + 1
	if sx < ex and sy < ey:
		grid[sx][sy] += 1
		if ex < MOD:
			grid[ex][sy] -= 1
		if ey < MOD:
			grid[sx][ey] -= 1
		if ex < MOD and ey < MOD:
			grid[ex][ey] += 1
	elif ex <= sx:
		if ey <= sy:
			draw(0, 0, ex, ey)
			draw(0, sy, ex, MOD)
			draw(sx, 0, MOD, ey)
			draw(sx, sy, MOD, MOD)
		else:
			draw(0, sy, ex, ey)
			draw(sx, sy, MOD, ey)
	else:
		draw(sx, 0, ex, ey)
		draw(sx, sy, ex, MOD)
for _ in range(N):
	(x, y, c) = next(sys.stdin).split()
	x = int(x)
	y = int(y)
	if c == 'W':
		x += K
	draw(x, y, x + K, y + K)
	draw(x + K, y + K, x + K * 2, y + K * 2)
for i in range(MOD - 1):
	for j in range(MOD):
		grid[i + 1][j] += grid[i][j]
for i in range(MOD - 1):
	for j in range(MOD):
		grid[j][i + 1] += grid[j][i]
ans = 0
for i in range(MOD):
	ans = max(ans, *grid[i])
print(ans)


import numpy as np
(n, k) = map(int, input().split())
list_subj = []
for i in range(n):
	(x, y, c) = input().split()
	if c == 'W':
		list_subj.append([int(x) % (2 * k), int(y) % (2 * k)])
	else:
		list_subj.append([(int(x) + k) % (2 * k), int(y) % (2 * k)])
imos_field = [[0] * (2 * k + 1) for _ in range(2 * k + 1)]
for (x, y) in list_subj:
	for place in (0, k):
		adj_y = (y + place) % (2 * k)
		if adj_y >= k - 1:
			imos_y = [(adj_y - k + 1, adj_y + 1)]
		else:
			imos_y = [(0, adj_y + 1), (adj_y + k + 1, 2 * k)]
		adj_x = (x + place) % (2 * k)
		if adj_x >= k - 1:
			imos_x = [(adj_x - k + 1, adj_x + 1)]
		else:
			imos_x = [(0, adj_x + 1), (adj_x + k + 1, 2 * k)]
		for (bottom, top) in imos_y:
			for (left, right) in imos_x:
				imos_field[bottom][left] += 1
				imos_field[top][left] -= 1
				imos_field[bottom][right] -= 1
				imos_field[top][right] += 1
result = np.cumsum(imos_field, axis=1)
result = np.cumsum(result, axis=0)
result = int(np.max(result))
print(result)


def main():
	(n, k) = map(int, input().split())
	e = [[0] * (2 * k) for i in range(2 * k)]
	mod = 2 * k
	for i in range(n):
		(x, y, c) = input().split()
		if c == 'W':
			x = int(x) % mod
			y = (int(y) + k) % mod
		else:
			x = int(x) % mod
			y = int(y) % mod
		e[x][y] += 1
	for i in range(mod):
		for j in range(mod):
			if i != 0 and j != 0:
				e[i][j] -= e[i - 1][j - 1]
			if i != 0:
				e[i][j] += e[i - 1][j]
			if j != 0:
				e[i][j] += e[i][j - 1]
	ans = 0
	for i in range(k + 1):
		for j in range(k + 1):
			count = e[-1][-1] + 2 * e[i + k - 1][j + k - 1] - e[-1][j + k - 1] - e[i + k - 1][-1]
			if i != 0 and j != 0:
				count += 2 * e[i - 1][j - 1]
			if i != 0:
				count += e[i - 1][-1] - 2 * e[i - 1][j + k - 1]
			if j != 0:
				count += e[-1][j - 1] - 2 * e[i + k - 1][j - 1]
			ans = max(ans, count, n - count)
	print(ans)
main()


import numpy as np
(N, K) = map(int, input().split())
mod = 2 * K
field = [[0] * (2 * K + 1) for _ in range(2 * K + 1)]

def gen_pattern(x):
	if x < K - 1:
		return [[0, x + 1], [x + K + 1, 2 * K]]
	else:
		return [[x - K + 1, x + 1]]
for _ in range(N):
	(x, y, c) = input().split()
	(x, y) = (int(x), int(y))
	if c == 'W':
		y += K
	x %= mod
	y %= mod
	for tmp in [0, K]:
		for (l, r) in gen_pattern((x + tmp) % mod):
			for (b, t) in gen_pattern((y + tmp) % mod):
				field[l][b] += 1
				field[l][t] -= 1
				field[r][b] -= 1
				field[r][t] += 1
print(np.max(np.cumsum(np.cumsum(field, axis=0), axis=1)))


from numpy import *
(N, K) = map(int, input().split())
m = zeros((3 * K, 3 * K), dtype=int)
L = 2 * K
o = 0
for _ in [0] * N:
	(x, y, c) = input().split()
	(x, y) = (int(x) % L, int(y) % L)
	t = c == 'W'
	m[x, y] += 2 * t - 1
	o += t
for _ in [0, 0]:
	m[L:] = m[:K]
	m = cumsum(m, axis=0)
	m[:L] -= m[K:]
	m = m.T
m = m[:L, :L]
print(o - int((m + roll(roll(m, K, axis=0), K, axis=1)).min()))


import sys
input = sys.stdin.readline
(n, k) = map(int, input().split())
dp = [[0] * k for i in range(k)]
for i in range(n):
	(x, y, z) = input().split()
	(x, y) = (int(x), int(y))
	if x // k % 2 == y // k % 2:
		if z == 'W':
			(x, y) = (x % k, y % k)
			dp[0][0] += 1
			if y < k - 1:
				dp[0][y + 1] -= 1
			if x < k - 1:
				dp[x + 1][0] -= 1
			if x < k - 1 and y < k - 1:
				dp[x + 1][y + 1] += 2
		else:
			(x, y) = (x % k, y % k)
			if y < k - 1:
				dp[0][y + 1] += 1
			if x < k - 1:
				dp[x + 1][0] += 1
			if x < k - 1 and y < k - 1:
				dp[x + 1][y + 1] -= 2
	elif z == 'W':
		(x, y) = (x % k, y % k)
		if y < k - 1:
			dp[0][y + 1] += 1
		if x < k - 1:
			dp[x + 1][0] += 1
		if x < k - 1 and y < k - 1:
			dp[x + 1][y + 1] -= 2
	else:
		(x, y) = (x % k, y % k)
		dp[0][0] += 1
		if y < k - 1:
			dp[0][y + 1] -= 1
		if x < k - 1:
			dp[x + 1][0] -= 1
		if x < k - 1 and y < k - 1:
			dp[x + 1][y + 1] += 2
for i in range(k):
	for j in range(1, k):
		dp[i][j] += dp[i][j - 1]
Max = 0
Min = n
for j in range(k):
	for i in range(k):
		if i == 0:
			Max = max(Max, dp[i][j])
			Min = min(Min, dp[i][j])
		else:
			dp[i][j] += dp[i - 1][j]
			Max = max(Max, dp[i][j])
			Min = min(Min, dp[i][j])
ans = max(Max, n - Min)
print(ans)


class Acc2D:

	def __init__(self, a):
		(h, w) = (len(a), len(a[0]))
		self.acc2D = self._build(h, w, a)

	def _build(self, h, w, a):
		ret = [[0] * (w + 1) for _ in range(h + 1)]
		for r in range(h):
			for c in range(w):
				ret[r + 1][c + 1] = ret[r][c + 1] + ret[r + 1][c] - ret[r][c] + a[r][c]
		return ret

	def get(self, r1, r2, c1, c2):
		acc2D = self.acc2D
		return acc2D[r2][c2] - acc2D[r1][c2] - acc2D[r2][c1] + acc2D[r1][c1]

def main():
	import sys
	input = sys.stdin.readline
	(n, k) = map(int, input().split())
	g = [[0] * (k * 2) for _ in range(k * 2)]
	for _ in range(n):
		(x, y, c_) = input().split()
		x = int(x) % (k * 2)
		y = int(y) % (k * 2)
		if c_ == 'W':
			y = (y + k) % (k * 2)
		g[y][x] += 1
	acc2D = Acc2D(g)
	ret = 0
	for r in range(k):
		for c in range(k):
			s = acc2D.get(0, r, 0, c) + acc2D.get(0, r, c + k, k * 2) + acc2D.get(r, r + k, c, c + k) + acc2D.get(r + k, k * 2, 0, c) + acc2D.get(r + k, k * 2, c + k, k * 2)
			ret = max(ret, s, n - s)
	print(ret)
main()


(N, K) = map(int, input().split())
ub = K * 2
b = [[0 for j in range(ub + 1)] for i in range(ub + 1)]

def create_bound(p):
	if p - K + 1 >= 0:
		return [(p - K + 1, p + 1)]
	else:
		return [(0, p + 1), ((p - K + 1) % ub, ub)]
for i in range(N):
	(x, y, c) = input().split()
	nx = int(x) % ub
	ny = (int(y) + (K if c == 'W' else 0)) % ub
	for add in [0, K]:
		for (xlb, xub) in create_bound((nx + add) % ub):
			for (ylb, yub) in create_bound((ny + add) % ub):
				b[xlb][ylb] += 1
				b[xlb][yub] -= 1
				b[xub][ylb] -= 1
				b[xub][yub] += 1
for i in range(ub):
	for j in range(ub):
		b[i][j + 1] += b[i][j]
for j in range(ub):
	for i in range(ub):
		b[i + 1][j] += b[i][j]
print(max((max(b[i]) for i in range(ub))))


(N, K) = map(int, input().split())
K2 = 2 * K
inmap = [[0] * K for _ in range(K2)]
accmap = [[0] * (K + 1) for _ in range(K2 + 1)]
inmapdeb = {}
for i in range(N):
	line = input().split()
	(x, y, wb) = (int(line[0]), int(line[1]), line[2])
	if wb == 'W':
		x += K
	y %= K2
	if y >= K:
		y = y % K
		x = (x + K) % K2
	else:
		x = x % K2
	inmap[x][y] += 1

def calc(x, y):
	return accmap[x][y] + (accmap[x + K][K] - accmap[x][K] - accmap[x + K][y] + accmap[x][y]) + (accmap[K2][y] - accmap[x + K][y])
for i in range(K2):
	for j in range(K):
		accmap[i + 1][j + 1] = inmap[i][j] + accmap[i + 1][j] + accmap[i][j + 1] - accmap[i][j]
res = 0
for i in range(K):
	for j in range(K):
		cand = calc(i, j)
		res = max(res, cand, N - cand)
print(res)


(n, k) = map(int, input().split())
g = [[0] * (k + 1) for _ in range(k + 1)]
(ans, b) = (0, 0)
for _ in range(n):
	(x, y, c) = input().split()
	(x, y) = (int(x), int(y))
	d = x // k + y // k
	x %= k
	y %= k
	if (c == 'W') ^ d % 2:
		g[x + 1][y + 1] += 1
	else:
		g[x + 1][y + 1] -= 1
		b += 1
for i in range(k):
	for j in range(k):
		g[i + 1][j + 1] += g[i][j + 1] + g[i + 1][j] - g[i][j]
for i in range(k):
	for j in range(k):
		cur = g[k][k] - g[k][j] - g[i][k] + g[i][j] * 2 + b
		if cur > ans:
			ans = cur
		if n - cur > ans:
			ans = n - cur
print(ans)


import numpy as np
import io
(nime, mike) = map(int, input().split())
mod = 2 * mike
array = [[0] * (2 * mike + 1) for _ in range(2 * mike + 1)]

def gen_pattern(x):
	if x < mike - 1:
		return [[0, x + 1], [x + mike + 1, 2 * mike]]
	else:
		return [[x - mike + 1, x + 1]]
for _ in range(nime):
	(x, y, c) = input().split()
	(x, y) = (int(x), int(y))
	if c == 'W':
		y += mike
	x %= mod
	y %= mod
	for tmp in [0, mike]:
		for (l, r) in gen_pattern((x + tmp) % mod):
			for (b, t) in gen_pattern((y + tmp) % mod):
				array[l][b] += 1
				array[l][t] -= 1
				array[r][b] -= 1
				array[r][t] += 1
print(np.max(np.cumsum(np.cumsum(array, axis=0), axis=1)))


import sys
input = sys.stdin.readline
(N, K) = map(int, input().split())
S = [[0] * (2 * K + 1) for _ in range(2 * K + 1)]
dx = [0, 0, K, 2 * K, K, 0, -K, -2 * K, -K, 2 * K, 2 * K, -2 * K, -2 * K]
dy = [0, 2 * K, K, 0, -K, -2 * K, -K, 0, K, 2 * K, -2 * K, 2 * K, -2 * K]

def ch(a):
	ax = a[1]
	ay = a[0]
	if 0 <= ax and ax <= 2 * K - 1:
		if 0 <= ay and ay <= 2 * K - 1:
			return True
	return False

def ch4(a, b, c, d):
	Li = [a, b, c, d]
	for i in range(4):
		if ch(Li[i]):
			return True
	return False

def app(a):
	aa = [0, 0]
	for i in range(2):
		aa[i] = a[i]
		if aa[i] < 0:
			aa[i] = 0
		elif aa[i] > 2 * K - 1:
			aa[i] = 2 * K - 1
	return aa

def paint(a, bw):
	S[a[0]][a[1]] += bw
	return 0
for _ in range(N):
	(sx, sy, sc) = input().split()
	x = int(sx) % (2 * K)
	y = int(sy) % (2 * K)
	if sc == 'W':
		bw = 1
	else:
		bw = -1
	s = (x - (K - 1), y - (K - 1))
	for i in range(len(dx)):
		ld = [s[0] + dx[i], s[1] + dy[i]]
		ru = [ld[0] + (K - 1), ld[1] + (K - 1)]
		lu = [ru[0], ld[1]]
		rd = [ld[0], ru[1]]
		if ch4(ld, ru, lu, rd):
			ld = app(ld)
			ru = app(ru)
			ru[0] += 1
			ru[1] += 1
			lu = [ru[0], ld[1]]
			rd = [ld[0], ru[1]]
			paint(ld, bw)
			paint(ru, bw)
			paint(lu, bw * -1)
			paint(rd, bw * -1)
	if bw == -1:
		S[0][0] += 1
		S[-1][-1] += 1
		S[0][-1] -= 1
		S[-1][0] -= 1
for i in range(2 * K + 1):
	for j in range(2 * K):
		S[i][j + 1] += S[i][j]
for j in range(2 * K + 1):
	for i in range(2 * K):
		S[i + 1][j] += S[i][j]
ans = 0
for i in range(2 * K + 1):
	for j in range(2 * K + 1):
		if S[i][j] > ans:
			ans = S[i][j]
print(ans)


(N, K) = (int(i) for i in input().split())
W = list()
for index in range(N):
	(x, y, c) = (s for s in input().split())
	x = int(x)
	y = int(y)
	c = 0 if c == 'W' else 1
	if x % (2 * K) != x % K:
		c = 1 - c
	if y % (2 * K) != y % K:
		c = 1 - c
	W.append((x % K, y % K, c))
w = [[0 for _ in range(K)] for _ in range(K)]
b = [[0 for _ in range(K)] for _ in range(K)]
wx = [0 for _ in range(K)]
wy = [0 for _ in range(K)]
bx = [0 for _ in range(K)]
by = [0 for _ in range(K)]
for (x, y, c) in W:
	if c == 0:
		w[x][y] += 1
		wx[x] += 1
		wy[y] += 1
	else:
		b[x][y] += 1
		bx[x] += 1
		by[y] += 1
M = 0
for row in w:
	M += sum(row)
s = M
for x in range(K):
	tmp = s
	for y in range(K):
		s += by[y]
		s -= wy[y]
		M = max(s, N - s, M)
	for y in range(K):
		by[y] += w[x][y]
		by[y] -= b[x][y]
		wy[y] += b[x][y]
		wy[y] -= w[x][y]
	tmp += bx[x]
	tmp -= wx[x]
	s = tmp
	M = max(s, N - s, M)
print(M)


import numpy as np
(n, k) = list(map(int, input().split()))
xyc = [list(input().split()) for _ in range(n)]
b = np.zeros((3 * k - 1, 2 * k - 1), np.int64)
for (x, y, c) in xyc:
	x = int(x)
	y = int(y)
	(q, r) = divmod(x, k)
	if q % 2:
		y += k
	x = r
	if c == 'W':
		y += k
	y %= 2 * k
	b[y][x] += 1
b[:k, k:] = b[k:2 * k, :k - 1]
b[k:2 * k, k:] = b[:k, :k - 1]
b[2 * k:, :] = b[:k - 1, :]
b = np.pad(b, (1, 0), 'constant')
b = np.cumsum(b, axis=0)
b = np.cumsum(b, axis=1)
cnt = b[k:, k:] - b[k:, :-k] - b[:-k, k:] + b[:-k, :-k]
ans = cnt.max()
print(ans)


from collections import defaultdict, deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def I():
	return int(sys.stdin.readline())

def LS():
	return [list(x) for x in sys.stdin.readline().split()]

def S():
	return list(sys.stdin.readline())[:-1]

def IR(n):
	return [I() for i in range(n)]

def LIR(n):
	return [LI() for i in range(n)]

def SR(n):
	return [S() for i in range(n)]

def LSR(n):
	return [LS() for i in range(n)]
sys.setrecursionlimit(1000000)
mod = 1000000007

def check(y, x, k):
	l = max(0, min(m, x))
	r = max(0, min(m, x + k))
	d = max(0, min(m, y))
	u = max(0, min(m, y + k))
	f[d][l] += 1
	f[d][r] -= 1
	f[u][l] -= 1
	f[u][r] += 1
(n, k) = LI()
m = k * 2
f = [[0] * (m + 1) for i in range(m + 1)]
for i in range(n):
	(x, y, c) = input().split()
	x = int(x) % m
	y = int(y) % m
	if c == 'W':
		y += k
		y %= m
	check(y - m + 1, x - m + 1, k)
	check(y + 1, x - m + 1, k)
	check(y - k + 1, x - k + 1, k)
	check(y + k + 1, x - k + 1, k)
	check(y - m + 1, x + 1, k)
	check(y + 1, x + 1, k)
	check(y - k + 1, x + k + 1, k)
	check(y + k + 1, x + k + 1, k)
for y in range(m + 1):
	for x in range(m):
		f[y][x + 1] += f[y][x]
for y in range(m):
	for x in range(m + 1):
		f[y + 1][x] += f[y][x]
ans = 0
for i in f:
	ans = max(ans, max(i))
print(ans)


import numpy as np
(n, k) = map(int, input().split())
m = np.zeros((3 * k, 3 * k), dtype=int)
l = 2 * k
o = 0
for _ in range(n):
	(x, y, c) = input().split()
	(x, y) = (int(x) % l, int(y) % l)
	t = c == 'W'
	m[x, y] += 1 - 2 * t
	o += t
for _ in range(2):
	m[l:] = m[:k]
	m = np.cumsum(m, axis=0)
	m[:l] = m[k:] - m[:l]
	m = m.T
m = m[:l, :l]
print(o + (m + np.roll(np.roll(m, k, axis=0), k, axis=1)).max())


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 15
mod = 10 ** 9 + 7

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def main():
	(n, k) = LI()
	aa = [LS() for _ in range(n)]
	a = [[int(_[0]) + (k if _[2] == 'W' else 0), int(_[1])] for _ in aa]
	k2 = k * 2
	aa = [[0] * k2 for _ in range(k2)]
	dr = [0, -2, -1, 0, 1, 2, 1, 0, -1, 2, 2, -2, -2]
	dc = [0, 0, 1, 2, 1, 0, -1, -2, -1, 2, -2, 2, -2]
	for (x, y) in a:
		x %= k2
		y %= k2
		for i in range(13):
			cx = x + dr[i] * k
			cy = y + dc[i] * k
			if cx >= k2 or cy >= k2 or cx <= -k or (cy <= -k):
				continue
			mx = max(cx, 0)
			my = max(cy, 0)
			aa[mx][my] += 1
			if cx < k:
				aa[cx + k][my] -= 1
				if cy < k:
					aa[cx + k][cy + k] += 1
			if cy < k:
				aa[mx][cy + k] -= 1
	for i in range(k2):
		for j in range(1, k2):
			aa[i][j] += aa[i][j - 1]
	for i in range(k2):
		for j in range(1, k2):
			aa[j][i] += aa[j - 1][i]
	return max((max(c) for c in aa))
print(main())


(n, k) = map(int, input().split())
imos = [[0 for i in range(k)] for j in range(k)]
white = 0
for i in range(n):
	(x, y, c) = input().split()
	x = int(x)
	y = int(y)
	if c == 'W':
		c = 1
	else:
		c = 0
	x %= 2 * k
	y %= 2 * k
	if x > k - 1:
		x -= k
		c += 1
	if y > k - 1:
		y -= k
		c += 1
	c %= 2
	if c == 1:
		white += 1
		imos[x][y] -= 1
	else:
		imos[x][y] += 1
for i in range(k):
	for j in range(k - 1):
		imos[i][j + 1] += imos[i][j]
for j in range(k):
	for i in range(k - 1):
		imos[i + 1][j] += imos[i][j]
ans = 0
for i in range(k):
	for j in range(k):
		exp = imos[k - 1][k - 1] - imos[k - 1][j] - imos[i][k - 1] + 2 * imos[i][j] + white
		ans = max(ans, max(exp, n - exp))
print(ans)


def main():
	import sys
	input = sys.stdin.readline
	(N, K) = map(int, input().split())
	grid = [[0] * (K * 2) for _ in range(K * 2)]
	for _ in range(N):
		(col, row, color) = input().split()
		col = int(col)
		row = int(row)
		if color == 'W':
			row += K
		row %= K * 2
		col %= K * 2
		grid[row][col] += 1
	tbl = [[0] * (K * 2 + 1) for _ in range(K * 2 + 1)]
	for row in range(K * 2):
		for col in range(K * 2):
			tbl[row + 1][col + 1] = tbl[row + 1][col] + tbl[row][col + 1] - tbl[row][col] + grid[row][col]

	def count(row, col, row2, col2):
		return tbl[row2][col2] - tbl[row][col2] - tbl[row2][col] + tbl[row][col]
	ret = 0
	for row in range(K):
		for col in range(K):
			tmp = count(0, 0, row, col) + count(row, col, row + K, col + K) + count(row + K, col + K, K * 2, K * 2) + count(row + K, 0, K * 2, col) + count(0, col + K, row, K * 2)
			ret = max(ret, tmp, N - tmp)
	print(ret)
main()