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 Rust (1.42.0) (judge verdict: Accepted)

// ---------- begin chmin, chmax ----------
trait ChangeMinMax {
    fn chmin(&mut self, x: Self) -> bool;
    fn chmax(&mut self, x: Self) -> bool;
}

impl<T: PartialOrd> ChangeMinMax for T {
    fn chmin(&mut self, x: Self) -> bool {
        *self > x && {
            *self = x;
            true
        }
    }
    fn chmax(&mut self, x: Self) -> bool {
        *self < x && {
            *self = x;
            true
        }
    }
}
// ---------- end chmin, chmax ----------

use proconio::*;
use proconio::marker::*;

fn run() {
    input! {
        n: usize,
        a: [Usize1; 3 * n],
    }
    let mut dp = vec![vec![-5000; n]; n];
    let mut max = vec![-5000; n];
    dp[a[0]][a[1]] = 0;
    dp[a[1]][a[0]] = 0;
    max[a[0]] = 0;
    max[a[1]] = 0;
    let mut add = 0;
    let mut change = vec![];
    for a in a[2..].chunks_exact(3) {
        let (a, b, c) = (a[0], a[1], a[2]);
        if a == b && b == c {
            add += 1;
            continue;
        }
        change.clear();
        let v = *max.iter().max().unwrap();
        if a == b || b == c || c == a {
            let q = a ^ b ^ c;
            let p = (a + b + c - q) / 2;
            for i in 0..n {
                change.push((i, q, dp[i][p] + 1));
                change.push((i, p, max[i]));
                change.push((i, q, max[i]));
            }
            change.push((p, p, v));
            change.push((p, q, v));
            change.push((p, p, dp[q][q] + 1));
        } else {
            change.push((b, c, dp[a][a] + 1));
            change.push((c, a, dp[b][b] + 1));
            change.push((a, b, dp[c][c] + 1));
            change.push((a, b, v));
            change.push((b, c, v));
            change.push((c, a, v));
            for &x in [a, b, c].iter() {
                for i in 0..n {
                    change.push((x, i, max[i]));
                }
            }
        }
        for &(x, y, v) in change.iter() {
            dp[x][y].chmax(v);
            dp[y][x].chmax(v);
            max[x].chmax(v);
            max[y].chmax(v);
        }
    }
    let last = *a.last().unwrap();
    let ans = add + std::cmp::max(*dp.iter().flatten().max().unwrap(), dp[last][last] + 1);
    println!("{}", ans);
}

fn main() {
    run();
}