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: Runtime Error)

#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let n = read::<usize>();
    let a = read_vec::<usize>()
        .iter()
        .map(|&x| x - 1)
        .collect::<Vec<_>>();

    let mut a = a.into_iter().collect::<VecDeque<_>>();
    let mut ans = 0;
    'o: while a.len() >= 5 {
        let mut count = HashMap::new();
        for i in 0..5 {
            *count.entry(a[i]).or_insert(0) += 1;
        }
        for (k, v) in count {
            if v >= 3 {
                ans += 1;
                let mut c = 0;
                let mut popped = vec![];
                for _ in 0..5 {
                    let top = a.pop_front().unwrap();
                    if top == k {
                        c += 1;
                        if c >= 4 {
                            popped.push(top);
                        }
                    } else {
                        popped.push(top);
                    }
                }
                for elem in popped {
                    a.push_front(elem);
                }
                continue 'o;
            }
        }
        let mut count = vec![0; n];
        let mut holded_nums = vec![];
        for i in 0..a.len() {
            count[a[i]] += 1;
            if count[a[i]] == 3 {
                holded_nums.push(a[i]);
            }
            if holded_nums.len() >= 3 {
                break;
            }
        }
        let mut holded = vec![];
        'o1: for hi in 0..5 {
            for i in 0..5 {
                if a[i] == holded_nums[hi] {
                    holded.push(a[i]);
                    if holded.len() >= 3 {
                        break 'o1;
                    }
                }
            }
        }
        for i in 0..5 {
            if holded.len() == 3 {
                break;
            }
            if holded.iter().filter(|&&x| x == i).count() == 0 {
                holded.push(i);
            }
        }
        for i in 0..5 {
            a.pop_front();
        }
        for elem in holded {
            a.push_front(elem);
        }
        //let mut hold = vec![];
    }
    if a.iter().all(|&x| x == a[0]) {
        ans += 1;
    }

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

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}