Problem: 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

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
// #define int long long

const int N=2005,inf=1e9;
int dp[N][N],a[3*N],mx[N],n,cnt=0,ans=0;

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    cin >> n ;
    for(int i=0;i<3*n;i++)
        cin >> a[i];
    fill(*dp,*dp+N*N,-inf);
    dp[a[0]][a[1]]=0,dp[a[1]][a[0]]=0;
    mx[a[0]]=a[1],mx[a[1]]=a[0];
    for(int i=1;i<n;i++){
        int v[]={a[3*i-1],a[3*i],a[3*i+1]};
        vector <tuple <int,int,int> > vec;
        auto udt=[&](int x,int y,int z){
        	vec.push_back({x,y,z});
        };
        sort(v,v+3);
        if(v[0]==v[2]){
            cnt++;continue;
        }
        for(int j=0;j<3;j++){
            for(int k=0;k<j;k++)udt(v[j],v[k],ans);
            for(int k=1;k<=n;k++)udt(k,v[j],dp[k][mx[k]]);
        }
        if(v[0]==v[1]){
            int x=v[0],y=v[2];
            for(int k=1;k<=n;k++){
            	udt(y,k,dp[x][k]+1);
            }
        }
        if(v[1]==v[2]){
            int x=v[1],y=v[0];
            for(int k=1;k<=n;k++){
            	udt(y,k,dp[x][k]+1);
            }
        }
        for(int j=0;j<3;j++){
            int x=v[j],y=v[(j+1)%3],z=v[(j+2)%3];
            udt(y,z,dp[x][x]+1);
        }
        for(auto [x,y,z]:vec){
        	dp[x][y]=max(dp[x][y],z),dp[y][x]=dp[x][y];
            ans=max(ans,z);
            if(dp[x][y]>dp[x][mx[x]])mx[x]=y;
            if(dp[x][y]>dp[y][mx[y]])mx[y]=x;
        }
    }
    ans=max(ans,dp[a[3*n-1]][a[3*n-1]]+1);
    cout << ans+cnt;
}