Problem:
Score : 500 points

Problem Statement

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 1

4 3
0 1 W
1 2 W
5 3 B
5 4 B

Sample Output 1

4

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

Sample Input 2

2 1000
0 0 B
0 1 W

Sample Output 2

2

Sample Input 3

6 2
1 2 B
2 1 W
2 2 B
1 0 B
0 6 W
4 5 W

Sample Output 3

4

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using static System.Math;
using static System.Array;
using static AtCoder.Tool;
using static AtCoder.CalcL;
namespace AtCoder
{
    class AC
    {
        const int MOD = 1000000007;
        //const int MOD = 998244353;
        const int INF = int.MaxValue / 2;
        const long SINF = long.MaxValue / 2;
        const double EPS = 1e-8;
        static readonly int[] dI = { 0, 1, 0, -1 };
        static readonly int[] dJ = { 1, 0, -1, 0 };
        //static List<List<int>> G = new List<List<int>>();
        //static List<List<Edge>>G = new List<List<Edge>>();
        //static List<Edge> E = new List<Edge>();
        static void Main(string[] args)
        {
            //var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            //Console.SetOut(sw);
            var cin = new Scanner();

            var input = cin.ReadSplitInt();
            int N = input[0];
            int K = input[1];
            var W = new long[4010, 4010];
            for(var i = 0; i < N; i++)
            {
                var p = Console.ReadLine().Split();
                int x = int.Parse(p[0]);
                int y = int.Parse(p[1]);
                if (p[2] == "B") y += K;
                x %= 2 * K;x++;
                y %= 2 * K;y++;
                W[x, y]++;
                W[x + K, y]--;
                W[x , y + K]--;
                W[x + K, y + K]++;
            }
            for (var i = 1; i <= 3 * K + 10; i++)
            {
                for (var j = 1; j <= 3 * K + 10; j++)
                {
                    W[i, j] += W[i, j - 1] + W[i - 1, j] - W[i - 1, j - 1];
                }
            }
            var sum = new long[2 * K, K];
            long ans = 0;

            for(var i = 1; i <= 2 * K; i++)
            {
                for (var j = 1; j <= 2 * K; j++)
                {
                    if (j <= K)
                    {
                        sum[i - 1, j - 1] += W[i, j] + W[i + 2 * K, j] + W[i, j + 2 * K] + W[i + 2 * K, j + 2 * K];
                        ans = Max(ans, sum[i - 1, j - 1]);
                    }
                    else
                    {
                        int I = (i - 1 + K) % (2 * K);
                        int J = j - K - 1;
                        sum[I, J] += W[i, j] + W[i + 2 * K, j] + W[i, j + 2 * K] + W[i + 2 * K, j + 2 * K];
                        ans = Max(ans, sum[I, J]);
                    }
                }
            }
            Console.WriteLine(ans);

            /*for(var i = 0; i < 2*K; i++)
            {
                for(var j = 0; j < K; j++)
                {
                    Console.Write($"{sum[i, j]} ");
                }
                Console.WriteLine();
            }*/
           

            //Console.Out.Flush();
        }
        struct Edge
        {
            public int from;

            public int to;
            public long dist;
            public Edge(int t, long c)
            {
                from = -1;
                to = t;
                dist = c;
            }
            public Edge(int f, int t, long c)
            {
                from = f;
                to = t;
                dist = c;
            }

        }
    }
    
    public class Scanner
    {
        public int[] ReadSplitInt()
        {
            return ConvertAll(Console.ReadLine().Split(), int.Parse);
        }
        public long[] ReadSplitLong()
        {
            return ConvertAll(Console.ReadLine().Split(), long.Parse);
        }
        public double[] ReadSplit_Double()
        {
            return ConvertAll(Console.ReadLine().Split(), double.Parse);
        }
    }
    public static class Tool
    {
        static public void Initialize<T>(ref T[] array, T initialvalue)
        {
            for (var i = 0; i < array.Length; i++)
            {
                array[i] = initialvalue;
            }
        }
        static public void Swap<T>(ref T a, ref T b)
        {
            T keep = a;
            a = b;
            b = keep;
        }

        static public void Display<T>(T[,] array2d, int n, int m)
        {
            for (var i = 0; i < n; i++)
            {
                for (var j = 0; j < m; j++)
                {
                    Console.Write($"{array2d[i, j]} ");
                }
                Console.WriteLine();
            }
        }

        static public long LPow(int a, int b)
        {
            return (long)Pow(a, b);
        }
    }
    static public class CalcI
    {
        public static int Gcd(int a, int b)
        {
            if (a * b == 0) { return Max(a, b); }
            return a % b == 0 ? b : Gcd(b, a % b);
        }
        public static int Lcm(int a, int b)
        {
            int g = Gcd(a, b);
            return a / g * b;
        }
        public static int Ceil(int n, int div)
        {
            return (n + div - 1) / div;
        }
    }
    static public class CalcL
    {
        public static long Gcd(long a, long b)
        {
            if (a * b == 0) { return Max(a, b); }
            return a % b == 0 ? b : Gcd(b, a % b);
        }
        public static long Lcm(long a, long b)
        {
            long g = Gcd(a, b);
            return a / g * b;
        }
        public static long Ceil(long n, long div)
        {
            return (n + div - 1) / div;
        }
    }
    class Modulo
    {
        private const int M = 1000000007;
        private readonly int[] m_facs;
        public int Mul(int a, int b)
        {
            return (int)(Math.BigMul(a, b) % M);
        }
        public Modulo(int n)
        {
            m_facs = new int[n + 1];
            m_facs[0] = 1;
            for (int i = 1; i <= n; ++i)
            {
                m_facs[i] = Mul(m_facs[i - 1], i);
            }
        }
        public int Fac(int n)
        {
            return m_facs[n];
        }
        public int Pow(int a, int m)
        {
            switch (m)
            {
                case 0:
                    return 1;
                case 1:
                    return a;
                default:
                    int p1 = Pow(a, m / 2);
                    int p2 = Mul(p1, p1);
                    return ((m % 2) == 0) ? p2 : Mul(p2, a);
            }
        }
        public int Div(int a, int b)
        {
            return Mul(a, Pow(b, M - 2));
        }
        public int Ncr(int n, int r)
        {
            if (n < r) { return 0; }
            if (n == r) { return 1; }
            int res = Fac(n);
            res = Div(res, Fac(r));
            res = Div(res, Fac(n - r));
            return res;
        }
        public Modulo() { }
    }
}