#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define LL long long
#define pii pair<int,int>
#define MEM(x) memset(x,0,sizeof(x))
using namespace std;
vector<int> v[2005];
int a[6005];
int dp[6005][6005];
int Max[6005];
void solve(){
    int n;
    scanf("%d",&n);
    for(int i = 0;i<3*n;i++){
        scanf("%d",&a[i]);
    }
    v[a[0]].pb(0);
    v[a[1]].pb(1);
    int ans=0,add=0;
    for(int i = 1;i<n;i++){
        //a[i*3-1],a[i*3],a[i*3+1];
        if(a[i*3-1]==a[i*3]&&a[i*3]==a[i*3+1]){
            add++;
            continue;
        }
        for(int j =i*3-1;j<=i*3+1;j++){
            for(int k=j+1;k<=i*3+1;k++){
                dp[j][k]=max(dp[j][k],ans);
                Max[k]=max(Max[k],dp[j][k]);
                Max[j]=max(Max[j],dp[j][k]);
            }
        }
        for(int j = 0;j<i*3-1;j++){
            for(int k=i*3-1;k<=i*3+1;k++){
                dp[j][k]=max(dp[j][k],Max[j]);
                Max[j]=max(Max[j],dp[j][k]);
                Max[k]=max(Max[k],dp[j][k]);
            }
        }
        for(int j=i*3-1;j<=i*3+1;j++){
            if(v[a[j]].size()>=2){
                int x=v[a[j]].back(),y=v[a[j]][v[a[j]].size()-2];
                int aa,bb;
                if(j!=i*3-1)aa=i*3-1;
                else aa=i*3;
                if(j==i*3+1)bb=i*3;
                else bb=i*3+1;
                dp[aa][bb]=max(dp[aa][bb],dp[y][x]+1);
             //   printf("!%d %d %d\n",aa,bb,dp[aa][bb]);
                ans=max(ans,dp[aa][bb]);
                Max[aa]=max(Max[aa],dp[aa][bb]);
                Max[bb]=max(Max[bb],dp[aa][bb]);
            }
            for(int k=j+1;k<=i*3+1;k++){
                if(a[j]==a[k]&&v[a[j]].size()){
                    int x=v[a[j]].back();
                    int aa;
                    for(int b=i*3-1;b<=i*3+1;b++){
                        if(b!=k&&b!=j)aa=b;
                    }
                    for(int b=0;b<3*i-1;b++){
                        if(b==x)continue;
                        int xx=x,yy=b;
                        int res;
                        if(xx>yy){
                            dp[b][aa]=max(dp[b][aa],dp[yy][xx]+1);//query(yy,xx)+1;
                        }
                        else{
                            dp[b][aa]=max(dp[b][aa],dp[xx][yy]+1);
                        }
                        ans=max(ans,dp[b][aa]);
                    //    printf("?%d %d %d\n",b,aa,dp[b][aa]);
                        Max[b]=max(Max[b],dp[b][aa]);
                        Max[aa]=max(Max[aa],dp[b][aa]);
                    } 
                }  
            }
        }
       // printf("%d\n",ans);
        v[a[i*3-1]].pb(i*3-1);
        v[a[i*3]].pb(i*3);
        v[a[i*3+1]].pb(i*3+1);
    }
  /*  for(int i = 0;i<3*n-1;i++){
        for(int j = 0;j<3*n-1;j++){
            printf("%d ",dp[i][j]);
        }
        printf("\n");
    }
    printf("\n");*/
    if(v[a[3*n-1]].size()>=2){
        int x=v[a[3*n-1]].back(),y=v[a[3*n-1]][v[a[3*n-1]].size()-2];
      //  printf("%d %d\n",y,x);
        int res=dp[y][x]+1;
       // update(i,i,res); 
        ans=max(ans,res);
    }
    printf("%d\n",ans+add);
}
int main()
{
    int t=1;
    //scanf("%d",&t);
    int T=1;
    while(t--){
        solve();
    }
}
### Prompt

Construct a Cpp code solution to the problem outlined:

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.

Examples

Input

2
1 2 1 2 2 1


Output

2


Input

