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)

use proconio::{input, fastout};
use proconio::marker::Usize1;

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
        }
    }
}

struct DpSolver {
    dp: Vec<Vec<i64>>,
    max: Vec<i64>,
    maximum: i64,
    update_stack: Vec<(usize, usize, i64)>,
}

impl DpSolver {
    fn register(&mut self, a: usize, b: usize, c: i64) {
        self.update_stack.push((a.min(b), a.max(b), c));
    }

    fn register_up_count(&mut self, a: usize, b: usize, c: usize, d: usize) {
        let a1 = a.min(b);
        let a2 = a.max(b);
        if self.dp[a1][a2] != -1 {
            self.register(c.min(d), c.max(d), self.dp[a1][a2] + 1);
        }
    }

    fn update(&mut self, a: usize, b: usize, c: i64) {
        self.dp[a][b].chmax(c);
        self.max[a].chmax(c);
        self.max[b].chmax(c);
        self.maximum.chmax(c);
    }

    fn do_update(&mut self) {
        while !self.update_stack.is_empty() {
            let (a, b, c) = self.update_stack.pop().unwrap();
            self.update(a, b, c);
        }
    }
}


#[fastout]
fn main() {
    input! {
        n: usize,
        aaa: [Usize1; n * 3],
    }

    let mut dps = DpSolver {
        dp: vec![vec![-1; n]; n],
        max: vec![-1; n],
        maximum: 0,
        update_stack: vec![],
    };
    let mut add = 0i64;
    let a = aaa[0].min(aaa[1]);
    let b = aaa[0].max(aaa[1]);
    dps.update(a, b, 0);

    for s in aaa[2..].chunks_exact(3) {
        let (a0, a1, a2) = (s[0], s[1], s[2]);
        if a0 == a1 && a1 == a2 {
            add += 1;
            continue;
        }
        if a0 == a1 || a1 == a2 || a2 == a0 {
            let q = a0 ^ a1 ^ a2;
            let p = (a0 + a1 + a2 - q) / 2;
            for b in 0..n {
                dps.register(q, b, dps.max[b]);
                dps.register_up_count(p, b, q, b);
            }
            dps.register(p, p, dps.maximum);
            dps.register(p, q, dps.maximum);
            dps.register_up_count(q, q, p, p);
        } else {
            dps.register(a0, a1, dps.maximum);
            dps.register(a0, a2, dps.maximum);
            dps.register(a1, a2, dps.maximum);
            dps.register_up_count(a0, a0, a1, a2);
            dps.register_up_count(a1, a1, a0, a2);
            dps.register_up_count(a2, a2, a0, a1);
            for b in 0..n {
                dps.register(a0, b, dps.max[b]);
                dps.register(a1, b, dps.max[b]);
                dps.register(a2, b, dps.max[b]);
            }
        }
        dps.do_update();
    }

    let &la = aaa.last().unwrap();
    dps.maximum.chmax(dps.dp[la][la] + 1);

    let ans = add + dps.maximum;

    println!("{}", ans);
}