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<iostream>
#include<algorithm>
using namespace std;
int N;
int dp[2000][2000],X[2000],Y[2000],nX[2000],nY[2000];
int A[3];
int tmp[2000];
int main()
{
	cin>>N;
	for(int i=0;i<N;i++)for(int j=0;j<N;j++)dp[i][j]=-1e9;
	for(int i=0;i<N;i++)X[i]=Y[i]=-1e9;
	cin>>A[0]>>A[1];
	A[0]--,A[1]--;
	dp[A[0]][A[1]]=0;
	X[A[0]]=0;
	Y[A[1]]=0;
	int ans=0;
	for(int i=1;i<N;i++)
	{
		int T[3];
		for(int j=0;j<3;j++)
		{
			cin>>A[j];
			A[j]--;
			T[j]=dp[A[j]][A[j]];
		}
		if(A[0]==A[1]&&A[1]==A[2])
		{
			ans++;
			continue;
		}
		int MX=0;
		for(int j=0;j<N;j++)
		{
			nX[j]=X[j];
			nY[j]=Y[j];
			if(MX<X[j])MX=X[j];
			if(MX<Y[j])MX=Y[j];
		}
		for(int j=0;j<3;j++)
		{
			int a=A[j],b=A[(j+1)%3],c=A[(j+2)%3];
			if(a==b)
			{
				for(int k=0;k<N;k++)tmp[k]=max(dp[a][k],dp[k][a]);
				for(int k=0;k<N;k++)
				{
					int now=dp[k][c]=max(dp[k][c],tmp[k]+1);
					if(nX[k]<now)nX[k]=now;
					if(nY[c]<now)nY[c]=now;
				}
			}
		}
		for(int j=0;j<3;j++)
		{
			int b=A[(j+1)%3],c=A[(j+2)%3];
			int now=dp[b][c]=max(dp[b][c],T[j]+1);
			if(nX[b]<now)nX[b]=now;
			if(nY[c]<now)nY[c]=now;
		}
		for(int j=0;j<3;j++)for(int k=0;k<3;k++)if(j!=k)
		{
			int a=A[j],b=A[k];
			int now=dp[a][b]=max(dp[a][b],MX);
			if(nX[a]<now)nX[a]=now;
			if(nY[b]<now)nY[b]=now;
		}
		for(int j=0;j<N;j++)
		{
			for(int k=0;k<3;k++)
			{
				int a=A[k];
				int now=dp[j][a]=max(dp[j][a],X[j]);
				if(nX[j]<now)nX[j]=now;
				if(nY[a]<now)nY[a]=now;
				now=dp[a][j]=max(dp[a][j],Y[j]);
				if(nX[a]<now)nX[a]=now;
				if(nY[j]<now)nY[j]=now;
			}
			X[j]=nX[j];
			Y[j]=nY[j];
		}
	}
	int x;cin>>x;x--;
	int MX=0;
	for(int i=0;i<N;i++)for(int j=0;j<N;j++)
	{
		MX=max(MX,dp[i][j]+(i==j&&j==x));
	}
	cout<<MX+ans<<endl;
}