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)

/// input macro from https://qiita.com/tanakh/items/1ba42c7ca36cd29d0ac8
macro_rules ! read_value {($ next : expr , ($ ($ t : tt ) ,* ) ) => {($ (read_value ! ($ next , $ t ) ) ,* ) } ; ($ next : expr , [$ t : tt ; $ len : expr ] ) => {(0 ..$ len ) . map (| _ | read_value ! ($ next , $ t ) ) . collect ::< Vec < _ >> () } ; ($ next : expr , chars ) => {read_value ! ($ next , String ) . chars () . collect ::< Vec < char >> () } ; ($ next : expr , usize1 ) => {read_value ! ($ next , usize ) - 1 } ; ($ next : expr , $ t : ty ) => {$ next () . parse ::<$ t > () . expect ("Parse error" ) } ; }
macro_rules ! input_inner {($ next : expr ) => {} ; ($ next : expr , ) => {} ; ($ next : expr , $ var : ident : $ t : tt $ ($ r : tt ) * ) => {let $ var = read_value ! ($ next , $ t ) ; input_inner ! {$ next $ ($ r ) * } } ; }
macro_rules ! input {(source = $ s : expr , $ ($ r : tt ) * ) => {let mut iter = $ s . split_whitespace () ; let mut next = || {iter . next () . unwrap () } ; input_inner ! {next , $ ($ r ) * } } ; ($ ($ r : tt ) * ) => {let stdin = std :: io :: stdin () ; let mut bytes = std :: io :: Read :: bytes (std :: io :: BufReader :: new (stdin . lock () ) ) ; let mut next = move || -> String {bytes . by_ref () . map (| r | r . unwrap () as char ) . skip_while (| c | c . is_whitespace () ) . take_while (| c |! c . is_whitespace () ) . collect () } ; input_inner ! {next , $ ($ r ) * } } ; }

