#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;
using T = tuple<int,int,int>;

int a[6010];
int dp[2010][2010];
int maxa[2010]; //要素にiを持つ場合の最大値

int const inf = 1e9;

int main(){
  int n;
  cin >> n;
  int n3 = n*3;
  
  rep(i,n3) cin >> a[i],--a[i];
  rep(i,n)rep(j,n) dp[i][j] = -inf;
  rep(i,n) maxa[i] = -inf;
  

  dp[a[0]][a[1]] = 0;
  dp[a[1]][a[0]] = 0;
  maxa[a[0]] = maxa[a[1]] = 0;
  int maxab = 0; //その時点での最大値
  int ex = 0;
    
  for(int i=2; i < n3-2; i+=3){
    int x = a[i];
    int y = a[i+1];
    int z = a[i+2];
    
    //新しい3枚が一致していたとき
    if(x == y && y == z){ 
      ++ex;
      continue;
    }
    
    vector<T> upd;

    rep(j,3){
      //古い2枚と新しい1枚
      //古いのを2枚捨てるからその時点の最大値maxabと比較。
      int m = max(maxab, dp[z][z]+1);
      upd.emplace_back(x,y,m);
      
      //古い1枚と新しい2枚
      //古いものを1枚捨てる
      rep(k,n){
        int m = maxa[k];
        if(x == y) m = max(m, dp[x][k]+1);
        upd.emplace_back(z,k,m);
      }
      swap(x,y); swap(y,z);
      
    }

    for(auto t : upd){
      int na, nb, s;
      tie(na, nb, s) = t;
      
      dp[na][nb] = max(dp[na][nb], s);
      dp[nb][na] = max(dp[nb][na], s);
      maxa[na] = max(maxa[na], s);
      maxa[nb] = max(maxa[nb], s);

      maxab = max(maxab, s);
    }   
  }
  
  int ans = maxab;
  int al = a[n3-1];
  ans = max(ans, dp[al][al]+1);
  ans += ex;
  cout << ans << endl;

  return 0;
}
### Prompt

Generate a CPP solution to the following 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

### Response

```cpp
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;
using T = tuple<int,int,int>;

int a[6010];
int dp[2010][2010];
int maxa[2010]; //要素にiを持つ場合の最大値

int const inf = 1e9;

int main(){
  int n;
  cin >> n;
  int n3 = n*3;
  
  rep(i,n3) cin >> a[i],--a[i];
  rep(i,n)rep(j,n) dp[i][j] = -inf;
  rep(i,n) maxa[i] = -inf;
  

  dp[a[0]][a[1]] = 0;
  dp[a[1]][a[0]] = 0;
  maxa[a[0]] = maxa[a[1]] = 0;
  int maxab = 0; //その時点での最大値
  int ex = 0;
    
  for(int i=2; i < n3-2; i+=3){
    int x = a[i];
    int y = a[i+1];
    int z = a[i+2];
    
    //新しい3枚が一致していたとき
    if(x == y && y == z){ 
      ++ex;
      continue;
    }
    
    vector<T> upd;

    rep(j,3){
      //古い2枚と新しい1枚
      //古いのを2枚捨てるからその時点の最大値maxabと比較。
      int m = max(maxab, dp[z][z]+1);
      upd.emplace_back(x,y,m);
      
      //古い1枚と新しい2枚
      //古いものを1枚捨てる
      rep(k,n){
        int m = maxa[k];
        if(x == y) m = max(m, dp[x][k]+1);
        upd.emplace_back(z,k,m);
      }
      swap(x,y); swap(y,z);
      
    }

    for(auto t : upd){
      int na, nb, s;
      tie(na, nb, s) = t;
      
      dp[na][nb] = max(dp[na][nb], s);
      dp[nb][na] = max(dp[nb][na], s);
      maxa[na] = max(maxa[na], s);
      maxa[nb] = max(maxa[nb], s);

      maxab = max(maxab, s);
    }   
  }
  
  int ans = maxab;
  int al = a[n3-1];
  ans = max(ans, dp[al][al]+1);
  ans += ex;
  cout << ans << endl;

  return 0;
}
```