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++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <complex>
#include <cmath>
#include <numeric>
#include <bitset>
#include <functional>
#include <random>
#include <ctime>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int N, A[6005];

int solve()
{
	int ans = 0;
	vector<vector<int>> dp(N + 1, vector<int>(N + 1, -1));
	dp[min(A[0], A[1])][max(A[0], A[1])] = 0;
	vector<int> vis(N+1, 0);
	int j = 0;

	for (int i = 2; i < N * 3; i += 3)
	{
		while(j < i){
			vis[A[j]] = 1;
			j++;
		}

		vector<int> cs = {A[i], A[i+1], A[i+2]};
		std::sort(cs.begin(), cs.end());
		if(cs[0] == cs[1] && cs[0] == cs[2]){
			for(int a=1; a<=N; a++){
				for(int b=a; b<=N; b++){
					if(dp[a][b] >= 0){
						dp[a][b]++;
					}
				}
			}

		}else if(cs[0] == cs[1]){
			int c = cs[0], b = cs[2];
			if(dp[b][b] >= 0){
				dp[c][c] = max(dp[c][c], dp[b][b] + 1);
			}
			for(int a=1; a<=N; a++){
				int p = dp[min(a, c)][max(a, c)];
				if(p < 0) continue;

				dp[min(a,b)][max(a,b)] = max(dp[min(a,b)][max(a,b)], p + 1); 
			}



		}else if(cs[1] == cs[2]){
			int c = cs[1], b = cs[0];
			if(dp[b][b] >= 0){
				dp[c][c] = max(dp[c][c], dp[b][b] + 1);
			}
			for(int a=1; a<=N; a++){
				int p = dp[min(a, c)][max(a, c)];
				if(p < 0) continue;

				dp[min(a,b)][max(a,b)] = max(dp[min(a,b)][max(a,b)], p + 1); 
			}

		}else{
			for(int j=0; j<3; j++){
				int a = cs[j];
				int p = dp[a][a];
				if(p < 0) continue;

				vector<int> cs2;
				for(int k=0; k < 3; k++){
					if(k != j){
						cs2.push_back(cs[k]);
					}
				}
				int b = cs2[0], c = cs2[1];
				dp[b][c] = max(dp[b][c], p + 1);
			}

		}

		for(int a=1; a<=N; a++){
			if(vis[a] == 0) continue;
			for(int c : cs){
				dp[min(a, c)][max(a, c)] = max(dp[min(a, c)][max(a, c)], 0);
			}
		}
		for(int i=0; i<3; i++){
			for(int j=i+1; j<3; j++){
				dp[cs[i]][cs[j]] = max(dp[cs[i]][cs[j]], 0);
			}
		}
	}

	for (int a = 1; a <= N; a++)
	{
		for (int b = 1; b <= N; b++)
		{
			ans = max(ans, dp[a][b]);
		}
	}

	ans = max(ans, dp[0][0]);
	return ans;
}

int main()
{
	//freopen("in.txt", "r", stdin);
	memset(A, 0, sizeof(A));
	cin >> N;
	for (int i = 0; i < 3 * N; i++)
	{
		cin >> A[i];
	}
	cout << solve() << endl;
	return 0;
}