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>
//#include <chrono>
//#pragma GCC optimize("Ofast")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N,M,H,W,Q,K,A,B;
string S;
typedef pair<ll, ll> P;
const ll INF = (1LL<<58);

template<class T> bool chmin(T &a, const T &b){
    if(a > b) {a = b; return true;}
    else return false;
}
template<class T> bool chmax(T &a, const T &b){
    if(a < b) {a = b; return true;}
    else return false;
}

int main() {
    cin>>N;
    vec a(N * 3);
    rep(i, N * 3) {cin>>a[i]; --a[i];}
    rep(i, N - 1) sort(a.begin() + i * 3 + 2, a.begin() + i * 3 + 5);
    vector<set<int> > dp(N);
    dp[a[0]].insert(a[1]);
    dp[a[1]].insert(a[0]);
    int ans = 0;
    bitset<2000> keep;
    for(int i = 2; i + 2 < N * 3; i += 3){
        keep = 0;
        if(a[i] == a[i + 1] && a[i + 1] == a[i + 2]){
            ++ans;
            continue;
        }
        bool plus = false, swapped = false;
        if(a[i + 1] == a[i + 2]){
            swapped = true;
            swap(a[i], a[i + 2]);
        }
        if(a[i] == a[i + 1]){
            bool after = dp[a[i + 2]].find(a[i + 2]) != dp[a[i + 2]].end();
            if(!dp[a[i]].empty()){
                dp[a[i + 2]] = dp[a[i]];
                keep[a[i + 2]] = true;
            }
            plus = after | keep[a[i + 2]];
            rep(j, N) if(!keep[j]) dp[j].clear();
            for(int c : dp[a[i + 2]]) dp[c].insert(a[i + 2]);
            if(after) dp[a[i]].insert(a[i]);
        }else{
            rep(k, 3){
                if(dp[a[i + k]].find(a[i + k]) != dp[a[i + k]].end()){
                    plus = true;
                    keep[a[i + k]] = true;
                }
            }
            rep(j, N){
                if(keep[j]){
                    rep(k, 3) dp[j].insert(a[i + k]);
                    dp[j].erase(j);
                }else{
                    dp[j].clear();
                }
            }
        }
        if(swapped) swap(a[i], a[i + 2]);

        ans += plus;
        if(!plus){
            rep(j, N) keep[j] = !dp[j].empty();
            rep(j, N) rep(k, 3) {
                dp[j].insert(a[i + k]);
                if(keep[j]) dp[a[i + k]].insert(j);
            }
        }
    }
    int la = *a.rbegin();
    ans += (dp[la].find(la) != dp[la].end());
    cout<<ans<<endl;
}