Cows like to build shapes out of hay bales. Each new shape is constructed from N (1 <= N <= 25,000) 1 x 1 x 1 cubic bales.
Bale 1 lies on the floor. After placing it, each successive bale from 2 through N is attached to the shape by placing the new bale in the proper relative position to one of the bales already placed. For instance, imagine the following figure:
Bale 1 - on the floor
Bale 2 - RIGHT Bale 1 (to Bale 1's right)
Bale 3 - FRONT Bale 2 (in front of Bale 2)
Bale 4 - FRONT Bale 3
+--+--+
1-> / / /| <-2
+--+--+ +
| / /|/
++--+ +
/ /|/ <-3
+--+ +
| |/ <-4
+--+
Bale 5 - OVER Bale 1
+--+
/ /|
+--+ +--+
5-> | |/ /| <-2
+--+--+ +
1-> | / /|/
++--+ +
/ /|/ <-3
+--+ +
| |/ <-4
+--+
Bale 6 - BACK Bale 4
* Line 1: A single integer, N
* Lines 2..N: These lines describe the placement of bales 2..N in the form "j X". Line 2 describes bale 2; line 3 describes bale 3; and so on. The letter "X" is one of: "L" (left), "R" (right), "F" (front), "B" (back), "O" (over), or "U" (under). The bales can be glued and require no underlying support if extended over an empty space.
* Line 1: The exposed surface area of the resulting solid. If the solid is invalid, output "-1".
5 1 R 2 F 3 F 1 O
18