Buggy python submission (verdict: Time Limit Exceeded):
def upd(a, b, v):
    global N
    dp[a][b] = max(dp[a][b], v)
    dp[b][a] = max(dp[b][a], v)
    dp[N][a] = max(dp[N][a], v)
    dp[N][b] = max(dp[N][b], v)
    dp[a][N] = max(dp[a][N], v)
    dp[b][N] = max(dp[b][N], v)
    dp[N][N] = max(dp[N][N], v)


N = int(input())
A = list(map(lambda x: int(x) - 1, input().split()))

INF = 1 << 32
dp = [[-INF] * (N + 1) for _ in range(N + 1)]
upd(A[0], A[1], 0)
base = 0
q = [None] * (3 * N + 3)

for i in range(2, 3 * N - 1, 3):
    x, y, z = A[i], A[i + 1], A[i + 2]
    if x == y == z:  # x, y, z 3文字すべて等しいとき
        base += 1
    else:
        cur = 0
        for _ in range(3):
            # a, b, x, y, z のうち, b, y, z を消すとき
            for k in range(N):
                v = dp[k][N]
                if y == z:
                    v = max(v, dp[k][y] + 1)
                q[cur] = (k, x, v)
                cur += 1
            # a, b, x, y, z のうち, a, b, z を消すとき
            v = max(dp[z][z] + 1, dp[N][N])
            q[cur] = (x, y, v)
            cur += 1
            # x, y, z をローテート
            x, y, z = z, x, y
        for j in range(cur):
            upd(*q[j])

l = A[-1]
ans = max(dp[N][N], dp[l][l] + 1)
print(ans + base)


Fixed submission:
from collections import deque


def upd(a, b, v):
    global N
    dp[a][b] = max(dp[a][b], v)
    dp[b][a] = max(dp[b][a], v)
    dp[N][a] = max(dp[N][a], v)
    dp[N][b] = max(dp[N][b], v)
    dp[a][N] = max(dp[a][N], v)
    dp[b][N] = max(dp[b][N], v)
    dp[N][N] = max(dp[N][N], v)


N = int(input())
A = list(map(lambda x: int(x) - 1, input().split()))

INF = 1 << 32
dp = [[-INF] * (N + 1) for _ in range(N + 1)]
upd(A[0], A[1], 0)
base = 0

for i in range(2, 3 * N - 1, 3):
    x, y, z = A[i], A[i + 1], A[i + 2]
    if x == y == z:  # x, y, z 3文字すべて等しい
        base += 1
    else:
        q = deque()
        for _ in range(3):
            for k in range(N):
                v = dp[k][N]
                if y == z:
                    v = max(v, dp[k][y] + 1)
                q.append((k, x, v))
            v = max(dp[z][z] + 1, dp[N][N])
            q.append((x, y, v))
            x, y, z = z, x, y
        while q:
            a, b, v = q.popleft()
            upd(a, b, v)

l = A[-1]
ans = max(dp[N][N], dp[l][l] + 1)
print(ans + base)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque


def upd(dp, a, b, v):
    dp[a][b] = max(dp[a][b], v)
    dp[b][a] = max(dp[b][a], v)
    dp[-1][a] = max(dp[-1][a], v)
    dp[-1][b] = max(dp[-1][b], v)
    dp[a][-1] = max(dp[a][-1], v)
    dp[b][-1] = max(dp[b][-1], v)
    dp[-1][-1] = max(dp[-1][-1], v)


def main():
    N = int(input())
    A = list(map(lambda x: int(x) - 1, input().split()))

    INF = 1 << 32
    dp = [[-INF] * (N + 1) for _ in range(N + 1)]
    upd(dp, A[0], A[1], 0)
    base = 0

    for i in range(2, 3 * N - 1, 3):
        x, y, z = A[i], A[i + 1], A[i + 2]
        if x == y == z:  # x, y, z 3文字すべて等しいとき
            base += 1
        else:
            q = deque()
            for _ in range(3):
                # a, b, x, y, z のうち, b, y, z を消すとき
                for k in range(N):
                    v = dp[k][N]
                    if y == z:
                        v = max(v, dp[k][y] + 1)
                    q.append((k, x, v))
                # a, b, x, y, z のうち, a, b, z を消すとき
                v = max(dp[z][z] + 1, dp[N][N])
                q.append((x, y, v))
                # x, y, z をローテート
                x, y, z = z, x, y
            while q:
                a, b, v = q.popleft()
                upd(dp, a, b, v)

    l = A[-1]
    ans = max(dp[N][N], dp[l][l] + 1)
    print(ans + base)


