cho.sh
Notes
Loading...

Arranging Shapes

Time limit

1s

Memory limit

128 MB

Problem

Triangles, squares, and circles are arranged in a line. In one operation, choose any two positions and swap the shapes at those positions. The goal is to rearrange the line so that each kind of shape occupies one contiguous block. The order of the three blocks does not matter.

Given the current order of the shapes, write a program that finds the minimum number of swaps needed to make equal shapes contiguous.

Input

The first line contains the total number of shapes NNN. NNN is between 333 and 100,000100,000100,000, inclusive.

The second line contains NNN integers separated by spaces, representing the shapes in order. Integer 111 represents a triangle, integer 222 represents a square, and integer 333 represents a circle.

Each of the three kinds of shape appears at least once.

Output

Print the minimum number of swaps required to make equal shapes contiguous.