This page is still under construction.

Parts of this page are still being built. What you see may change.

RBY Pop!

Time limit1sMemory limit128 MB

Summary
Change exactly one ball's color, then repeatedly remove any run of 4 or more equal adjacent balls; minimize the number left.
Level

Medium5 of 10

Topics
Simulation, Implementation, Stack, Brute force
Solved
No attempts yet

Problem

There are NN balls stuck together in a single vertical column. Each ball is one of three colors: R (red), B (blue), or Y (yellow).

The player may pick one ball and change its color to a different color. After the change, whenever 44 or more balls of the same color are vertically adjacent, that whole consecutive group goes pop! and disappears at once. Where balls disappear, the remaining balls above and below join together into one vertical column again; if this joining once more produces 44 or more consecutive balls of the same color, those balls also pop! in a chain. This chain repeats until no color has 44 or more balls in a row.

Your goal is to change exactly one ball's color so that, after all the chained pops finish, the number of balls that remain (do not disappear) is as small as possible.

For example, in the left state of the figure below, changing the 66th ball from the top from yellow to blue makes 55 blue balls consecutive, so they pop! Then 44 red balls pop! in a chain, leaving only 33 balls in the end.

Given the colors of the NN balls in the initial state, find the minimum number MM of balls left after the chained pops when you change the color of exactly one ball.

It is guaranteed that in the initial state no 44 or more balls of the same color are consecutive.

Input

The first line contains the number of balls NN (1≤N≤100001 \le N \le 10000).

Each of the next NN lines contains the color of one ball, given from top to bottom. Each color is 11, 22, or 33, where 11 is red, 22 is blue, and 33 is yellow.

Output

Print the minimum number MM of balls that remain without disappearing.

Examples2

  1. Example 1

    Input
    12
    3
    2
    1
    1
    2
    3
    2
    2
    2
    1
    1
    3
    
    Expected output
    3
    
  2. Example 2

    Input
    12
    3
    2
    1
    1
    2
    3
    2
    1
    3
    2
    1
    3
    
    Expected output
    12