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 Option<i32>, b: i32) {
    *a = Some(if let Some(v) = *a {
        v.max(b)
    } else {
        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<Option<i32>>>,
    l1_max: Vec<Option<i32>>,
    l2_max: Vec<Option<i32>>,
    max: Option<i32>,
    n: usize,
}

impl DP {
    fn new(n: usize) -> DP {
        DP {
            l12: vec![vec![None; n + 1]; n + 1],
            l1_max: vec![None; n],
            l2_max: vec![None; n],
            max: Some(0),
            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 q = vec![];
            for _ in 0..3 {
                for l in 0..n {
                    if let Some(l1_max) = dp.l1_max[l] {
                        let mut v = l1_max;
                        if y == z {
                            if let Some(l12) = dp.l12[l][y] {
                                v = v.max(l12 + 1);
                            }
                        }
                        q.push((l, x, v));
                    }
                }
                let mut v = dp.max.unwrap();
                if y == z {
                    if let Some(l1_max) = dp.l1_max[y] {
                        v = v.max(l1_max + 1);
                    }
                }
                q.push((x, n, v));

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