Harryxun/stf_run huggingface dataset:
n, k = map(int,input().split())
town = list(map(int,input().split()))
cnt = 1
step = 1
now = town[0]
via = [1]
s = {1}
while True:
  now = town[via[-1]-1-
  if not now in s:
    via.append(now)
    s.add(now)
  else:
    start = via.index(now)
    roop = len(via) - start
    break


if len(via) == 1:
    print(1)
    exit()
 
elif len(via) > k:
    print(via[k])
    exit()
 
else:
    roop_remainder = (k + 1 - start) % roop
    if roop_remainder == 0:
        print(via[-1])
    else:
        print(via[start + roop_remainder -1])


### FIXED CODE ###

n, k = map(int,input().split())
town = list(map(int,input().split()))
cnt = 1
step = 1
now = town[0]
via = [1]
s = {1}
while True:
  now = town[via[-1]-1]
  if not now in s:
    via.append(now)
    s.add(now)
  else:
    start = via.index(now)
    roop = len(via) - start
    break
 
 
if len(via) == 1:
    print(1)
    exit()
 
elif len(via) > k:
    print(via[k])
    exit()
 
else:
    roop_remainder = (k + 1 - start) % roop
    if roop_remainder == 0:
        print(via[-1])
    else:
        print(via[start + roop_remainder -1])
from collections import defaultdict
import copy


mod = 10 ** 9 + 7
primes = []
for i in range(2,1002):
  for prime in primes:
    if not i % prime:
      break
  else:
    primes.append(i)

def factorize(n):
  now = n
  fct = defaultdict(int)
  for prime in primes:
    cnt = 0
    if prime ** 2 > n:
      break
    while not now % prime:
      now //= prime
      cnt += 1
    if cnt:
      fct[prime] = cnt
  if now > 1:
    fct[now] = 1
  return fct


n = int(input())
a = list(map(int, input().split()))
fct_a = list()
for i in a:
  fct_a.append(factorize(i))
lcm = defaultdict(int)
for i in fct_a:
  for k,v in i.items():
    lcm[k] = max(lcm[k], v)
ans = 0
for i in fct_a:
  b = 1
  for k,v in lcm.items():
    b *= k**(v-i[k])
    b %= mod
  ans += b
ans %= mod
print(ans)


### FIXED CODE ###

n,m = map(int, input().split())
if n == m:
  print("Yes")
else:
  print("No")
n=int(input())
if n%2=0:
  print(1/2)
else:
  print((n+1)/2n)

### FIXED CODE ###

n=int(input())
if n%2==0:
  print(1/2)
else:
  print((n+1)/(2*n))
#https://zoom.us/j/96904363338?pwd=dVhyazRpV29MeWVEMGJRNi9ycmxmZz09
N,M=map(int,input().split())
A=[int(x) for x in input().split()]


loop_all=[1]
num=0
for index,i in enumerate(A,start=0):
    if i not in loop_all:
        loop_all.append(A[num])
        num=A[num]-1

loop_1=[]
tmp1=A[A[len(loop_all)-1]-1]
tmp2=loop_all.index(tmp1)

num1=0
for i in loop_all:
    if num1==tmp2:
        break
    loop_1.append(i)
    num1+=1


loop_2=[]
for i in loop_all[tmp2:]:
    loop_2.append(i)

if M<=len(loop_1)-1:
    print(A[K])
else:
    print(loop_2[ (M-len(loop_1)-1)%len(loop_2)-2])

### FIXED CODE ###

n,k=map(int,input().split())
a=list(map(int,input().split()))

p=1
if k<=n:
  for i in range(k):
    p=a[p-1]
  print(p)
  exit()

t=[-1]*n
t[0]=0
for i in range(1,n):
  p=a[p-1]
  if t[p-1] !=-1:
    break
  t[p-1]=i

d=i-t[p-1]
k-=i
k%=d
for i in range(k):
  p=a[p-1]
print(p)
import collections
n = int(input())
one = collections.deque([1])
two = collections.deque([])
three = collections.deque([])
graph = [[] for _ in range(n)]

for i in range(n - 1):
    a, b = map(int, input().split())
    graph[a - 1].append(b - 1)
    graph[b - 1].append(a - 1)
    if i % 3 == 0: two.append(i + 2)
    elif i % 3 == 1: three.append(i + 2)
    else: one.append(i + 2)

que = collections.deque([0])
num = [0] * n
dist = [-1] * n
parity = [0] * 2
dist[0] = 0
flag = True
while len(que) != 0:
    i = que.popleft()
    parity[dist[i] % 2] += 1
    for j in graph[i]:
        if dist[j] != -1: continue
        dist[j] = dist[i] + 1
        que.append(j)

if parity[0] <= n // 3 or parity[1] <= n // 3:
    rem = 0 if parity[0] < parity[1] else 1
    for i, v in enumerate(dist):
        if v % 2 == rem:
            num[i] = str(three.popleft())
        else:
            if len(one) != 0: str(one.popleft())
            elif len(two) != 0: str(two.popleft())
            else: str(three.popleft())
else:
    for i, v in enumerate(dist):
        if v % 2 == 0:
            if len(one) != 0: num[i] = str(one.popleft())
            elif len(three) != 0: num[i] = str(three.popleft())
            else:
                flag = False
                break
        else:
            if len(two) != 0: num[i] = str(two.popleft())
            elif len(three) != 0: num[i] = str(three.popleft())
            else:
                flag = False
                break

print(' '.join(num))


### FIXED CODE ###

import collections
n = int(input())
one = collections.deque([1])
two = collections.deque([])
three = collections.deque([])
graph = [[] for _ in range(n)]

for i in range(n - 1):
    a, b = map(int, input().split())
    graph[a - 1].append(b - 1)
    graph[b - 1].append(a - 1)
    if i % 3 == 0: two.append(i + 2)
    elif i % 3 == 1: three.append(i + 2)
    else: one.append(i + 2)

que = collections.deque([0])
num = [0] * n
dist = [-1] * n
parity = [0] * 2
dist[0] = 0
flag = True
while len(que) != 0:
    i = que.popleft()
    parity[dist[i] % 2] += 1
    for j in graph[i]:
        if dist[j] != -1: continue
        dist[j] = dist[i] + 1
        que.append(j)

if parity[0] <= n // 3 or parity[1] <= n // 3:
    rem = 0 if parity[0] < parity[1] else 1
    for i, v in enumerate(dist):
        if v % 2 == rem:
            num[i] = str(three.popleft())
        else:
            if len(one) != 0: num[i] = str(one.popleft())
            elif len(two) != 0: num[i] = str(two.popleft())
            else: num[i] = str(three.popleft())
else:
    for i, v in enumerate(dist):
        if v % 2 == 0:
            if len(one) != 0: num[i] = str(one.popleft())
            elif len(three) != 0: num[i] = str(three.popleft())
            else:
                flag = False
                break
        else:
            if len(two) != 0: num[i] = str(two.popleft())
            elif len(three) != 0: num[i] = str(three.popleft())
            else:
                flag = False
                break

print(' '.join(num))
a,b,c,d=map(int,input().split())
ans=max[a*c,a*d,b*c,b*d]
print(ans)

### FIXED CODE ###

a,b,c,d=map(int,input().split())
print(max([a*c,a*d,b*c,b*d]))
data = list(map(int,input().split()))

if data[0] >= 13:
    print(int(data[1]))
elif data[0] >= 6:
    print(int(data[1]/2)
else:
    print(0)


### FIXED CODE ###

data = list(map(int,input().split()))

if data[0] >= 13:
    print(int(data[1]))
elif data[0] >= 6:
    print(int(data[1]/2))
else:
    print(0)
n=int(input())
i = 1
alllist = []
while i < n :
  a,b=map(str, input().split())
  c = [a ,int(b),i]
  alllist.append(c)
  i += 1
alllist.sort(key=lambda x:-x[1],reverse=True)
#print(alllist[0][2])
j = 0
while j < n :
  print(alllist[j-1][2])
  j += 1

### FIXED CODE ###

n=int(input())
i = 1
alllist = []
while i < n+1 :
  a,b=map(str, input().split())
  c = [a ,int(b),i]
  alllist.append(c)
  i += 1
alllist.sort(key=lambda x:(x[0],-x[1]),reverse=True)
alllist.reverse()
j = 0
while j < n :
  print(alllist[j][2])
  j += 1
mylist = []
x = 0
while x <= 1:
  val = input()
  x = x + 1
  if val:
    mylist.append(val)
  else:
    break
 


a = mylist[0].split( )
A = int(a[0])
B = int(a[1])
C = int(a[2])
K = int(mylist[1])
r = A
g = B 
b = C



for k in range(K):
    if r > b:
        b = b * 2
    elif r > g:
        g = g * 2
    elif g > b:
        b = b * 2

if b > g and g > r:
    print("yes")
else:
    print("no")    


### FIXED CODE ###

import sys

x = input()
y = int(x)


a = 400
b = 0
kyu = 8

while(1):
    
    if a + b <= y <= a + b + 199:
        print(str(kyu))
        break
    
    b = b + 200
    kyu = kyu - 1
s=int(input())
print("Good" if s[0]!=s[1] or s[1]!=s[2] or s[2]!=s[3] else "Bad")

### FIXED CODE ###

s=input()
print("Bad" if s[0]==s[1] or s[1]==s[2] or s[2]==s[3] else "Good")
a,b,c=map(int(input().split()))
if a+b+c>=22:
  print("bust")
else:
  print("win")

### FIXED CODE ###

a,b,c=map(int,input().split())
if a+b+c>=22:
  print('bust')
else:
  print('win')
n,x = map(int,input().split())
m = []
for i in range(n):
  m = m.append(int(input())) 
  
print(n + x//min(m))


### FIXED CODE ###

n,x = map(int,input().split())
m = [int(input()) for i in range(n)]
print(n + (x-sum(m))//min(m))
N, M = list(map(int,input().split()))
s = []
c = []
for i in range(M):
    in1, in2 = list(map(int,input().split()))
    s.append(in1 - 1)
    c.append(in2)
    
if M > N:
    print(-1)
else:
    solve()
    
def solve():
    
    res = [-1 for _ in range(N)]

    for i in range(M):
        if res[s[i]] != -1 and res[s[i]] != c[i]:
            print(-1)
            return
        res[s[i]] = c[i] 

    ans = ""
    for i in range(N):
        if res[i] == -1:
            if i == 0:
                ans += "1"
            else:
                ans += "0"
        else:
            ans += str(res[i])
    print(ans)

### FIXED CODE ###

N, M = list(map(int,input().split()))
s = []
c = []
for i in range(M):
    in1, in2 = list(map(int,input().split()))
    s.append(in1 - 1)
    c.append(in2)
    
def solve():
    
    res = [-1 for _ in range(N)]

    for i in range(M):
        if res[s[i]] != -1 and res[s[i]] != c[i]:
            print(-1)
            return
        if s[i] == 0 and c[i] == 0 and N != 1:
            print(-1)
            return
        res[s[i]] = c[i] 

    ans = ""
    
    if N == 1:
        if res[0] == -1:
            print(0)
        else:
            print(res[0])
        return
    
    for i in range(N):
        if res[i] == -1:
            if i == 0:
                ans += "1"
            else:
                ans += "0"
        else:
            ans += str(res[i])
    print(ans)

solve()
import numpy as np
n = int(input())
arr = np.array(list(map(int, input().split())))
q = int(input())

cnt = np.bincount(arr)
cnt = np.concatenate([cnt, np.zeros(10**5 - cnt.shape[0], dtype=int)])

for i in range(q):
    b, c = map(int, input().split())
    cnt[c] += cnt[b]
    cnt[b] = 0
    print((cnt * np.arange(cnt.shape[0])).sum())

### FIXED CODE ###

from collections import defaultdict
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
arr = defaultdict(int)
for k, v in Counter(a).items():
    arr[k] = v
q = int(input())
bc = []
for _ in range(q):
    b, c = map(int, input().split())
    bc.append((b, c))

whole = sum([x * y for x, y in arr.items()])

for b, c in bc:
    arr[c] += arr[b]
    tmp = arr[b]
    arr[b] = 0
    whole += (c - b) * tmp
    print(whole)
import numpy as np
nkq = list(map(int, (input().split())))
ans = [int(input()) for i in range(nkq[2])]
total = nkq[1] - nkq[2]
hold_point = np.array([total]*nkq[0])
for i in ans:
    hold_point[i - 1] += 1
for i in hold_point:
    if i <= 0:
        print('No')
    else:
        print('Yes')

### FIXED CODE ###

import numpy as np
nkq = list(map(int, (input().split())))
ans = [int(input()) for i in range(nkq[2])]
total = nkq[1] - nkq[2]
hold_point = np.array([total]*nkq[0])
for i in ans:
    hold_point[i - 1] += 1
for i in hold_point:
    if i <= 0:
        print('No')
    else:
        print('Yes')
A=list(map(int,input().split()))
A.sort()
for i in range(len(A)):
  if i==0:
    continue
  else:
    res=res+A[i]-A[i-1]
print(res)

### FIXED CODE ###

A=list(map(int,input().split()))
A.sort()
res=0
for i in range(len(A)):
  if i==0:
    continue
  else:
    res=res+A[i]-A[i-1]
print(res)
I0 = lambda :map(int,input().split())
I1 = lambda :int(input())
I2 = lambda :list(map(int,input().split()))
#####################################################
from collections import defaultdict
tree = defaultdict(list)


def inc(tree,counter,p):
    if tree[p] == []:
        return
    for i in tree[p]:
        counter[i-1]+=counter[p-1]
        inc(tree,counter,i)

n ,q = I0()
counter = [0]*n
ver = [0]*n
for i in range(n-1):
    a,b=I0()
    tree[a].append(b)


for i in range(q):
    p,x = I0()
    counter[p-1]+=x
    ver[p-1]+=x

inc(tree,counter,1)

print(*counter)



### FIXED CODE ###

I0 = lambda :map(int,input().split())
I1 = lambda :int(input())
I2 = lambda :list(map(int,input().split()))
#####################################################


from sys import stdin,setrecursionlimit
#from copy import deepcopy
 
setrecursionlimit(10**6)
input=stdin.readline

def dfs(p,father=-1):
    for i in tree[p]:
        if i == father:
            continue
        counter[i]+=counter[p]
        dfs(i,p)

n ,q = I0()
counter = [0]*n
tree = [[] for i in range(n)]
for i in range(n-1):
    a,b=I0()
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)

for i in range(q):
    p,x = I0()
    counter[p-1]+=x
    

dfs(0)

print(*counter)

'''stack = [1]
while stack:
    yy = stack.pop()
    for i in tree[yy]:
        counter[i-1]+=counter[yy-1]
        stack.append(i)'''
N,A =map(int,input().split())

if N/500 >=1 and N%500 <= A:
  print("Yes")
else:
  print("No")


### FIXED CODE ###

N = int(input())
A = int(input())

if N%500 <= A:
  print("Yes")
else:
  print("No")
n, k = map(int, input().split())
d = sorted(list({0, 1, 2, 3, 4, 5, 6, 7, 8, 9} - set(map(int, input().split()))))
n = str(n)
ans = ''
for i in n:
    for j in d:
        if j >= int(i):
            ans += str(j)
            break
print(int(ans))

### FIXED CODE ###

n, k = map(int, input().split())
d = set(map(str, input().split()))
for i in range(n, 10*n):
    if len(d & set(str(i))) == 0:
        print(i)
        break
H,W= map(int,raw_input().split())

M = []
for i in range(H):
	M.append(raw_input())
	
flag_list_H = []
for i in range(H):
	if '#' in M[i]:
		flag_list_H.append(True)
	else:
		flag_list_H.append(False)

flag_list_W = []
for i in range(W):
	flag = False
	for j in range(H):
		if M[j][i]=='#':
			flag=True
	flag_list_W.append(flag)

for i in range(H):
	answer = ""
	for j in range(W):
		if flag_list_H[i] and flag_list_W[j]:
			answer+=M[i][j] + " "
	print answer[:-1]
	

### FIXED CODE ###

N,i= map(int,raw_input().split())

print N-i+1
import numpy as np


def div(n, d):
    if d == 2:
        return format(n, "b")[::-1].find("1")
    cnt = 0
    while n % d == 0:
        n //= d
        cnt += 1
    return cnt


n = int(input())
arr = np.array([float(input()) for _ in range(n)], np.float64)
arr = np.rint(arr * 10**9).astype(np.int64)

f = np.vectorize(div)
arr2 = f(arr, 2) + np.zeros([arr.shape[0], arr.shape[0]], np.int64)
arr5 = f(arr, 5) + np.zeros([arr.shape[0], arr.shape[0]], np.int64)

arr2 = np.tril(arr2 + arr2.T)
arr2 -= arr2.T
arr5 = np.tril(arr5 + arr5.T)
arr5 -= arr5.T

result = (arr2 >= 18).astype(np.int32) + (arr5 >= 18).astype(np.int32)

print(np.sum(result >= 2))

### FIXED CODE ###

def div(n, d):
    cnt = 0
    while n % d == 0 and cnt < 18:
        n //= d
        cnt += 1
    return cnt


n = int(input())
dp = [[0]*19 for _ in range(19)]

ans = 0
for _ in range(n):
    x = round(float(input()) * pow(10, 9))
    two = div(x, 2)
    five = div(x, 5)
    ans += dp[18 - two][18 - five]
    for i in range(two + 1):
        for j in range(five + 1):
            dp[i][j] += 1

print(ans)
N=int(input())
A = list(map(int, input().split()))
if N==1:
    print(1)
else:
    count=0
    for i in range(N-1):
        for j in range(i,N):
            if A[i]<=A[j+1]:
                count=count+1
                break
    print(count)

### FIXED CODE ###

N=int(input())
A = list(map(int, input().split()))
count=1
m=A[0]
for i in range(1,N):
    if m>=A[i]:
        count=count+1
        m=A[i]
print(count)
from collections import defaultdict

N = int(input())
d = defaultdict(list)
cd = set()
D = 100000

for _ in range(N):
    x, y = map(int, input().split())
    y += D
    cd.add(x)
    d[x].append(y)
    d[y].append(x)

used = [0]*(D+1)
ans = 0

for x in cd:
    if used[x]:continue
    q = [x]
    used[x] = 1
    cx, sx = 0, 0

    while q:
        a = q.pop()
        if a<=D:
            cx += 1
        sx += 1
        for b in d[a]:
            if used[b]:continue
            used[b] = 1
            q.append(b)
    ans += cx*(sx-cx)

print(ans-N)


### FIXED CODE ###

from collections import defaultdict

N = int(input())
d = defaultdict(list)
cd = set()
D = 100000
used = [0]*(2*D+1)
ans = 0

for _ in range(N):
    x, y = map(int, input().split())
    y += D
    cd.add(x)
    d[x].append(y)
    d[y].append(x)

for x in cd:
    if used[x]:continue
    q = [x]
    used[x] = 1
    cx, sx = 0, 0

    while q:
        a = q.pop()
        if a<=D:
            cx += 1
        sx += 1
        for b in d[a]:
            if used[b]:continue
            used[b] = 1
            q.append(b)
    ans += cx*(sx-cx)

print(ans-N)
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
A.sort()
BC = []

for i in range(M):
  bc = [int(i) for i in input().split()]
  BC.append(bc)

BC = sorted(BC, key=lambda x: x[1])
BC.reverse()

k = 0
for i in BC:
  if i[1] <= A[k]:
    break
  for j in range(i[0]):
    if i[1] > A[k]:
      A[k] = i[1]
      k += 1
    else:
      break

print(sum(A))

### FIXED CODE ###

N, M = map(int, input().split())
A = [int(i) for i in input().split()]
A.sort()
BC = []

for i in range(M):
  bc = [int(i) for i in input().split()]
  BC.append(bc)

BC = sorted(BC, key=lambda x: x[1])
BC.reverse()

k = 0
for i in BC:
  if k == N:
    break
  if i[1] <= A[k]:
    break
  for j in range(i[0]):
    if k == N:
      break
    if i[1] > A[k]:
      A[k] = i[1]
      k += 1
    else:
      break

print(sum(A))
import sys
import math
import itertools

a,b,k=list(map(int,input().split()))
ans=[]
for i in range (1,max(a,b),1):
	if a%i==0 and b%i==0:
		ans.append(i)
print(ans[-k])

### FIXED CODE ###

import sys
import math
import itertools

a,b,k=list(map(int,input().split()))
ans=[]
for i in range (1,max(a,b)+1,1):
	if a%i==0 and b%i==0:
		ans.append(i)
print(ans[-k])
from sys import stdin
import math

a=input()
if (a[0]<a[2]):
    print("<")
elif(a[0]==a[2])
    print("=")
else:
    print(">")

### FIXED CODE ###

from sys import stdin
import math

a=input()
if (a[0]<a[2]):
    print("<")
elif(a[0]==a[2]):
    print("=")
else:
    print(">")
import pandas as pd
n = int(input())

list_A = []
list_B = []

for i in range(n):
    a, b = map(int, input().split())
    list_A.append(a)
    list_B.append(b)

# print(list_A , list_B)

df = pd.DataFrame()
df["a"] = list_A
df["b"] = list_B
df = df.sort_values(["b", "a"]).reset_index(drop = True)
for i in df.index[1:]:
    df.loc[i, "a"] = df.loc[i, "a"] + df.loc[i-1, "a"]
    if df.loc[i, "a"] > df.loc[i, "b"]:
        print("No")
        exit()
print("Yes")

### FIXED CODE ###

import heapq as hq

n = int(input())

list_A = []
list_B = []
h = []
for i in range(n):
    a, b = map(int, input().split())
    hq.heappush(h, [b, a])

time = 0
for _ in range(n):
    b, a = hq.heappop(h)
    time += a
    if b < time:
        print("No")
        exit()

print("Yes")
import collections


n=int(input())
vList=list(map(int,input().split()))

gusuList=[]
kisuList=[]

for i in range(n):
  if(i % 2==0):
    gusuList.append(vList[i])
  else:
    kisuList.append(vList[i])
#print(gusuList)
#print(kisuList)

g = collections.Counter(gusuList)
gStatus=g.most_common()

k = collections.Counter(kisuList)
kStatus=k.most_common()

ans=-100

if(not gStatus[0][0]==kStatus[0][0]):
    ans=(n/2-gStatus[0][1])+(n/2-kStatus[0][1])
else:
  if(gStatus[0][1]>kStatus[0][1]):
    if(not len(kStatus)==1):
      ans=(n/2-gStatus[0][1])+(n/2-kStatus[1][1])
    else:
      ans=(n/2-gStatus[0][1])+n/2
  elif(gStatus[0][1]<kStatus[0][1]):
    if(not len(gStatus)==1):
      ans=(n/2-gStatus[1][1])+(n/2-kStatus[0][1])
    else:
      ans=n/2+(n/2-kStatus[0][1])
  elif(gStatus[0][1]==kStatus[0][1]):
      if(gStatus[1][1]>=kStatus[1][1]):
        if(not len(kStatus)==2):
          ans=(n/2-gStatus[0][1])+(n/2-kStatus[2][1])
        else:
          ans=(n/2-gStatus[0][1])+(n/2-kStatus[1][1])
      elif(gStatus[0][1]<kStatus[0][1]):
        if(not len(gStatus)==2):
          ans=(n/2-gStatus[2][1])+(n/2-kStatus[0][1])
        else:
          ans=(n/2-gStatus[1][1])+(n/2-kStatus[0][1])
      
    
    
print(int(ans))


### FIXED CODE ###

import collections

n=int(input())
vList=list(map(int,input().split()))

gusuList=[]
kisuList=[]

for i in range(n):
  if(i % 2==0):
    gusuList.append(vList[i])
  else:
    kisuList.append(vList[i])
#print(gusuList)
#print(kisuList)

g = collections.Counter(gusuList)
gStatus=g.most_common()

k = collections.Counter(kisuList)
kStatus=k.most_common()

ans=-100
if(not gStatus[0][0]==kStatus[0][0]):
    ans=(n/2-gStatus[0][1])+(n/2-kStatus[0][1])
else:
    ans1=-101
    ans2=-102
    if(not len(kStatus)==1):
      ans1=(n/2-gStatus[0][1])+(n/2-kStatus[1][1])
    else:
      ans1=(n/2-gStatus[0][1])+n/2

    if(not len(gStatus)==1):
      ans2=(n/2-gStatus[1][1])+(n/2-kStatus[0][1])
    else:
      ans2=n/2+(n/2-kStatus[0][1])

    ans=min(ans1,ans2)

print(int(ans))
import sys
input=sys.stdin.readline
N,Q=map(int,input().split())
s=["s"]+list(input())+["e"]

A=[]

for i in range(Q):
    t,d=list(input().split())
    A.append([t,d])
#print(A)

High=N
Low=1

while abs(High - Low)>4:
    Mid=(High + Low)//2
    #print(H,Mid,L)
    P=Mid
    T=s[P]
    for i in range(Q):
        if A[i][0]==T:
            if A[i][1]=="L":
                P-=1
                T=s[P]
            else:
                P+=1
                T=s[P]
    if P==0:
        Low=Mid
    else:
        High=Mid
for j in range(min(5,N)):
    Mid= High-j
    if Mid==0:
        left=0
    P=Mid
    T=s[P]
    for i in range(Q):
        if A[i][0]==T:
            if A[i][1]=="L":
                P-=1
                T=s[P]
            else:
                P+=1
                T=s[P]
    if P==0:
        left=Mid+1
        break
#print(left)        

High=N
Low=1

while abs(High - Low)>4:
    Mid=(High + Low)//2
    #print(H,Mid,L)
    P=Mid
    T=s[P]
    for i in range(Q):
        if A[i][0]==T:
            if A[i][1]=="L":
                P-=1
                T=s[P]
            else:
                P+=1
                T=s[P]
    if P==N:
        High=Mid
    else:
        Low=Mid
for j in range(min(5,N)):
    Mid= Low+j
    if Mid==N:
        right=N
    P=Mid
    T=s[P]
    for i in range(Q):
        if A[i][0]==T:
            if A[i][1]=="L":
                P-=1
                T=s[P]
            else:
                P+=1
                T=s[P]
    if P==N+1:
        right=Mid-1
        break
#print(right)        
print(max(0,right-left+1))

        

### FIXED CODE ###

N,Q=map(int,input().split())
s=["x"]+list(input())+["x"]
#print(s)
L=[]
for i in range(Q):
    t=list(input().split())
    L.append(t)
#print(L)
h=N
l=1
LEFT=-10
RIGHT=-10
while abs(h-l)>2:
    Mid=(h+l)//2
    P=Mid
    for i in range(Q):
        if s[P]==L[i][0]:
            if L[i][1]=="L":
                P-=1
            else:
                P+=1
                
    if P==0:
        l=Mid
    else:
        h=Mid
#print(l,h)
for j in range(4):
    M=l-1+j
    P=M
    for i in range(Q):
        if s[P]==L[i][0]:
            #print("S",j,P,i)
            if L[i][1]=="R":
                P+=1
            else:
                P-=1
    #print("A",M,P)
    if P!=0:
        LEFT=l-1+j
        break

h=N
l=1

while abs(h-l)>2:
    Mid=(h+l)//2
    P=Mid
    for i in range(Q):
        if s[P]==L[i][0]:
            if L[i][1]=="L":
                P-=1
            else:
                P+=1
                
    if P==N+1:
        h=Mid
    else:
        l=Mid
for j in range(4):
    M=h+1-j
    P=M
    for i in range(Q):
        if s[P]==L[i][0]:
            if L[i][1]=="L":
                P-=1
            else:
                P+=1
    if P!=N+1:
        RIGHT=h+1-j
        break
if LEFT==-10 or RIGHT==-10:
    print(0)
else:
    print(max(0,RIGHT - LEFT +1))
#print(RIGHT,LEFT)
a,b = map (int, input().split)
if a<b:
    print("a < b")
elif a>b:
    print("a > b")
else:
    print("a == b")


### FIXED CODE ###

a,b = map (int, input().split())
if a<b:
    print("a < b")
elif a>b:
    print("a > b")
else:
    print("a == b")