Problem:
Score : 600 points

Problem Statement

We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.

You will do the following operation N-1 times:

Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.

After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.

Find the maximum number of points you can gain.

Constraints

1 \leq N \leq 2000

1 \leq A_i \leq N

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_{3N}

Output

Print the maximum number of points you can gain.

Sample Input 1

2
1 2 1 2 2 1

Sample Output 1

2

Let us rearrange the five leftmost cards so that the integers written on the six cards will be 2\ 2\ 2\ 1\ 1\ 1 from left to right.

Then, remove the three leftmost cards, all of which have the same integer 2, gaining 1 point.

Now, the integers written on the remaining cards are 1\ 1\ 1.

Since these three cards have the same integer 1, we gain 1 more point.

In this way, we can gain 2 points - which is the maximum possible.

Sample Input 2

3
1 1 2 2 3 3 3 2 1

Sample Output 2

1

Sample Input 3

3
1 1 2 2 2 3 3 3 1

Sample Output 3

3

Solution in C# (.NET Core 3.1.201) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;

public static class Solution
{
    static void ChMax(ref int cur, int next)
    {
        if (cur < next)
        {
            cur = next;
        }
    }

    public static void Main()
    {
        //var N = int.Parse(Console.ReadLine());
        //var A = Console.ReadLine().Split(' ').Select(x => int.Parse(x) - 1).ToArray();
        var N = 2000;
        var B = Enumerable.Range(0, N).ToArray();
        var A = Enumerable.Repeat(B, 3).SelectMany(x => x).ToArray();

        var dp = new int[N, N];
        var max = 0;
        var kMax = new int[N];
        var chain = 0;

        void ChMax(int k, int l, int val)
        {
            Solution.ChMax(ref dp[k, l], val);
            Solution.ChMax(ref dp[l, k], val);
            Solution.ChMax(ref max, val);
            Solution.ChMax(ref kMax[k], val);
            Solution.ChMax(ref kMax[l], val);
        }

        for (int y = 0; y < N; y++)
        {
            for (int x = 0; x < N; x++)
            {
                dp[y, x] = int.MinValue;
            }
        }
        dp[A[0], A[1]] = dp[A[1], A[0]] = 0;

        for (int i = 2; i < 3 * N - 1; i += 3)
        {
            var upList = new List<(int k, int l, int v)>();
            // k,l,p,p,p -> k,l
            if (A[i] == A[i + 1] && A[i] == A[i + 2])
            {
                chain++;
                continue;
            }

            // k,p,p,p,q -> k,q
            {
                var p = -1;
                var q = -1;
                if (A[i] == A[i + 1])
                {
                    p = A[i];
                    q = A[i + 2];
                }
                else if (A[i + 1] == A[i + 2])
                {
                    p = A[i + 1];
                    q = A[i];
                }
                else if (A[i] == A[i + 2])
                {
                    p = A[i];
                    q = A[i + 1];
                }
                if (p != -1)
                {
                    for (int k = 0; k < N; k++)
                    {
                        upList.Add((k, q, dp[p, k] + 1));
                    }
                }
            }

            for (int j = 0; j < 3; j++)
            {
                var p = A[i + j];
                var q = A[i + (j + 1) % 3];
                var r = A[i + (j + 2) % 3];
                // p,p,p,q,r -> q,r
                upList.Add((q, r, dp[p, p] + 1));

                // k,l,p,q,r -> p,q or q,r or r,p
                upList.Add((p, q, max));

                // k,l,p,q,r -> k,p or k,q or k,r
                for (int k = 0; k < N; k++)
                {
                    upList.Add((k, p, kMax[k]));
                }
            }

            foreach (var kv in upList)
            {
                ChMax(kv.k, kv.l, kv.v);
            }
        }

        var last = A.Last();
        dp[last, last]++;
        Solution.ChMax(ref max, dp[last, last]);
        chain += max;

        Console.WriteLine(chain);
    }
}