fn solve() {
    input!(n: usize, a: [usize1; 3 * n]);

    let mut dp = vec![vec![(0, false); n]; n];
    dp[a[0]][a[1]].1 = true;
    dp[a[1]][a[0]].1 = true;

    for i in 0..(n - 1) {
        let mut dp2 = dp.clone();
        if a[i * 3 + 2] == a[i * 3 + 3] && a[i * 3 + 2] == a[i * 3 + 4] {
            for j in 0..n {
                for k in 0..n {
                    dp2[j][k] = (dp[j][k].0 + 1, dp[j][k].1);
                }
            }
        }
        else {
            if a[i * 3 + 2] == a[i * 3 + 3] {
                let other = 4;
                let tmp = a[i * 3 + 2];
                for j in 0..n {
                    dp2[j][a[i * 3 + other]] = {
                        if dp[j][a[i * 3 + other]].1 ^ dp[j][tmp].1 {
                            if dp[j][a[i * 3 + other]].1 {
                                dp[j][a[i * 3 + other]]
                            }
                            else {
                                (dp[j][tmp].0 + 1, dp[j][tmp].1)
                            }
                        }
                        else {
                            (
                                dp[j][a[i * 3 + other]].0.max(dp[j][tmp].0 + 1),
                                dp[j][tmp].1,
                            )
                        }
                    };
                    dp2[a[i * 3 + other]][j] = dp2[j][a[i * 3 + other]];
                }
            }
            else if a[i * 3 + 2] == a[i * 3 + 4] {
                let other = 3;
                let tmp = a[i * 3 + 2];
                for j in 0..n {
                    dp2[j][a[i * 3 + other]] = {
                        if dp[j][a[i * 3 + other]].1 ^ dp[j][tmp].1 {
                            if dp[j][a[i * 3 + other]].1 {
                                dp[j][a[i * 3 + other]]
                            }
                            else {
                                (dp[j][tmp].0 + 1, dp[j][tmp].1)
                            }
                        }
                        else {
                            (
                                dp[j][a[i * 3 + other]].0.max(dp[j][tmp].0 + 1),
                                dp[j][tmp].1,
                            )
                        }
                    };
                    dp2[a[i * 3 + other]][j] = dp2[j][a[i * 3 + other]];
                }
            }
            else if a[i * 3 + 3] == a[i * 3 + 4] {
                let other = 2;
                let tmp = a[i * 3 + 3];
                for j in 0..n {
                    dp2[j][a[i * 3 + other]] = {
                        if dp[j][a[i * 3 + other]].1 ^ dp[j][tmp].1 {
                            if dp[j][a[i * 3 + other]].1 {
                                dp[j][a[i * 3 + other]]
                            }
                            else {
                                (dp[j][tmp].0 + 1, dp[j][tmp].1)
                            }
                        }
                        else {
                            (
                                dp[j][a[i * 3 + other]].0.max(dp[j][tmp].0 + 1),
                                dp[j][tmp].1,
                            )
                        }
                    };
                    dp2[a[i * 3 + other]][j] = dp2[j][a[i * 3 + other]];
                }
            }
            else {
                dp2[a[i * 3 + 2]][a[i * 3 + 3]] = {
                    let other = 5;
                    if dp[a[i * 3 + other]][a[i * 3 + other]].1 ^ dp[a[i * 3 + 2]][a[i * 3 + 3]].1 {
                        if dp[a[i * 3 + 2]][a[i * 3 + 3]].1 {
                            dp[a[i * 3 + 2]][a[i * 3 + 3]]
                        }
                        else {
                            (
                                dp[a[i * 3 + other]][a[i * 3 + other]].0 + 1,
                                dp[a[i * 3 + other]][a[i * 3 + other]].1,
                            )
                        }
                    }
                    else {
                        (
                            dp[a[i * 3 + 2]][a[i * 3 + 3]]
                                .0
                                .max(dp[a[i * 3 + other]][a[i * 3 + other]].0 + 1),
                            dp[a[i * 3 + other]][a[i * 3 + other]].1,
                        )
                    }
                };
                dp2[a[i * 3 + 2]][a[i * 3 + 4]] = {
                    let other = 3;
                    if dp[a[i * 3 + other]][a[i * 3 + other]].1 ^ dp[a[i * 3 + 2]][a[i * 3 + 4]].1 {
                        if dp[a[i * 3 + 2]][a[i * 3 + 4]].1 {
                            dp[a[i * 3 + 2]][a[i * 3 + 4]]
                        }
                        else {
                            (
                                dp[a[i * 3 + other]][a[i * 3 + other]].0 + 1,
                                dp[a[i * 3 + other]][a[i * 3 + other]].1,
                            )
                        }
                    }
                    else {
                        (
                            dp[a[i * 3 + 2]][a[i * 3 + 4]]
                                .0
                                .max(dp[a[i * 3 + other]][a[i * 3 + other]].0 + 1),
                            dp[a[i * 3 + other]][a[i * 3 + other]].1,
                        )
                    }
                };
                dp2[a[i * 3 + 3]][a[i * 3 + 4]] = {
                    let other = 2;
                    if dp[a[i * 3 + other]][a[i * 3 + other]].1 ^ dp[a[i * 3 + 3]][a[i * 3 + 4]].1 {
                        if dp[a[i * 3 + 3]][a[i * 3 + 4]].1 {
                            dp[a[i * 3 + 3]][a[i * 3 + 4]]
                        }
                        else {
                            (
                                dp[a[i * 3 + other]][a[i * 3 + other]].0 + 1,
                                dp[a[i * 3 + other]][a[i * 3 + other]].1,
                            )
                        }
                    }
                    else {
                        (
                            dp[a[i * 3 + 3]][a[i * 3 + 4]]
                                .0
                                .max(dp[a[i * 3 + other]][a[i * 3 + other]].0 + 1),
                            dp[a[i * 3 + other]][a[i * 3 + other]].1,
                        )
                    }
                };

                dp2[a[i * 3 + 3]][a[i * 3 + 2]] = dp2[a[i * 3 + 2]][a[i * 3 + 3]];
                dp2[a[i * 3 + 4]][a[i * 3 + 2]] = dp2[a[i * 3 + 2]][a[i * 3 + 4]];
                dp2[a[i * 3 + 4]][a[i * 3 + 3]] = dp2[a[i * 3 + 3]][a[i * 3 + 4]];
            }
        }
        dp = dp2;
    }

    if a[3 * n - 2] == a[3 * n - 1] {
        for i in 0..n {
            dp[i][a[3 * n - 2]].0 += 1;
            if i != a[3 * n - 2] {
                dp[a[3 * n - 2]][i].0 += 1;
            }
        }
    }
    else {
        dp[a[3 * n - 2]][a[3 * n - 2]].0 += 1;
        dp[a[3 * n - 1]][a[3 * n - 1]].0 += 1;
    }

    let mut max_value = 0;
    for v in dp {
        for (c, b) in v {
            if b && c > max_value {
                max_value = c;
            }
        }
    }

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

fn main() {
    std::thread::Builder::new()
        .name("solve".into())
        .stack_size(256 * 1024 * 1024)
        .spawn(solve)
        .unwrap()
        .join()
        .unwrap();
}