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 Java (OpenJDK 11.0.6) (judge verdict: Wrong Answer)

import java.util.Arrays;
import java.util.Scanner;
import java.util.Vector;

/**
 * sp86
 */

public class Main {
    
    static int[][]arr=new int[2001][2001];
    static int[]arr2=new int[2001];
    static int[]ar=new int[6001];
    static int cn;
    public static void main(String[] args) {
        for(int i=0;i<2001;i++)
            Arrays.fill(arr[i],-1);
        int ak,aj;
        ak=aj=0;
        Scanner sc= new Scanner(System.in);
        cn=sc.nextInt();
        for(int i=1;i<=3*cn;i++)ar[i]=sc.nextInt();
        arr[ar[1]][ar[2]]=0;
        arr[ar[2]][ar[1]]=0;
        arr2[ar[1]]=0;
        arr2[ar[2]]=0;
        
        for(int i=1;i<cn;i++){
            Vector<Tasd> ts =new Vector<Tasd>();
            int[]ar2=new int[4];
            for(int j=0;j<3;j++)ar2[j+1]=ar[3*i+j];
            if(ar2[1]==ar2[2]&&ar2[2]==ar2[3]){ak++;continue;}
            for(int j=0;j<3;j++){
                if(ar2[(j+1)%3+1]==ar2[(j+2)%3+1]){
                    int k=ar2[(j+1)%3+1];
                    for(int t=1;t<=cn;t++){
                        if(arr[k][t]==-1)continue;
                        Tasd z=new Tasd();
                        z.x=ar2[j+1];
                        z.y=t;
                        z.num=arr[k][t]+1;
                        ts.add(z);


                    }
                }
                if(arr[ar2[j+1]][ar2[j+1]]!=-1){
                    Tasd z=new Tasd();
                    z.x=ar2[(j+1)%3+1];
                    z.y=ar2[(j+2)%3+1];
                    z.num=arr[ar2[j+1]][ar2[j+1]]+1;
                    ts.add(z);
                }
                for(int t=1;t<=cn;t++){
                    if(arr2[t]==-1)continue;
                    Tasd z=new Tasd();
                    z.x=t;
                    z.y=ar2[j+1];
                    z.num=arr2[t];
                    ts.add(z);

                    
                }
                Tasd z=new Tasd();
                z.x=ar2[(j+1)%3+1];
                z.y=ar2[(j+2)%3+1];
                z.num=aj;
                ts.add(z);
            }
            for(Tasd z:ts){
               // System.out.println(z.x+" "+z.y+" "+z.num);
                aj=Math.max(aj,z.num);
                arr[z.x][z.y]=Math.max(arr[z.x][z.y],z.num);
                arr[z.y][z.x]=Math.max(arr[z.y][z.x],z.num);
                arr2[z.x]=Math.max(arr2[z.x],z.num);
                arr2[z.y]=Math.max(arr2[z.y],z.num);
            }
        }
        aj=Math.max(aj,arr[ar[3*cn]][ar[3*cn]]+1);
        int ans=ak+aj;
        System.out.println(ans);

    }
}

class Tasd {
    public int x,y,num;

    
}