ABC Street

Starting from block 1, hop forward across A, B, C blocks in repeating order to reach block N while minimizing the sum of squared jump lengths.

Medium4Dynamic programmingInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

ABC Street is a road made of NN paving blocks in a row. The blocks are numbered 1 to NN.

Junseo lives on block 1 and Hayun lives on block NN. Junseo jumps from block to block to meet Hayun.

Every block has one letter on it, either A, B, or C. The letter on block 1 is always A.

Junseo moves only by jumping, and always toward larger numbers. If he stands on block ii, he can jump to any block from i+1i+1 to NN. One jump of kk blocks costs k2k^2 energy.

Junseo chants A, B, C in that order as he goes. So the letters on the blocks he steps on, starting from the first one, must read A, B, C, A, B, C, and so on.

Write a program that computes the smallest amount of energy Junseo needs to meet Hayun.

Input

The first line contains the number of paving blocks NN. (1N10001 \le N \le 1000)

The second line contains the letters written on blocks 1 to NN as one string of length NN. Each character is A, B, or C, and the first character is always A.

Output

Print the smallest amount of energy Junseo needs to meet Hayun. If he cannot reach Hayun, print -1.

If NN is 1, the two of them stand on the same block, so print 0.