Under construction

Still being built. Things may change

Robot

Interview

Time limit1sMemory limit512 MB

Summary
Follow TURN and MOVE commands from the origin facing east, print the final coordinates, or -1 if any move leaves the square.
Level

Easy3 of 10

Topics
Simulation, Implementation, Math
Solved
No attempts yet

Problem

A robot reads commands and moves inside a square region S, always parallel to the x axis or the y axis. The lower left corner of S is (0, 0) and the upper right corner is (M, M). The robot starts at (0, 0) facing east.

Each command consists of an action and one value that belongs to it. There are two actions, TURN and MOVE. TURN 0 rotates the robot 90 degrees to the left in place, and TURN 1 rotates it 90 degrees to the right in place. MOVE d moves the robot a distance of d in the direction it faces, where d is positive.

A command is valid if the robot is on the boundary of S or inside S after the command is carried out. It is not valid if the robot ends up outside S. A sequence of commands is valid when every command in it is valid.

For example, with M=11M = 11 and the command sequence MOVE 6, TURN 0, MOVE 5, TURN 0, MOVE 2, TURN 0, MOVE 2, TURN 0, MOVE 4, TURN 0, MOVE 3, MOVE 2, the robot stops at (8, 8).

Given a square with side length M and n commands, print the final position of the robot after it follows the commands in order, starting at (0, 0) facing east.

Input

The first line contains two integers M and n (1≤M≤10001 \le M \le 1000, 1≤n≤10001 \le n \le 1000). M is the side length of the square S, so the upper right corner is (M, M). n is the number of commands the robot carries out.

Each of the next n lines contains one command, either TURN dir or MOVE d. Here dir is 0 or 1, and d is a positive integer at most 1000. Note that the robot starts at (0, 0) facing east.

Output

Print exactly one line. If the command sequence is valid, print the x coordinate and the y coordinate of the robot after all commands are carried out, as two non-negative integers separated by a single space. If the command sequence is not valid, print -1.

Examples3

  1. Example 1

    Input
    11 14
    MOVE 10
    TURN 0
    MOVE 2
    TURN 0
    MOVE 5
    TURN 1
    MOVE 5
    TURN 1
    MOVE 2
    TURN 1
    MOVE 3
    TURN 0
    TURN 0
    MOVE 6
    
    Expected output
    7 10
    
  2. Example 2

    Input
    11 7
    MOVE 5
    TURN 0
    MOVE 4
    TURN 1
    MOVE 2
    TURN 1
    MOVE 5
    
    Expected output
    -1
    
  3. Example 3

    Input
    2 7
    MOVE 2
    TURN 0
    MOVE 3
    TURN 0
    MOVE 2
    TURN 0
    MOVE 2
    
    Expected output
    -1