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));
	int all = 0;
	dp[min(A[0], A[1])][max(A[0], A[1])] = 0;

	for(int i=2; i<N*3; i+=3){
		if(A[i] == A[i+1] && A[i] == A[i+2]){
			all++;
			continue;
		}

		vector<int> cs = {A[i], A[i+1], A[i+2]};
		sort(cs.begin(), cs.end());

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

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

		int a = cs[0], b = cs[1], c = cs[2];
		dp[a][b] = max(dp[a][b], dp[c][c] >= 0 ? dp[c][c] + 1 : -1);
		dp[a][b] = max(dp[a][b], 0);
		dp[b][c] = max(dp[b][c], dp[a][a] >= 0 ? dp[a][a] + 1 : -1);
		dp[b][c] = max(dp[b][c], 0);
		dp[a][c] = max(dp[a][c], dp[b][b] >= 0 ? dp[b][b] + 1 : -1);
		dp[a][c] = max(dp[a][c], 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 + all;
}


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;
}