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: Time Limit Exceeded)

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC push_options
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#include<bits/stdc++.h>
#include <xmmintrin.h>
#include <immintrin.h>
using namespace::std;
__attribute__((constructor))void init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include<ext/pb_ds/tag_and_trait.hpp>
// #include <boost/multiprecision/cpp_dec_float.hpp>
// #include <boost/multiprecision/cpp_int.hpp>
// namespace mp = boost::multiprecision;
// typedef mp::number<mp::cpp_dec_float<0>> cdouble;
// typedef mp::cpp_int cint;
template<typename T>inline void output(T t){bool f=0;for(auto i:t){cout<<(f?" ":"")<<i;f=1;}cout<<endl;}
template<typename T>inline void output2(T t){for(auto i:t)output(i);}
template<typename T>inline void debug(T t){bool f=0;for(auto i:t){cerr<<(f?" ":"")<<i;f=1;}cerr<<endl;}
template<typename T>inline void debug2(T t){for(auto i:t)debug(i);}
template<typename T>using pbds=__gnu_pbds::tree<T,__gnu_pbds::null_type,less<T>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>;
template<typename T>using pbds_map=__gnu_pbds::tree<T,T,less<T>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>;
template<typename T,typename E>using hash_map=__gnu_pbds::gp_hash_table<T,E>;
template<typename T>using pqueue =__gnu_pbds::priority_queue<T, greater<T>,__gnu_pbds::rc_binomial_heap_tag>;
typedef long long lint;
#define INF (1LL<<60)
#define IINF (1<<30)
#define LINF (9223372036854775807LL)
#define EPS (1e-10)
#define endl ('\n')
//#define MOD 1000000007LL
#define MOD 998244353LL
//#define MOD 1000000000039LL
typedef vector<lint> vec;
typedef vector<vector<lint>> mat;
typedef vector<vector<vector<lint>>> mat3;
typedef vector<string> svec;
typedef vector<vector<string>> smat;
#define rep(i,...) for(auto i:range(__VA_ARGS__)) 
#define rrep(i,...) for(auto i:reversed(range(__VA_ARGS__)))
#define repi(i,a,b) for(lint i=lint(a);i<(lint)(b);++i)
#define rrepi(i,a,b) for(lint i=lint(b)-1;i>=lint(a);--i)
#define irep(i) for(lint i=0;;++i)
inline vector<int64_t> range(int64_t n){vector<int64_t>v(n);iota(v.begin(),v.end(),0LL);return v;}
inline vector<int64_t> range(int64_t a,int64_t b){vector<int64_t>v(b-a);iota(v.begin(),v.end(),a);return v;}
inline vector<int64_t> range(int64_t a,int64_t b,int64_t c){vector<int64_t>v((b-a+c-1)/c);for(int i=0;i<(int)v.size();++i)v[i]=i?v[i-1]+c:a;return v;}
inline auto reversed(auto v){reverse(v.begin(),v.end());return v;}
#define all(n) begin(n),end(n)
#define dist(a,b,c,d) sqrt(pow(a-c,2)+pow(b-d,2))
//inline lint gcd(lint A,lint B){return B?gcd(B,A%B):A;}
//inline lint lcm(lint A,lint B){return A/gcd(A,B)*B;}
// inline cint cgcd(cint A,cint B){return B?cgcd(B,A%B):A;}
// inline cint clcm(cint A,cint B){return A/cgcd(A,B)*B;}
bool chmin(auto& s,const auto& t){bool res=s>t;s=min(s,t);return res;}
bool chmax(auto& s,const auto& t){bool res=s<t;s=max(s,t);return res;}
const vector<lint> dx={1,0,-1,0,1,1,-1,-1};
const vector<lint> dy={0,1,0,-1,1,-1,1,-1};
#define SUM(v) accumulate(all(v),0LL)
auto call=[](auto f,auto... args){return f(f,args...);};
template<typename T,typename ...Args>auto make_vector(T x,int arg,Args ...args){if constexpr(sizeof...(args)==0)return vector<T>(arg,x);else return vector(arg,make_vector<T>(x,args...));}
int msb(int x){return 32-__builtin_clz(x);}

template<typename T>
struct pvector{
    struct node;
    using np=node*;
    struct node{
        T data=0;
        np ch[16];
        node(){
            for(int i=0;i<16;i++)ch[i]=nullptr;
        }
        static int node_count;
        void *operator new(std::size_t) {
            static node pool[10000000];
            return pool + node_count++;
        }
    };
    np root=nullptr;
    np set(lint idx,T val,np t){
        np res=t?new node(*t):new node();
        if(idx==0)res->data=val;
        else res->ch[idx&15]=set(idx>>4,val,res->ch[idx&15]);        
        return res;
    }
    T get(lint idx,np t){
        if(!t)return T();
        return idx?get(idx>>4,t->ch[idx&15]):t->data;
    }
    public:
    pvector(){}
    pvector(np t):root(t){}
    T get(lint idx){
        return get(idx,root);
    }
    pvector set(lint idx,T val){
        return pvector(set(idx,val,root));
    }
};
template<typename T>int pvector<T>::node::node_count = 0;

int main(){
    lint n;
    cin>>n;
    vec a(n*3);
    rep(i,3*n)cin>>a[i];
    rep(i,3*n)a[i]--;
    auto v=make_vector<pair<int,int>>({-1,-IINF},n,n,1);
    vector<int>mx(n,-IINF);
    int ans=0;
    auto set=[&](auto& mx,int& ans2,int s,int t,int x,int idx){
        chmax(mx[s],x);
        chmax(mx[t],x);
        chmax(ans2,x);
        if(s>t)swap(s,t);
        // cerr<<x<<endl;
        if(v[s][t].back().first==idx){
            chmax(v[s][t].back().second,x);
        }else{
            if(v[s][t].back().second<x){
                v[s][t].emplace_back(idx,x);
            }
        }
    };
    auto get=[&](lint s,lint t,lint idx){
        if(s>t)swap(s,t);
        // cerr<<v[s][t].back().first<<" "<<idx<<endl;
        if(v[s][t].back().first>idx){
            return v[s][t][v[s][t].size()-2].second;
        }else{
            return v[s][t][v[s][t].size()-1].second;
        }
    };
    if(a[0]>a[1])swap(a[0],a[1]);
    set(mx,ans,a[0],a[1],0,-1);
    lint cnt=0;
    rep(_,n-1){
        // cerr<<endl;
        auto mx2=mx;
        int ans2=ans;
        // cerr<<ans<<endl;
        vec b;
        rep(j,3){
            b.push_back(a[_*3+2+j]);
        }
        if(b[0]==b[1]&&b[1]==b[2])cnt++;
        else{
            //3つから一つ残す場合
            rep(i,n){
                rep(j,b.size()){
                    set(mx2,ans2,i,b[j],mx[i],_);
                    if(b[(j+1)%3]==b[(j+2)%3])set(mx2,ans2,i,b[j],get(i,b[(j+1)%3],_-1)+1,_);
                }
            }
            //3つから２つ残す場合
            rep(i,b.size()){
                set(mx2,ans2,b[(i+1)%3],b[(i+2)%3],max<int>(ans,get(b[i],b[i],_-1)+1),_);
            }
        }
        ans=ans2;
        mx=mx2;
    }
    cout<<max<int>(ans,get(a.back(),a.back(),n)+1)+cnt<<endl;
}