#abc376b. B - Hands on Ring (Easy)

B - Hands on Ring (Easy)

Score : 250250 points

问题陈述

注意:这个问题的设置几乎与问题F相同。只有在正文中加粗的部分和约束条件有所不同。

你用双手拿着一个环。这个环由N (N3)N\ (N \geq 3)部分组成,编号为1,2,,N1,2,\dots,N,其中第ii部分和第i+1i+1部分(1iN11 \leq i \leq N-1)是相邻的,第1部分和第N部分也是相邻的。

最初,你的左手拿着第1部分,右手拿着第2部分。在一个 操作 中,你可以做以下事情:

  • 将一只手移动到它当前持有的部分的相邻部分。但是,只有当另一只手不在目的地部分时,你才能这样做。

下面的图表显示了初始状态以及可以从那里进行和不能进行的操作示例。环的每个部分上写的数字代表部分编号,标记为L和R的圆圈分别代表你的左手和右手。

你需要按照顺序遵循给你的QQ个指令。第ii个(1iQ1 \leq i \leq Q)指令由一个字符HiH_i和一个整数TiT_i表示,含义如下:

  • 执行一些操作(可能是零),使你的左手(如果HiH_iL)或右手(如果HiH_iR)拿着第TiT_i部分。这里,你不能移动HiH_i未指定的另一只手。

保证只给出可实现的指令。

在这个问题的设置下,可以证明,在遵循第ii个指令之前,两只手的位置是唯一确定的。在那时,如果我们将左手和右手的位置分别表示为部分lil_irir_i,那么当HiH_iL时,保证TiriT_i \neq r_i,当HiH_iR时,保证TiliT_i \neq l_i

找出遵循所有指令所需的最小总操作数。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

Note: This problem has almost the same setting as Problem F. Only the parts in bold in the main text and constraints differ.

You are holding a ring with both hands. This ring consists of N (N3)N\ (N \geq 3) parts numbered 1,2,,N1,2,\dots,N, where parts ii and i+1i+1 (1iN11 \leq i \leq N-1) are adjacent, and parts 11 and NN are also adjacent.

Initially, your left hand is holding part 11, and your right hand is holding part 22. In one operation, you can do the following:

  • Move one of your hands to an adjacent part of the part it is currently holding. However, you can do this only if the other hand is not on the destination part.

The following figure shows the initial state and examples of operations that can and cannot be made from there. The number written on each part of the ring represents the part number, and the circles labeled L and R represent your left and right hands, respectively.

You need to follow QQ instructions given to you in order. The ii-th (1iQ1 \leq i \leq Q) instruction is represented by a character HiH_i and an integer TiT_i, meaning the following:

  • Perform some number of operations (possibly zero) so that your left hand (if HiH_i is L) or your right hand (if HiH_i is R) is holding part TiT_i. Here, you must not move the other hand not specified by HiH_i.

It is guaranteed that only achievable instructions are given.

Details Under the settings of this problem, it can be proved that the positions of both hands are uniquely determined just before following the ii-th instruction for each ii. At that time, if we denote the positions of the left and right hands as parts lil_i and rir_i, respectively, it is guaranteed that TiriT_i \neq r_i when HiH_i is L, and TiliT_i \neq l_i when HiH_i is R.

Find the minimum total number of operations required to follow all the instructions.

Constraints

  • 3N1003 \leq N \leq 100
  • 1Q1001 \leq Q \leq 100
  • HiH_i is L or R.
  • 1TiN1 \leq T_i \leq N
  • NN, QQ, and TiT_i are integers.
  • Only achievable instructions are given (see the problem statement for details).

Input

The Input is given from Standard Input in the following format:

NN QQ

H1H_1 T1T_1

H2H_2 T2T_2

\vdots

HQH_Q TQT_Q

Output

Print the minimum total number of operations required to follow all the instructions.

Sample Input 1

6 3
R 4
L 5
R 6

Sample Output 1

8

By performing the following operations, you can follow all QQ instructions in order.

  1. Move your right hand as part 2342 \rightarrow 3 \rightarrow 4 to follow the first instruction.
  2. Move your left hand as part 1651 \rightarrow 6 \rightarrow 5 to follow the second instruction.
  3. Move your right hand as part $4 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 6$ to follow the third instruction.

In this case, the total number of operations is 2+2+4=82+2+4=8, which is the minimum. (Note that when following the third instruction, you cannot move your right hand as part 4564 \rightarrow 5 \rightarrow 6.)

Sample Input 2

100 2
L 1
R 2

Sample Output 2

0

There are cases where you can follow the instructions without performing any operations.

Sample Input 3

30 8
R 23
R 26
R 29
L 20
R 29
R 19
L 7
L 16

Sample Output 3

92