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, marker::Usize1};

fn chmax(a: &mut i32, b: i32) { *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<i32>>,
    l1_max: Vec<i32>,
    l2_max: Vec<i32>,
    max: i32,
    n: usize,
}

impl DP {
    fn new(n: usize) -> DP {
        DP {
            l12: vec![vec![i32::min_value(); n + 1]; n + 1],
            l1_max: vec![i32::min_value(); n],
            l2_max: vec![i32::min_value(); n],
            max: i32::min_value(),
            n: n,
        }
    }
    fn set(&mut self, l1: usize, l2: usize, v: i32) {
        if l2 != self.n {
            chmax(&mut self.l12[l1][l2], v);
            chmax(&mut self.l12[l2][l1], v);
            chmax(&mut self.l1_max[l2], v);
            chmax(&mut self.l2_max[l2], v);
        }
        chmax(&mut self.l1_max[l1], v);
        chmax(&mut self.l2_max[l1], v);
        chmax(&mut self.max, v);
    }
}

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

    let mut dp = DP::new(n);
    dp.set(a[0], a[1], 0);
    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 {
            let mut h = std::collections::HashMap::new();
//            let mut q = vec![];
            for _ in 0..3 {
                for l in 0..n {
                    let mut v = dp.l1_max[l];
                    if y == z {
                        v = v.max(dp.l12[y][l] + 1);
                    }
//                    q.push((x, l, v));
                    let hv = h.entry((x, l)).or_insert(v);
                    chmax(hv, v);
                }
                let mut v = dp.max;
                if y == z {
                    v = v.max(dp.l1_max[y] + 1);
                }
//                q.push((x, n, v));
                let hv = h.entry((x, n)).or_insert(v);
                chmax(hv, v);

                let v = dp.max.max(dp.l12[z][z] + 1);
//                q.push((x, y, v));
                let hv = h.entry((x, y)).or_insert(v);
                chmax(hv, v);
                rotate(&mut x, &mut y, &mut z);
            }
//            for (x, y, v) in q {
            for ((x, y), v) in h {
                dp.set(x, y, v);
            }
        }
    }
    let l = a[3 * n - 1];
    let v = dp.max.max(dp.l12[l][l] + 1);
    println!("{}", v + mc);
}