local n, k = io.read("*n", "*n")
local k2 = k * 2
local rows_per_col = {}
for i = 1, k2 do rows_per_col[i] = {} end
local count_per_row, count_per_row2 = {}, {}
for i = 1, k2 do
  count_per_row[i] = 0
  count_per_row2[i] = 0
end
for i = 1, n do
  local x, y, c = io.read("*n", "*n", "*l")
  if c == " W" then x = x + k end
  x, y = x % k2 + 1, y % k2 + 1
  table.insert(rows_per_col[x], y)
  if x <= k then
    count_per_row[y] = count_per_row[y] + 1
  else
    count_per_row2[y] = count_per_row2[y] + 1
  end
end
local mma = math.max
local function solve()
  local val = 0
  for i = 1, k do
    val = val + count_per_row[i] + count_per_row2[i + k]
  end
  local maxval = val
  for i = 1, k do
    val = val - count_per_row[i] + count_per_row[k + i] - count_per_row2[k + i] + count_per_row2[i]
    maxval = mma(maxval, val)
  end
  for i = 1, k do
    val = val + count_per_row[i] - count_per_row[k + i] + count_per_row2[k + i] - count_per_row2[i]
    maxval = mma(maxval, val)
  end
  return maxval
end
local ret = solve(maxval)
for i = 1, k do
  for j = 1, #rows_per_col[i] do
    count_per_row[rows_per_col[i][j]] = count_per_row[rows_per_col[i][j]] - 1
    count_per_row2[rows_per_col[i][j]] = count_per_row2[rows_per_col[i][j]] + 1
  end
  for j = 1, #rows_per_col[i + k] do
    count_per_row[rows_per_col[i + k][j]] = count_per_row[rows_per_col[i + k][j]] + 1
    count_per_row2[rows_per_col[i + k][j]] = count_per_row2[rows_per_col[i + k][j]] - 1
  end
  ret = mma(ret, solve())
end
for i = 1, k do
  for j = 1, #rows_per_col[i] do
    count_per_row[rows_per_col[i][j]] = count_per_row[rows_per_col[i][j]] + 1
    count_per_row2[rows_per_col[i][j]] = count_per_row2[rows_per_col[i][j]] - 1
  end
  for j = 1, #rows_per_col[i + k] do
    count_per_row[rows_per_col[i + k][j]] = count_per_row[rows_per_col[i + k][j]] - 1
    count_per_row2[rows_per_col[i + k][j]] = count_per_row2[rows_per_col[i + k][j]] + 1
  end
  ret = mma(ret, solve())
end
print(ret)
