Sickly Yunho

Given a string of B, L, D doses, remove from either end in the fixed order B, L, D, B, L, D... and find the most doses removable before the required dose is missing from both ends.

Medium6Dynamic programmingArrayTwo pointersGreedyInterviewNo attempts yetTime limit1sMemory limit512 MB

Problem

Yunho is in poor health, so he takes medicine three times a day: morning, noon, and night. The medicine he picked up covers NN days, so the strip holds 3N3N doses.

A strip of dose packets

Yunho is a perfectionist, so he never pulls a dose out of the middle of the row of 3N3N packets. Each time he takes one dose from the front end or the back end of the row. The dose he takes has to be the kind he is due to swallow. He starts with the morning dose of day 1, then noon, then night, then the morning dose of day 2, and so on. If neither end holds the dose he is due to take, Yunho stops there.

Find the largest number of doses Yunho can swallow.

Input

The program reads from standard input.

The first line holds NN, the number of days the medicine covers. (1N5001 \le N \le 500)

The second line holds the kinds of all 3N3N doses in the order they lie on the strip, with no spaces. A morning dose is B, a noon dose is L, and a night dose is D. Each of the three letters appears exactly NN times.

Output

The program writes to standard output.

Print the largest number of doses Yunho can swallow on one line.