cswork / OAOpen practice workspace

IBMOA #67

Rover Move

Estimated EasyJudge-ready

Topics (AI-labelled)Simulation

The rover starts at the top-left corner of an n×n grid and moves one cell at a time according to UP, DOWN, LEFT and RIGHT. A command that would take it outside the grid is ignored, and the following commands still run. Return the final cell number row*n+column, with rows and columns numbered from 0.

Input

The first line contains n m, followed by m commands. Full original bounds: 2≤n≤20, 1≤m≤20; commands are only UP, DOWN, LEFT and RIGHT.

Output

Output one integer, the answer.

Example 1

Input:

4 6
RIGHT
UP
DOWN
LEFT
DOWN
DOWN

Output:

12

Example 2

Input:

2 4
LEFT
UP
RIGHT
RIGHT

Output:

1

Example 3

Input:

3 4
DOWN
DOWN
DOWN
RIGHT

Output:

7

Example notes

The outputs of the three public examples are, in order: 12; 1; 7. These answers were verified by independent enumeration or direct simulation.

Editorial

Loading solution…