Problem:
Score : 600 points

Problem Statement

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.

Sample Input 1

2
1 2 1 2 2 1

Sample Output 1

2

Let us rearrange the five leftmost cards so that the integers written on the six cards will be 2\ 2\ 2\ 1\ 1\ 1 from left to right.

Then, remove the three leftmost cards, all of which have the same integer 2, gaining 1 point.

Now, the integers written on the remaining cards are 1\ 1\ 1.

Since these three cards have the same integer 1, we gain 1 more point.

In this way, we can gain 2 points - which is the maximum possible.

Sample Input 2

3
1 1 2 2 3 3 3 2 1

Sample Output 2

1

Sample Input 3

3
1 1 2 2 2 3 3 3 1

Sample Output 3

3

Solution in PyPy3 (7.3.0) (judge verdict: Wrong Answer)

n = int(input())
A = list(map(int,input().split()))

dp = [[-1000]*(n+1) for i in range(n+1)]
low = [-1000]*(n+1)
col = [-1000]*(n+1)
m = 0
dp[A[0]][A[1]] = 0
low[A[0]] = 0
col[A[1]] = 0
base = 0

for _ in range(1,n):
    a,b,c = A[3*_-1:3*_+2]
    if a==b==c:
        base += 1
        continue
    q = []
    if a==b or b==c or c==a:
        if b==c:
            a,b,c = b,c,a
        elif c==a:
            a,b,c = a,c,b
        v = max(m,dp[c][c]+1)
        q.append((3,a,a,v))
        q.append((0,a,v))
        q.append((1,a,v))
        v1 = -2
        for i in range(1,n+1):
            dp[i][c] = max(dp[i][c],low[i],dp[i][a]+1)
            v1 = max(v1,dp[i][c])
            if dp[i][c] > low[i]:
                q.append((0,i,dp[i][c]))
        q.append((1,c,v1))
        v2 = -2
        for i in range(1,n+1):
            dp[c][i] = max(dp[c][i],col[i],dp[a][i]+1)
            v2 = max(v2,dp[c][i])
            if dp[c][i] > col[i]:
                q.append((1,i,dp[c][i]))
        q.append((0,c,v2))
        q.append((2,0,max(v,v1,v2)))
    
    else:
        for j in range(3):
            v = max(m,dp[c][c]+1)
            q.append((3,a,b,v))
            q.append((0,a,v))
            q.append((1,a,v))
            v1 = -2
            va = dp[a][a]+1
            vb = dp[b][b]+1
            for i in range(1,n+1):
                if i == b:
                    x = max(va,low[i],dp[i][c])
                    if x > low[i]:
                        q.append((0,i,x))
                elif i == a:
                    x = max(vb,low[i],dp[i][c])
                    if x > low[i]:
                        q.append((0,i,x))
                else:
                    x = max(va,vb,low[i],dp[i][c])
                    if x > low[i]:
                        q.append((0,i,x))
                v1 = max(v1,x)
                q.append((3,i,c,x))
            q.append((1,c,v1))
            v2 = -2
            for i in range(1,n+1):
                if i == b:
                    x = max(va,col[i],dp[c][i])
                    if x > col[i]:
                        q.append((0,i,x))
                elif i == a:
                    x = max(vb,col[i],dp[c][i])
                    if x > col[i]:
                        q.append((0,i,x))
                else:
                    x = max(va,vb,col[i],dp[c][i])
                    if x > col[i]:
                        q.append((0,i,dp[c][i]))
                v2 = max(v2,x)
                q.append((3,c,i,x))
            q.append((0,c,v2))
            q.append((2,0,max(v,v1,v2)))

            a,b,c = b,c,a
    for l in q:
        if l[0] == 0:
            low[l[1]] = max(low[l[1]],l[2])
        elif l[0] == 1:
            col[l[1]] = max(col[l[1]],l[2])
        elif l[0] == 2:
            m = max(m,l[2])
        else:
            dp[l[1]][l[2]] = max(dp[l[1]][l[2]],l[3])
        
print(max(m,dp[A[-1]][A[-1]]+1)+base)