#Lutece0190. Sorting a Three-Valued Sequence

Sorting a Three-Valued Sequence

Migrated from Lutece 190 Sorting a Three-Valued Sequence

All parts of this problem, including description, images, samples, data and checker, might be broken. If you find bugs in this problem, please contact the admins.

Description

Sorting is one of the most frequently performed computational tasks. Consider the special sorting problem in which the records to be sorted have at most three different key values. This happens for instance when we sort medalists of a competition according to medal value, that is, gold medalists come first, followed by silver, and bronze medalists come last.

In this task the possible key values are the integers 11, 22 and 33. The required sorting order is non-decreasing. However, sorting has to be accomplished by a sequence of exchange operations. An exchange operation, defined by two position numbers pp and qq, exchanges the elements in positions pp and qq.

You are given a sequence of key values. Write a program that computes the minimal number of exchange operations that are necessary to make the sequence sorted.

Input

Line 11: NN (1N10001\leq N\leq 1000), the number of records to be sorted

Lines 2N+12-N+1: A single integer from the set {1,2,3}\{1, 2, 3\}

Output

A single line containing the number of exchanges required

Samples

9
2
2
1
3
3
3
2
3
1
4

Resources

USACO TRAINING selected by rectaflex