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: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using CompLib.Util;

public class Program
{
    int N;
    int[] A;
    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextInt();
        A = sc.IntArray();
        for (int i = 0; i < 3 * N; i++)
        {
            A[i]--;
        }

        // i,jが残ってる最大
        var dp = new int[N, N];
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            {
                dp[i, j] = int.MinValue;
            }
        }


        int cnt = 0;
        dp[A[0], A[1]] = 0;
        dp[A[1], A[0]] = 0;

        // dp[i,j]の最大値
        int max = 0;

        // dp[i]の最大値
        int[] max2 = new int[N];
        for (int i = 0; i < N; i++)
        {
            max2[i] = int.MinValue;
        }

        max2[A[0]] = 0;
        max2[A[1]] = 0;

        for (int i = 0; i < N - 1; i++)
        {
            // 更新
            var ls = new List<(int j, int k, int v)>();
            int[] p = new int[] { A[i * 3 + 2], A[i * 3 + 3], A[i * 3 + 4] };

            // 3つとも同じ
            if (p[0] == p[1] && p[1] == p[2])
            {
                cnt++;
                continue;
            }

            for (int t = 0; t < 3; t++)
            {
                Swap(ref p[0], ref p[t]);
                // 2ついっしょ
                if (p[1] == p[2])
                {
                    for (int k = 0; k < N; k++)
                    {
                        if (dp[p[1], k] != int.MinValue)
                        {
                            ls.Add((k, p[0], dp[p[1], k] + 1));
                            ls.Add((p[0], k, dp[p[1], k] + 1));
                        }
                    }
                }

                // p[0]と現在の2つで作る
                if (dp[p[0], p[0]] != int.MinValue)
                {
                    ls.Add((p[1], p[2], dp[p[0], p[0]] + 1));
                    ls.Add((p[2], p[1], dp[p[0], p[0]] + 1));
                }

                // 作らない 現在の2つ、p[0]を使う
                ls.Add((p[1], p[2], max));
                ls.Add((p[2], p[1], max));

                // 作らない p[1],p[2],前のやつの片方使う
                for (int k = 0; k < N; k++)
                {
                    if (max2[k] != int.MinValue)
                    {
                        ls.Add((p[0], k, max2[k]));
                        ls.Add((k, p[0], max2[k]));
                    }
                }

                // 作らない p3つ使う
                // 使い回す
            }

            // 更新
            foreach (var tuple in ls)
            {
                dp[tuple.j, tuple.k] = Math.Max(dp[tuple.j, tuple.k], tuple.v);
                max2[tuple.j] = Math.Max(max2[tuple.j], tuple.v);
                max = Math.Max(max, tuple.v);
            }
        }

        int ans = int.MinValue;
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            {
                if (dp[i, j] == int.MinValue) continue;
                var p = dp[i, j];
                if (i == j && j == A[3 * N - 1]) p++;
                ans = Math.Max(ans, p + cnt);
            }
        }

        Console.WriteLine(ans);
    }

    void Swap(ref int l, ref int r)
    {
        int t = l;
        l = r;
        r = t;
    }

    public static void Main(string[] args) => new Program().Solve();
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            if (_index >= _line.Length)
            {
                string s;
                do
                {
                    s = Console.ReadLine();
                } while (s.Length == 0);

                _line = s.Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public string ReadLine()
        {
            _index = _line.Length;
            return Console.ReadLine();
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            string s = Console.ReadLine();
            _line = s.Length == 0 ? new string[0] : s.Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}