cho.sh
Notes
Loading...

Robot Commands

Time limit

2s

Memory limit

128 MB

Problem

An old exploration robot understands four commands:

  1. Forward: move one cell forward.
  2. Turn Left: rotate 90 degrees to the left.
  3. Turn Right: rotate 90 degrees to the right.
  4. Scan: inspect the cell directly in front of the robot.

The robot works on a square grid, so every rotation is exactly 90 degrees. Turning around therefore requires two rotation commands.

A new robot supports only the following two command forms:

  1. Move [direction] [N]: rotate toward [direction], then move N cells in that direction. [direction] is one of Forward, Back, Left, and Right, and N is a positive integer. After moving, the robot keeps facing the direction in which it moved.
  2. Scan [direction]: rotate toward [direction], then inspect the adjacent cell in that direction. [direction] is one of Forward, Back, Left, and Right. After scanning, the robot keeps facing the direction it scanned.

Given the command sequence executed by the old robot, find the minimum number of commands needed for the new robot to inspect the same cells in the same order.

The old and new robots start at the same position and face the same direction. The new robot's intermediate path may differ from the old robot's path; only the inspected cells and their order must match. If the old command sequence inspects the same cell multiple times, the new command sequence must also inspect that same cell the same number of times. The robot may pass through cells that will be inspected later.

Input

The first line contains the number of commands N performed by the old robot (1 <= N <= 100,000).

Each of the next N lines contains one command in the old robot's format.

Output

Print the minimum number of commands required by the new robot.