Hop — Don't Walk!

No attempts yetTime limit1sMemory limit128 MB

Problem

This is a classic tile-sliding puzzle. An animated frog can walk and hop, both forward and backward. The frog stands in the single empty space within an otherwise contiguous line of tiles. Each tile is painted black on one side and white on the other.

A situation is written as a string. F marks the empty space where the frog stands, B is a tile showing its black face, and W is a tile showing its white face. The forward direction is from left to right. For example, BWFBBW means the frog has two tiles behind it and three tiles in front of it.

Walk. The frog can walk over an adjacent tile. Walking forward, the frog and the tile immediately in front of it swap places (the tile slides into the space the frog just left) and the tile keeps its face. Walking backward is symmetric. For example, walking forward from BWFBBW gives BWBFBW.

Hop. The frog can hop over an adjacent tile and land on the tile beyond it. That landing tile slides back into the space the frog just left and flips to its other face; the tile that was hopped over does not move. For example, hopping backward from BWFBBW gives FWWBBW.

A walk and a hop each count as one move. The objective — which state to reach and how the answer is defined — is given in the Output section below.

Input

The input contains one or more test cases, one per line. Each line is a non-empty string S, at most 100 characters long, made up of the letters B, W, and exactly one F, describing the initial tile arrangement. The input ends with a line that contains one or more - (minus) characters.

Output

For each test case, print one line:

k. M

where k is the test case number (starting at 1) and M is the minimum number of moves needed to reach an arrangement that has no white tile between any two black tiles — equivalently, one in which all black tiles form a single contiguous block. The frog (the empty space) may end up anywhere and is ignored when checking this condition. If such an arrangement cannot be reached in fewer than 10 moves, print M as -1.