Snake

No attempts yetTime limit1sMemory limit256 MB

Problem

A square board has width and height both equal to 2L+12L+1. Every cell is written as a coordinate pair (x,y)(x, y). The center cell is (0,0)(0, 0), the bottom left cell is (L,L)(-L, -L), and the top right cell is (L,L)(L, L). The xx coordinate grows from left to right and the yy coordinate grows from bottom to top.

One snake sits on cell (0,0)(0, 0) of this board. At the start the snake is the size of a single cell and its head faces right. Every second the snake extends its body by one cell in the direction the head faces, and the head moves into that new cell. The tail never retracts, so every cell the body has covered stays covered. Take the case L=3L = 3 shown in the picture above. The snake starts on (0,0)(0, 0), is one cell in size, and its head faces right. After one second the body has grown by a cell and covers (0,0)(0, 0) and (1,0)(1, 0), with the head on (1,0)(1, 0). After one more second it covers (0,0)(0, 0), (1,0)(1, 0) and (2,0)(2, 0), with the head on (2,0)(2, 0).

The snake turns the direction its head faces by 90 degrees, clockwise or counterclockwise, on a fixed schedule. The first turn happens t1t_1 seconds after the snake starts, and turn ii (for i>1i > 1) happens tit_i seconds after turn i1i-1 ends. So the snake extends its body by tit_i cells and then turns its head, and turning the head takes no time.

The snake dies the moment its head leaves the board or enters a cell its own body occupies. It keeps extending right up to that moment.

Given the turning schedule, that is each tit_i and its direction, write a program that reports how many seconds after the start the snake dies.

Input

The first line has the integer LL (1L1081 \le L \le 10^8). The second line has the integer NN (0N1030 \le N \le 10^3), the number of times the head turns. Each of the next NN lines describes one turn. Line ii (1iN1 \le i \le N) has the integer tit_i (1ti2×1081 \le t_i \le 2 \times 10^8) followed by diri\mathrm{dir}_i, which is the character L or R. The snake turns its head in direction diri\mathrm{dir}_i at tit_i seconds after the start when i=1i = 1, and at tit_i seconds after turn i1i-1 otherwise. An L turns the head 90 degrees to the left (counterclockwise) and an R turns it 90 degrees to the right (clockwise).

Output

On the first line print one value, the number of seconds after the start at which the snake dies.

Hint

In the first sample the head enters (1,0)(1, 0), a cell its own body occupies, seven seconds after the start, and the snake dies there.