3
1 1 2 2 3 3 3 2 1


Output

1


Input

3
1 1 2 2 2 3 3 3 1


Output

3

### Response

```cpp
#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define LL long long
#define pii pair<int,int>
#define MEM(x) memset(x,0,sizeof(x))
using namespace std;
vector<int> v[2005];
int a[6005];
int dp[6005][6005];
int Max[6005];
void solve(){
    int n;
    scanf("%d",&n);
    for(int i = 0;i<3*n;i++){
        scanf("%d",&a[i]);
    }
    v[a[0]].pb(0);
    v[a[1]].pb(1);
    int ans=0,add=0;
    for(int i = 1;i<n;i++){
        //a[i*3-1],a[i*3],a[i*3+1];
        if(a[i*3-1]==a[i*3]&&a[i*3]==a[i*3+1]){
            add++;
            continue;
        }
        for(int j =i*3-1;j<=i*3+1;j++){
            for(int k=j+1;k<=i*3+1;k++){
                dp[j][k]=max(dp[j][k],ans);
                Max[k]=max(Max[k],dp[j][k]);
                Max[j]=max(Max[j],dp[j][k]);
            }
        }
        for(int j = 0;j<i*3-1;j++){
            for(int k=i*3-1;k<=i*3+1;k++){
                dp[j][k]=max(dp[j][k],Max[j]);
                Max[j]=max(Max[j],dp[j][k]);
                Max[k]=max(Max[k],dp[j][k]);
            }
        }
        for(int j=i*3-1;j<=i*3+1;j++){
            if(v[a[j]].size()>=2){
                int x=v[a[j]].back(),y=v[a[j]][v[a[j]].size()-2];
                int aa,bb;
                if(j!=i*3-1)aa=i*3-1;
                else aa=i*3;
                if(j==i*3+1)bb=i*3;
                else bb=i*3+1;
                dp[aa][bb]=max(dp[aa][bb],dp[y][x]+1);
             //   printf("!%d %d %d\n",aa,bb,dp[aa][bb]);
                ans=max(ans,dp[aa][bb]);
                Max[aa]=max(Max[aa],dp[aa][bb]);
                Max[bb]=max(Max[bb],dp[aa][bb]);
            }
            for(int k=j+1;k<=i*3+1;k++){
                if(a[j]==a[k]&&v[a[j]].size()){
                    int x=v[a[j]].back();
                    int aa;
                    for(int b=i*3-1;b<=i*3+1;b++){
                        if(b!=k&&b!=j)aa=b;
                    }
                    for(int b=0;b<3*i-1;b++){
                        if(b==x)continue;
                        int xx=x,yy=b;
                        int res;
                        if(xx>yy){
                            dp[b][aa]=max(dp[b][aa],dp[yy][xx]+1);//query(yy,xx)+1;
                        }
                        else{
                            dp[b][aa]=max(dp[b][aa],dp[xx][yy]+1);
                        }
                        ans=max(ans,dp[b][aa]);
                    //    printf("?%d %d %d\n",b,aa,dp[b][aa]);
                        Max[b]=max(Max[b],dp[b][aa]);
                        Max[aa]=max(Max[aa],dp[b][aa]);
                    } 
                }  
            }
        }
       // printf("%d\n",ans);
        v[a[i*3-1]].pb(i*3-1);
        v[a[i*3]].pb(i*3);
        v[a[i*3+1]].pb(i*3+1);
    }
  /*  for(int i = 0;i<3*n-1;i++){
        for(int j = 0;j<3*n-1;j++){
            printf("%d ",dp[i][j]);
        }
        printf("\n");
    }
    printf("\n");*/
    if(v[a[3*n-1]].size()>=2){
        int x=v[a[3*n-1]].back(),y=v[a[3*n-1]][v[a[3*n-1]].size()-2];
      //  printf("%d %d\n",y,x);
        int res=dp[y][x]+1;
       // update(i,i,res); 
        ans=max(ans,res);
    }
    printf("%d\n",ans+add);
}
int main()
{
    int t=1;
    //scanf("%d",&t);
    int T=1;
    while(t--){
        solve();
    }
}
```