#P6116. Joy Grass Rearrangement

    ID: 19338 Type: Default 1000ms 256MiB

Joy Grass Rearrangement

Joy Grass Rearrangement

Mr. JOI, the household garden expert, grows a plant called Joy Grass in his garden. In his garden, there are N flower pots arranged from east to west, numbered 1 through N. Each pot contains one Joy Grass which will sprout leaves that can be red, green, or yellow. Mr. JOI observed that if two Joy Grass of the same color are placed adjacent to each other, their growth slows down. Therefore, he wishes to rearrange the pots so that no two adjacent plants share the same leaf color.

However, the flower pots are very heavy, so in one operation he can only swap two adjacent pots. Formally, in one move, he may choose an index i (1 ≤ i < N) and swap the pots numbered i and i+1.

Your task is to compute the minimum number of adjacent swaps required to achieve a rearrangement with no two consecutive pots having the same color.

Input format details: The garden is represented by a string consisting only of the characters 'R', 'G', and 'Y', corresponding to red, green, and yellow leaves respectively.

inputFormat

The input consists of two lines:

  1. An integer N (1 ≤ N ≤ 100) representing the number of flower pots.
  2. A string of length N composed only of the characters 'R', 'G', and 'Y', indicating the color of the Joy Grass in each pot from east to west.

outputFormat

Output a single integer – the minimum number of adjacent swaps required to rearrange the pots so that no two adjacent Joy Grass have the same color. If no such rearrangement is possible, output -1.

sample

3
RRG
1