#include<bits/stdc++.h>
using namespace std;
const int N=2e3+5;
int a[N*3],f[N][N],ma[N],t,mx;
struct Node{int x,y,z;}op[N<<2];
int main(){
	int n,n1,ans1=0;scanf("%d",&n),n1=3*n-1;
	for(int i=0;i<=n1;i++)scanf("%d",a+i);
	memset(f,0x8f,sizeof(f)),memset(ma,0x8f,sizeof(ma));
	ma[a[0]]=ma[a[1]]=f[a[0]][a[1]]=f[a[1]][a[0]]=0;
	for(int i=2;i<n1;i+=3){
		if(a[i]==a[i+1]&&a[i]==a[i+2]){ans1++;continue;}
		if(a[i]==a[i+2])swap(a[i+1],a[i+2]);
		if(a[i+1]==a[i+2])swap(a[i+2],a[i]);
		if(a[i]==a[i+1]){
			for(int j=1;j<=n;j++)op[t++]={a[i+2],j,f[j][a[i]]+1},op[t++]={j,a[i],ma[j]},op[t++]={j,a[i+2],ma[j]};
			op[t++]={a[i],a[i],max(f[a[i+2]][a[i+2]]+1,mx)};
		}
		else{
			for(int j=1;j<=n;j++)op[t++]={j,a[i],ma[j]},op[t++]={j,a[i+1],ma[j]},op[t++]={j,a[i+2],ma[j]};
			op[t++]={a[i],a[i+1],max(f[a[i+2]][a[i+2]]+1,mx)};
			op[t++]={a[i],a[i+2],max(f[a[i+1]][a[i+1]]+1,mx)};
			op[t++]={a[i+2],a[i+1],max(f[a[i]][a[i]]+1,mx)};
		}
		while(t--){
			int x=op[t].x,y=op[t].y,z=op[t].z;
			f[x][y]=f[y][x]=max(z,f[x][y]);
			ma[x]=max(ma[x],z),ma[y]=max(ma[y],z),mx=max(mx,z);
		}t++;
	}
	printf("%d",max(mx,f[a[n1]][a[n1]]+1)+ans1);
	return 0;
}
### 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>
using namespace std;
const int N=2e3+5;
int a[N*3],f[N][N],ma[N],t,mx;
struct Node{int x,y,z;}op[N<<2];
int main(){
	int n,n1,ans1=0;scanf("%d",&n),n1=3*n-1;
	for(int i=0;i<=n1;i++)scanf("%d",a+i);
	memset(f,0x8f,sizeof(f)),memset(ma,0x8f,sizeof(ma));
	ma[a[0]]=ma[a[1]]=f[a[0]][a[1]]=f[a[1]][a[0]]=0;
	for(int i=2;i<n1;i+=3){
		if(a[i]==a[i+1]&&a[i]==a[i+2]){ans1++;continue;}
		if(a[i]==a[i+2])swap(a[i+1],a[i+2]);
		if(a[i+1]==a[i+2])swap(a[i+2],a[i]);
		if(a[i]==a[i+1]){
			for(int j=1;j<=n;j++)op[t++]={a[i+2],j,f[j][a[i]]+1},op[t++]={j,a[i],ma[j]},op[t++]={j,a[i+2],ma[j]};
			op[t++]={a[i],a[i],max(f[a[i+2]][a[i+2]]+1,mx)};
		}
		else{
			for(int j=1;j<=n;j++)op[t++]={j,a[i],ma[j]},op[t++]={j,a[i+1],ma[j]},op[t++]={j,a[i+2],ma[j]};
			op[t++]={a[i],a[i+1],max(f[a[i+2]][a[i+2]]+1,mx)};
			op[t++]={a[i],a[i+2],max(f[a[i+1]][a[i+1]]+1,mx)};
			op[t++]={a[i+2],a[i+1],max(f[a[i]][a[i]]+1,mx)};
		}
		while(t--){
			int x=op[t].x,y=op[t].y,z=op[t].z;
			f[x][y]=f[y][x]=max(z,f[x][y]);
			ma[x]=max(ma[x],z),ma[y]=max(ma[y],z),mx=max(mx,z);
		}t++;
	}
	printf("%d",max(mx,f[a[n1]][a[n1]]+1)+ans1);
	return 0;
}
```