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 Fortran (GNU Fortran 9.2.1) (judge verdict: Accepted)

module mod_selection_sort
  implicit none
  integer, private, parameter :: intkind = 4
contains
  logical function less(a, b) result(res)
    integer(intkind), intent(in) :: a, b
    res = a <= b
  end
  subroutine swap(a, b)
    integer(intkind), intent(inout) :: a, b
    a = xor(a, b)
    b = xor(a, b)
    a = xor(a, b)
  end
  subroutine selection_sort(a)
    integer(intkind), intent(inout) :: a(:)
    integer :: n, i, j, m
    n = size(a)
    do i = 1, n - 1
      m = i
      do j = i + 1, n
        if (.not.less(a(m), a(j))) m = j
      end do
      if (i /= m) call swap(a(i), a(m))
    end do
  end
end module mod_selection_sort
program brave_chain
  use mod_selection_sort
  implicit none
  integer, parameter :: inf = 1e9
  integer :: n, a(6000) = 0, dp(2000, 2000) = -inf, m = 0, b(3) = 0, v, i, j
  integer, dimension(2000) :: dp0 = 0, mv = -inf, mv0 = 0
  read(*,*) n
  read(*,*) a(1:3 * n)
  call update(a(1), a(2), 0)
  do i = 3, 3 * n - 2, 3
    if (a(i) == a(i + 1) .and. a(i) == a(i + 2)) then
      m = m + 1
      cycle
    end if
    call selection_sort(a(i:i + 2))
    if (a(i + 1) == a(i + 2)) call swap(a(i), a(i + 2))
    do j = 1, n
      dp0(j) = dp(min(a(i), j), max(a(i), j))
    end do
    mv0(1:n) = mv(1:n)
    v = max(0, maxval(mv(1:n)))
    do j = 1, 3
      b(j) = dp(a(i + j - 1), a(i + j - 1))
    end do
    if (a(i) == a(i + 1)) then
      do j = 1, n
        call update(a(i + 2), j, dp0(j) + 1)
      end do
    end if
    call update(a(i + 1), a(i + 2), b(1) + 1)
    call update(a(i), a(i + 2), b(2) + 1)
    call update(a(i), a(i + 1), b(3) + 1)
    call update(a(i + 1), a(i + 2), v)
    call update(a(i), a(i + 2), v)
    call update(a(i), a(i + 1), v)
    do j = 1, n
      call update(a(i), j, mv0(j))
      call update(a(i + 1), j, mv0(j))
      call update(a(i + 2), j, mv0(j))
    end do
  end do
  i = a(3 * n)
  write(*,'(i0)') max(dp(i, i) + 1, maxval(mv(1:n))) + m
contains
  subroutine update(a, b, z)
    integer, intent(in) :: a, b, z
    integer :: x, y
    if (a > b) then
      x = b
      y = a
    else
      x = a
      y = b
    end if
    if (dp(x, y) >= z) return
    dp(x, y) = z
    mv(x) = max(mv(x), z)
    mv(y) = max(mv(y), z)
  end
end program brave_chain