#arc177f. F - Two Airlines

F - Two Airlines

Score: 10001000 points

问题陈述

AtCoder共和国由L+1L+1个东西向排列的岛屿组成,从西向东编号为00LL。这些岛屿通过空中航线相连,对于每个1iL1 \leq i \leq L,岛屿i1i-1ii之间有一条双向空中航线,如图中所示。每条航线由A公司或J公司拥有,岛屿i1i-1ii之间的航线由SiS_i公司拥有。

这个国家有NN名居民,编号为11NN。居民ii目前位于岛屿XiX_i

每位居民都有一张公司的优惠券。具体来说,居民ii有一张CiC_i公司的优惠券。凭借优惠券,居民可以无限次免费乘坐该公司的航线,但乘坐另一家公司的航线每次需要支付1枚硬币。

现在,岛屿00上有一个宝箱。居民们希望合作将其运送到首都,首都位于岛屿LL。确定实现此目标所需的最少总硬币数。

居民们可以相互传递宝箱,但不能传递他们的优惠券。

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

Problem Statement

The Republic of AtCoder consists of L+1L+1 islands aligned east-west, numbered 00 to LL from west to east. The islands are connected by air routes, where for each 1iL1 \leq i \leq L, there is a bidirectional air route between islands i1i-1 and ii, as shown in the figure below. Each air route is owned by company A or company J, and the route between islands i1i-1 and ii is owned by company SiS_i.

The nation has NN residents, numbered 11 to NN. Resident ii is currently on island XiX_i.

Each resident has a coupon of one of the companies. Specifically, resident ii has a coupon of company CiC_i. With a coupon, a resident can take that company's routes for free any number of times, but taking a route of the other company costs 11 coin per trip.

Now, there is a treasure chest on island 00. The residents want to cooperate to carry it to the capital, which is on island LL. Determine the minimum total number of coins required to achieve this goal.

The residents can pass the treasure chest to each other, but not their coupons.

Constraints

  • 1L6×1041 \leq L \leq 6 \times 10^4
  • 1N6×1041 \leq N \leq 6 \times 10^4
  • Si (1iL)S_i \ (1 \leq i \leq L) is A or J.
  • 0XiL (1iN)0 \leq X_i \leq L \ (1 \leq i \leq N)
  • Ci (1iN)C_i \ (1 \leq i \leq N) is A or J.
  • LL, NN, and XiX_i are integers.

Input

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

LL NN

S1S_1S2S_2\cdotsSLS_L

X1X_1 C1C_1

X2X_2 C2C_2

\vdots

XNX_N CNC_N

Note that the second line is given as a string of length LL.

Output

Print the answer.

Sample Input 1

4 3
AAJJ
3 A
1 J
1 J

Sample Output 1

2

The following procedure carries the treasure chest to island 44 for a total of 22 coins.

  1. Resident 11 moves from island 33 to island 22. The route is not covered by the coupon, so 11 coin is paid.
  2. Resident 11 moves from island 22 to island 11. The route is covered by the coupon, so no coin is needed.
  3. Resident 11 moves from island 11 to island 00. The route is covered by the coupon, so no coin is needed.
  4. Resident 11 picks up the treasure chest.
  5. Resident 11, carrying the treasure chest, moves from island 00 to island 11. The route is covered by the coupon, so no coin is needed.
  6. Resident 11 passes the treasure chest to resident 22.
  7. Resident 22, carrying the treasure chest, moves from island 11 to island 22. The route is not covered by the coupon, so 11 coin is paid.
  8. Resident 22, carrying the treasure chest, moves from island 22 to island 33. The route is covered by the coupon, so no coin is needed.
  9. Resident 22, carrying the treasure chest, moves from island 33 to island 44. The route is covered by the coupon, so no coin is needed.

Sample Input 2

8 3
JJAAJJAJ
2 A
6 A
8 J

Sample Output 2

6

Sample Input 3

8 6
JJAAJJAJ
2 A
6 A
8 J
8 J
8 J
8 J

Sample Output 3

4