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: Wrong Answer)

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

fn chmax(a: &mut i64, b: i64) { *a = (*a).max(b); }
fn rotate(x: &mut usize, y: &mut usize, z: &mut usize) {
    let w = *x;
    *x = *y;
    *y = *z;
    *z = w;
}

struct DP {
    l12: [Vec<Vec<i64>>; 2],
    l1_max: [Vec<i64>; 2],
    l2_max: [Vec<i64>; 2],
    max: [i64; 2],
    now: usize,
    pre: usize,
}

impl DP {
    fn new(n: usize) -> DP {
        DP {
            l12: [vec![vec![i64::min_value(); n]; n], vec![vec![i64::min_value(); n]; n]],
            l1_max: [vec![i64::min_value(); n], vec![i64::min_value(); n]],
            l2_max: [vec![i64::min_value(); n], vec![i64::min_value(); n]],
            max: [i64::min_value(); 2],
            now: 1,
            pre: 0,
        }
    }
    fn set(&mut self, l1: usize, l2: usize, v: i64) {
        chmax(&mut self.l12[self.now][l1][l2], v);
        chmax(&mut self.l12[self.now][l2][l1], v);
        chmax(&mut self.l1_max[self.now][l1], v);
        chmax(&mut self.l1_max[self.now][l2], v);
        chmax(&mut self.l2_max[self.now][l1], v);
        chmax(&mut self.l2_max[self.now][l2], v);
        chmax(&mut self.max[self.now], v);
    }
}

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

    let mut dp = DP::new(n);
    dp.set(a[0], a[1], 0);
    dp.pre = dp.now;
    dp.now = (dp.now + 1) % 2;
    let mut mc = 0;
    for i in 1..n {
        let (mut x, mut y, mut z) = (a[i * 3 - 1], a[i * 3], a[i * 3 + 1]);
        if x == y && x == z {
            mc += 1;
        } else {
            for _ in 0..3 {
                for l in 0..n {
                    let mut v = dp.l1_max[dp.pre][l];
                    if y == z {
                        v = v.max(dp.l12[dp.pre][l][y] + 1);
                    }
                    dp.set(l, x, v);
                }
                let mut v = dp.max[dp.pre];
                if y == z {
                    v = v.max(dp.l2_max[dp.pre][y] + 1);
                }
                chmax(&mut dp.l1_max[dp.now][x], v);
                chmax(&mut dp.l2_max[dp.now][x], v);
                chmax(&mut dp.max[dp.now], v);

                let v = dp.max[dp.pre].max(dp.l12[dp.pre][z][z] + 1);
                dp.set(x, y, v);
                rotate(&mut x, &mut y, &mut z);
            }
            dp.pre = dp.now;
            dp.now = (dp.now + 1) % 2;
        }
    }
    let l = a[3 * n - 1];
    let v = dp.max[dp.pre].max(dp.l12[dp.pre][l][l] + 1);
    println!("{}", v + mc);
}