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 <bits/stdc++.h>
#define sz(v)   ((int)(v).size())
#define  all(v)    ((v).begin()),((v).end())
#define  allr(v)    ((v).rbegin()),((v).rend())
#define   pb         push_back
#define   mp         make_pair
#define    clr(v,d)      memset( v, d ,sizeof(v))
#define     PI                       acos(-1)
typedef  long long     ll ;
typedef  unsigned long long ull;
const double eps= (1e-9);
using namespace std;
int dcmp(long double a,long double b){   return fabsl(a-b)<=eps ? 0: (a>b)? 1:-1  ;}
int getBit(ll num, int idx) {return ((num >> idx) & 1ll) == 1;}
int setBit1(int num, int idx) {return num | (1<<idx);}
ll setBit0(ll num, int idx) {return num & ~(1ll<<idx);}
ll flipBit(ll num, int idx) {return num ^ (1ll<<idx);}
void GO(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);}
int countNumBit1(int mask) {int ret=0; while (mask) 	{mask &= (mask-1);++ret;	}return ret;}
const int N=300000+5,mod=1e9+7;
int a[N],n;
map< pair<pair<int,int>,int> ,int> mem;
int check(int i,int j,vector<int> &temp){
    int val=-1;
   for(int k=0;k<sz(temp);k++){
     if(k==i||k==j) continue;
     if(val==-1)
     val=temp[k];
     else{
        if(temp[k]!=val) return 0;
     }
   }
   return 1;
}
int solve(int pre,int ppre,int id){

   if(id+3-1>3*n-2){
     return 0;
   }
   auto it=mem.find(mp(mp(pre,ppre),id));
   if(it!=mem.end()){
    return (*it).second;
   }
   vector<int> temp;
   temp.pb(pre);
   temp.pb(ppre);
   for(int i=id;i<id+3;i++) temp.pb(a[i]);
   int ans=0;
   for(int i=0;i<sz(temp);i++){

    for(int j=i+1;j<sz(temp);j++){
        ans=max(ans,check(i,j,temp)+solve(temp[i],temp[j],id+3));
    }
   }
  return (*it).second=ans;

}
int main(){
    GO();

    cin>>n;
    for(int i=0;i<3*n;i++) cin>>a[i];
    cout<<solve(a[0],a[1],2)<<"\n";

}