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: Runtime Error)

#include<bits/stdc++.h>
 
using namespace std;
 
#define LL long long int
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
const int N = 2e3 + 5;
const int oo = 1e9 + 5;
const LL ooll = (LL)1e18 + 5;
const int MOD = 1e9 + 7;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rand(l, r) uniform_int_distribution<int>(l, r)(rng)

clock_t start = clock();

int dp[N][N], t[N][N], n;

void upd(int x, int y, int z) {
    for (int i=1;i<=n;++i) for (int j=1;j<=n;++j) t[i][j] = -oo;
    vector<int> v({x, y, z});
    sort(v.begin(), v.end());
    do {
        x = v[0]; y = v[1]; z = v[2];
        for (int i=1;i<=n;++i) for (int j=1;j<=n;++j) {
            t[i][j] = max(t[i][j], dp[i][j] + ((x == y) && (y == z)));
            t[x][y] = max(t[x][y], dp[i][j] + ((i == j) && (j == z)));
            t[i][z] = max(t[i][z], dp[i][j] + ((x == y) && (y == j)));
        }
    } while(next_permutation(v.begin(), v.end()));

    for (int i=1;i<=n;++i) for (int j=1;j<=n;++j) dp[i][j] = t[i][j];
}

int v[3*N];

void solve() {  
    cin >> n;
    assert(n<500);
    for (int i=1;i<=3*n;++i) {
        cin >> v[i];
        assert(v[i] >= 1 && v[i] <= n);
    }
    if (n == 1) {
        cout << ((v[1] == v[2]) && (v[2] == v[3])) << '\n';
        return;
    }
    for (int i=1;i<=n;++i) for (int j=1;j<=n;++j) dp[i][j] = -oo;

    sort(v+1, v+6);

    do {
        dp[v[1]][v[2]] = max(dp[v[1]][v[2]], (int)((v[3] == v[4]) && (v[4] == v[5])));
    }while(next_permutation(v+1, v+6));

    for (int i=6;i<3*n;i+=3) {
        upd(v[i], v[i+1], v[i+2]);
    }

    int ans = 0;
    for (int i=1;i<=n;++i) for (int j=1;j<=n;++j) {
        ans = max(ans, dp[i][j] + ((i == j) && (j == v[3*n])));
    }

    cout << ans << '\n';
}

int main() {
    FASTIO;
    int T = 1;
    // cin >> T;
    while(T--) {
        solve();
    }
    // cerr << fixed << setprecision(10);
    // cerr << (clock() - start) / ((long double)CLOCKS_PER_SEC) << '\n';
    return 0;
}