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 Go (1.14.1) (judge verdict: Wrong Answer)

package main

import (
	"fmt"
)

func main() {
	var N int
	fmt.Scanf("%d", &N)
	maxp := make([][]int, N)
	for i := 0; i < N; i++ {
		maxp[i] = make([]int, N)
	}
	maximaxp := make([]int, N)
	maxpbias := -1000000000
	tmp0 := make([]int, N)
	tmp1 := 0
	var A0, A1 int
	fmt.Scanf("%d%d", &A0, &A1)
	A0--
	A1--
	maxp[A0][A1] = 1000000000
	maxp[A1][A0] = 1000000000
	maximaxp[A1] = 1000000000
	maximaxp[A0] = 1000000000
	for i := 1; i < N; i++ {
		var A3im1, A3i, A3ip1 int
		fmt.Scanf("%d%d%d", &A3im1, &A3i, &A3ip1)
		A3im1--
		A3i--
		A3ip1--
		subroutine := func(pair, rest int) {
			for j := 0; j < N; j++ {
				tmp0[j] = maxp[pair][j] + 1
			}
			tmp1 = maxp[rest][rest] + 1

			for j := 0; j < N; j++ {
				maxp[pair][j] = maximaxp[j]
				maxp[j][pair] = maximaxp[j]
				maxp[rest][j] = maximaxp[j]
				maxp[j][rest] = maximaxp[j]
			}
			for j := 0; j < N; j++ {
				if maximaxp[pair] < maximaxp[j] {
					maximaxp[pair] = maximaxp[j]
				}
				if maximaxp[rest] < maximaxp[j] {
					maximaxp[rest] = maximaxp[j]
				}
			}

			for j := 0; j < N; j++ {
				if maxp[rest][j] < tmp0[j] {
					maxp[rest][j] = tmp0[j]
					maxp[j][rest] = tmp0[j]
				}
				if maximaxp[j] < tmp0[j] {
					maximaxp[j] = tmp0[j]
				}
				if maximaxp[rest] < tmp0[j] {
					maximaxp[rest] = tmp0[j]
				}
			}

			if maxp[pair][pair] < tmp1 {
				maxp[pair][pair] = tmp1
			}
			if maximaxp[pair] < tmp1 {
				maximaxp[pair] = tmp1
			}
		}
		if A3im1 == A3i && A3i == A3ip1 {
			maxpbias++
		} else if A3im1 == A3i {
			subroutine(A3im1, A3ip1)
		} else if A3i == A3ip1 {
			subroutine(A3i, A3im1)
		} else if A3ip1 == A3im1 {
			subroutine(A3ip1, A3i)
		} else {
			tmp1 := maxp[A3im1][A3im1] + 1
			tmp2 := maxp[A3i][A3i] + 1
			tmp3 := maxp[A3ip1][A3ip1] + 1

			for j := 0; j < N; j++ {
				maxp[A3im1][j] = maximaxp[j]
				maxp[j][A3im1] = maximaxp[j]
				maxp[A3i][j] = maximaxp[j]
				maxp[j][A3i] = maximaxp[j]
				maxp[A3ip1][j] = maximaxp[j]
				maxp[j][A3ip1] = maximaxp[j]
			}
			for j := 0; j < N; j++ {
				if maximaxp[A3im1] < maximaxp[j] {
					maximaxp[A3im1] = maximaxp[j]
				}
				if maximaxp[A3i] < maximaxp[j] {
					maximaxp[A3i] = maximaxp[j]
				}
				if maximaxp[A3ip1] < maximaxp[j] {
					maximaxp[A3ip1] = maximaxp[j]
				}
			}
			if maxp[A3i][A3ip1] < tmp1 {
				maxp[A3i][A3ip1] = tmp1
				maxp[A3ip1][A3i] = tmp1
			}
			if maximaxp[A3ip1] < tmp1 {
				maximaxp[A3ip1] = tmp1
			}
			if maximaxp[A3i] < tmp1 {
				maximaxp[A3i] = tmp1
			}

			if maxp[A3ip1][A3im1] < tmp2 {
				maxp[A3ip1][A3im1] = tmp2
				maxp[A3im1][A3ip1] = tmp2
			}
			if maximaxp[A3ip1] < tmp1 {
				maximaxp[A3ip1] = tmp1
			}
			if maximaxp[A3i] < tmp1 {
				maximaxp[A3i] = tmp1
			}

			if maxp[A3im1][A3i] < tmp3 {
				maxp[A3im1][A3i] = tmp3
				maxp[A3i][A3im1] = tmp3
			}
			if maximaxp[A3ip1] < tmp1 {
				maximaxp[A3ip1] = tmp1
			}
			if maximaxp[A3i] < tmp1 {
				maximaxp[A3i] = tmp1
			}
		}
	}
	var A3Nm1 int
	fmt.Scanf("%d", &A3Nm1)
	A3Nm1--
	maxp[A3Nm1][A3Nm1]++
	maxmaxp := 0
	for i := 0; i < N; i++ {
		for j := 0; j < N; j++ {
			if maxmaxp < maxp[i][j] {
				maxmaxp = maxp[i][j]
			}
		}
	}
	fmt.Printf("%d\n", maxmaxp+maxpbias)
}