if __name__ == '__main__':
    main()


Fixed submission:
from collections import deque


def upd(a, b, v):
    global N
    dp[a][b] = max(dp[a][b], v)
    dp[b][a] = max(dp[b][a], v)
    dp[N][a] = max(dp[N][a], v)
    dp[N][b] = max(dp[N][b], v)
    dp[a][N] = max(dp[a][N], v)
    dp[b][N] = max(dp[b][N], v)
    dp[N][N] = max(dp[N][N], v)


N = int(input())
A = list(map(lambda x: int(x) - 1, input().split()))

INF = 1 << 32
dp = [[-INF] * (N + 1) for _ in range(N + 1)]
upd(A[0], A[1], 0)
base = 0

for i in range(2, 3 * N - 1, 3):
    x, y, z = A[i], A[i + 1], A[i + 2]
    if x == y == z:  # x, y, z 3文字すべて等しい
        base += 1
    else:
        q = deque()
        for _ in range(3):
            for k in range(N):
                v = dp[k][N]
                if y == z:
                    v = max(v, dp[k][y] + 1)
                q.append((k, x, v))
            v = max(dp[z][z] + 1, dp[N][N])
            q.append((x, y, v))
            x, y, z = z, x, y
        while q:
            a, b, v = q.popleft()
            upd(a, b, v)

l = A[-1]
ans = max(dp[N][N], dp[l][l] + 1)
print(ans + base)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque


def upd(a, b, v):
    dp[a][b] = max(dp[a][b], v)
    dp[b][a] = max(dp[b][a], v)
    dp[-1][a] = max(dp[-1][a], v)
    dp[-1][b] = max(dp[-1][b], v)
    dp[a][-1] = max(dp[a][-1], v)
    dp[b][-1] = max(dp[b][-1], v)
    dp[-1][-1] = max(dp[-1][-1], v)


N = int(input())
A = list(map(lambda x: int(x) - 1, input().split()))

INF = 1 << 32
dp = [[-INF] * (N + 1) for _ in range(N + 1)]
upd(A[0], A[1], 0)
base = 0

for i in range(2, 3 * N - 1, 3):
    x, y, z = A[i], A[i + 1], A[i + 2]
    if x == y == z:  # x, y, z 3文字すべて等しいとき
        base += 1
    else:
        q = deque()
        for _ in range(3):
            # a, b, x, y, z のうち, b, y, z を消すとき
            for k in range(N):
                v = dp[k][N]
                if y == z:
                    v = max(v, dp[k][y] + 1)
                q.append((k, x, v))
            # a, b, x, y, z のうち, a, b, z を消すとき
            v = max(dp[z][z] + 1, dp[N][N])
            q.append((x, y, v))
            # x, y, z をローテート
            x, y, z = z, x, y
        while q:
            a, b, v = q.popleft()
            upd(a, b, v)

l = A[-1]
ans = max(dp[N][N], dp[l][l] + 1)
print(ans + base)


Fixed submission:
from collections import deque


def upd(a, b, v):
    global N
    dp[a][b] = max(dp[a][b], v)
    dp[b][a] = max(dp[b][a], v)
    dp[N][a] = max(dp[N][a], v)
    dp[N][b] = max(dp[N][b], v)
    dp[a][N] = max(dp[a][N], v)
    dp[b][N] = max(dp[b][N], v)
    dp[N][N] = max(dp[N][N], v)


N = int(input())
A = list(map(lambda x: int(x) - 1, input().split()))

INF = 1 << 32
dp = [[-INF] * (N + 1) for _ in range(N + 1)]
upd(A[0], A[1], 0)
base = 0

for i in range(2, 3 * N - 1, 3):
    x, y, z = A[i], A[i + 1], A[i + 2]
    if x == y == z:  # x, y, z 3文字すべて等しい
        base += 1
    else:
        q = deque()
        for _ in range(3):
            for k in range(N):
                v = dp[k][N]
                if y == z:
                    v = max(v, dp[k][y] + 1)
                q.append((k, x, v))
            v = max(dp[z][z] + 1, dp[N][N])
            q.append((x, y, v))
            x, y, z = z, x, y
        while q:
            a, b, v = q.popleft()
            upd(a, b, v)

l = A[-1]
ans = max(dp[N][N], dp[l][l] + 1)
print(ans + base)