#ABC176F. [ABC176F] Brave CHAIN

[ABC176F] Brave CHAIN

Score : 600600 points

Problem Statement

We have 3N3N cards arranged in a row from left to right, where each card has an integer between 11 and NN (inclusive) written on it. The integer written on the ii-th card from the left is AiA_i.

You will do the following operation N1N-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 11 point.

After these N1N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 11 additional point.

Find the maximum number of points you can gain.

Constraints

  • 1N20001 \leq N \leq 2000
  • 1AiN1 \leq A_i \leq N

Input

Input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \cdots A3NA_{3N}

Output

Print the maximum number of points you can gain.

2
1 2 1 2 2 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 12\ 2\ 2\ 1\ 1\ 1 from left to right.

Then, remove the three leftmost cards, all of which have the same integer 22, gaining 11 point.

Now, the integers written on the remaining cards are 1 1 11\ 1\ 1.

Since these three cards have the same integer 11, we gain 11 more point.

In this way, we can gain 22 points - which is the maximum possible.

3
1 1 2 2 3 3 3 2 1
1
3
1 1 2 2 2 3 3 3 1
3