Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

import sys
input = sys.stdin.readline
import bisect
sys.setrecursionlimit(10**8)

N,Q=map(int,input().split())
S=input().strip()
S=[ord(s)-65 for s in S]
M=[input().split() for i in range(Q)]

MLIST=[[] for i in range(26)]

for i in range(Q):
    m,lr=M[i][0],M[i][1]
    MLIST[ord(m)-65].append(i)

dist=dict()
def distinct(PLACE,TURN):
    #print(PLACE,TURN)

    x=bisect.bisect_left(MLIST[S[PLACE]],TURN)

    if x==len(MLIST[S[PLACE]]):
        #dist[(PLACE,TURN)]=1
        return 1

    TURN=MLIST[S[PLACE]][x]

    if dist.get((PLACE,TURN))!=None:
        return dist.get((PLACE,TURN))
    
    if PLACE==0 and M[TURN][1]=="L":
        dist[(PLACE,TURN)]=0
        return 0
    
    if PLACE==N-1 and M[TURN][1]=="R":
        dist[(PLACE,TURN)]=0
        return 0

    if M[TURN][1]=="L":
        dist[(PLACE,TURN)]=distinct(PLACE-1,TURN+1)
        return dist[(PLACE,TURN)]

    if M[TURN][1]=="R":
        dist[(PLACE,TURN)]=distinct(PLACE+1,TURN+1)
        return dist[(PLACE,TURN)]

ANS=0
for i in range(N):
    ANS+=distinct(i,0)
    #print(i,dist)

print(ANS)

### FIXED CODE ###

import sys
input = sys.stdin.readline

N,Q=map(int,input().split())
S=input().strip()
M=[input().split() for i in range(Q)]

PLACE=-1

for i in range(Q-1,-1,-1):

    if PLACE+1<N and M[i][0]==S[PLACE+1] and M[i][1]=="L":
        PLACE+=1

    elif PLACE>=0 and M[i][0]==S[PLACE] and M[i][1]=="R":
        PLACE-=1

    if PLACE==N:
        print(0)
        sys.exit()

PLACE2=N

for i in range(Q-1,-1,-1):

    if PLACE2-1>=0 and M[i][0]==S[PLACE2-1] and M[i][1]=="R":
        PLACE2-=1

    elif PLACE2<=N-1 and M[i][0]==S[PLACE2] and M[i][1]=="L":
        PLACE2+=1

    if PLACE==-1:
        print(0)
        sys.exit()

print(max(0,N-(PLACE+1+(N-PLACE2))))
### BUGGY CODE ###

a,b = map(int,input().split())
import math
def cf(x1,x2):
    cf=[]
    for i in range(2,min(x1,x2)+1):
        if x1 % i == 0 and x2 % i == 0:
            cf.append(i)
    return cf

cf = cf(a,b)
for c in cf:
    cf = [i for i in cf if i%c != 0 or i == c]
print(len(cf) + 1)


### FIXED CODE ###

a,b = map(int,input().split())
import fractions
import collections
import copy
def prime_factorization(n):
    a = [1]
    n1 = copy.deepcopy(n)
    while n % 2 == 0:
        n //= 2
    if n1 != n:
        a.append(2)
    f = 3
    while f * f <= n:
        if n % f == 0:
            n //= f
            if a[-1] != f:
                a.append(f)
        else:
            f += 2
    if a[-1] != n and n != 1:
        a.append(n)
    return a
print(len(prime_factorization(fractions.gcd(a,b))))
### BUGGY CODE ###

s = input()
q = int(input())
for i in range(q):
    query = list(input().split())
    if query[0] == "2":
        if query[1] == "1":
            s = query[2] + s
        else:
            s = s + query[2]
    else:
        temp = s[:]
        s = ""
        for j in range(len(temp)):
            s += temp[-1-j]
print(s)

### FIXED CODE ###

from collections import deque
s = deque(input())
q = int(input())
state = 0
for i in range(q):
    query = input().split()
    if query[0] =="1":
        state +=1
    else:
        if (int(query[1])+state)%2 == 1:
            s.appendleft(query[2])
        else:
            s.append(query[2])
if state%2 == 1:
    s.reverse()
print("".join(s))
### BUGGY CODE ###

N = int(input())
P = list(map(int,input().split()))
T = [0]*N
ans = 0

for i in range(N):
    T[i] = min([P[0]]+P[:i])

for i in range(N):
    if P[i] <= T[i]:
        ans += 1

print(ans)

### FIXED CODE ###

N = int(input())
P = list(map(int,input().split()))
T = [P[0]]+[10**10]*(N-1)
ans = 0

for i in range(N):
    T[i] = min(T[i-1],P[i])

for i in range(N):
    if P[i] <= T[i]:
        ans += 1

print(ans)
### BUGGY CODE ###

s=input()
t=''
while s!='':
  if s[:5]=='dream':
    if s[5:10]=='erase':
      s=s[5:]
      continue
    elif s[5:7]=='er':
      s=s[7:]
      continue
  elif s[:5]=='erase':
    if s[5]=='r':
      s=s[6:]
    else:
      s=s[5:]
      
  else:
    break
    
if s=='':
  print('YES')
  
else:
  print('NO')

### FIXED CODE ###

s=input()
t=''
while s!='':
  if len(s)<=4:
    break
  elif s[:5]=='dream':
    if len(s)>=10 and s[5:10]=='erase':
      s=s[5:]
      continue
    elif len(s)>=7 and s[5:7]=='er':
      s=s[7:]
      continue
    else:
      s=s[5:]
      continue
  elif s[:5]=='erase':
    if len(s)>=6 and s[5]=='r':
      s=s[6:]
      continue
    else:
      s=s[5:]
      continue
  
  elif s[:5]!='dream' and s[:5]!='erase':
    break
      
    
if s=='':
  print('YES')
  
else:
  print('NO')
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))
for i in range(1,N+1):
    print(A[i-1:].count(i))

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
B = [0 for _ in range(N)]
for i in A:
    B[i-1] += 1
for j in range(N):
    print(B[j])
### BUGGY CODE ###

s = input()
n = int(input())
query_li = (input().split() for i in range(n))

flag = 1

for query in query_li:
    if int(query[0]) == 1:
        flag = flag * -1
    else:
        if int(query[1]) == 1:
            s = query[2] + s if flag == 1 else s + query[2]
        else:
            s = s + query[2] if flag == 1 else query[2] + s

if flag == -1:
    s = s[::-1]

print(s)

### FIXED CODE ###

from collections import deque

s = deque(input().split())
n = int(input())
query_li = (input().split() for i in range(n))

flag = 1

for query in query_li:
    if int(query[0]) == 1:
        flag = flag * -1
    else:
        if int(query[1]) == 1:
            if flag == 1:
                s.appendleft(query[2])
            else:
                s.append(query[2])
        else:
            if flag == 1:
                s.append(query[2])
            else:
                s.appendleft(query[2])

s = ''.join(s)
if flag == -1:
    s = s[::-1]

print(s)
### BUGGY CODE ###

import math

def primelis(n):
    a = []
    if n % 2 == 0:
        a.append(2)
        while n%2==0:
          n=n//2
    f = 3
    while f * f <= n:
      if n%f==0:
        a.append(f)
        while n%f==0:
          n=n//f
    if n != 1:
        a.append(n)
    return a



n=int(input())

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

gcd=A[0]

for i in range(n-1):
  gcd=math.gcd(gcd,A[i+1])

nglis=[0 for i in range(10**6)]

if gcd!=1:
  print("not coprime")
else:
  check=0
  for i in range(n):
    if nglis[A[i]-1]==1:
      check=1
      break
    else:
      for pi in primelis(A[i]):
        j=1
        while pi*j<=10**6:
          nglis[pi*j-1]=1
          j+=1
  if check==1:
    print("setwise coprime")
  else:
    print("pairwise coprime")



### FIXED CODE ###

import math

def primelis(n):
    a = []
    if n % 2 == 0:
        a.append(2)
        while n%2==0:
          n=n//2
    f = 3
    while f * f <= n:
      if n%f==0:
        a.append(f)
        while n%f==0:
          n=n//f
    if n != 1:
        a.append(n)
    return a



n=int(input())

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

gcd=A[0]

for i in range(n-1):
  gcd=math.gcd(gcd,A[i+1])

d=[0 for i in range(10**6+1)]

for i in range(2,10**6+1):
  if d[i]==0:
    j=1
    while i*j<=10**6:
      d[i*j]=i
      j+=1

nglis=[0 for i in range(10**6+1)]

if gcd!=1:
  print("not coprime")
else:
  check=0
  for i in range(n):
    if nglis[A[i]]==1:
      check=1
      break
    else:
      ai=A[i]
      while ai!=1:
        j=1
        while d[ai]*j<=10**6:
          nglis[d[ai]*j]=1
          j+=1
        ai=ai//d[ai]
  if check==1:
    print("setwise coprime")
  else:
    print("pairwise coprime")
### BUGGY CODE ###

from collections import deque

import sys
input = sys.stdin.readline


n = int(input())
a_list = [None for _ in range(n)]
num = 0
day = 0
for i in range(n):
    a_list[i] = deque([int(i) - 1 for i in input().split()])

for kkk in range(n * (n - 1) // 2):
    visit = set([])
    day += 1
    for j in range(n):
        if j not in visit:
            if len(a_list[j]) > 0:
                k = a_list[j][0]
                if k not in visit:
                    if a_list[k][0] == j:
                        num += 1
                        visit.add(a_list[j].popleft())
                        visit.add(a_list[k].popleft())
    if (len(visit) == 0):
        day = -1
        break
    if num >= n * (n - 1) // 2:
        break
print(day)

### FIXED CODE ###

from collections import deque
n = int(input())
a_list = []
for i in range(n):
    a_list.append(deque([int(aa) - 1 for aa in input().split()]))
day = 0
battle = 0
for _ in range(n * (n - 1) // 2):
    if battle == n * (n - 1) // 2:
        break
    day += 1
    battle_today = 0
    tomorrow = set([])
    if (day == 1):
        player = set([i for i in range(n)])
    for i in player:
        if len(a_list[i]) == 0:
            continue
        j = a_list[i][0]
        if (i == a_list[j][0]) & (j not in tomorrow) & (len(a_list[j]) > 0) & (i not in tomorrow):
            tomorrow.add(i)
            tomorrow.add(a_list[i][0])
            battle += 1
            battle_today += 1
            a_list[i].popleft()
            a_list[j].popleft()
    player = tomorrow
    if battle_today == 0:
        day = -1
        break
print(day)
### BUGGY CODE ###

r,g,b,n=map(int,input().split())
rgb=[r,g,b]
rgb.sort()
r=rgb[0]
g=rgb[1]
b=rgb[2]
ans=0
for i in range(n//b+1):
    for j in range(n//g+1):
        if g*j+b*i==n:
            ans=ans+1
        elif g*j+b*i>n:
            break
        elif (n-(g*j+b*i))%r==0:
            ans=ans+1
print(ans)

### FIXED CODE ###

r,g,b,n=map(int,input().split())
ans=0
for i in range(n//b+1):
    bi=b*i
    for j in range(n//g+1):
        gj=g*j
        if gj+bi>n:
            break
        elif gj+bi==n:
            ans=ans+1
            break
        elif (n-(gj+bi))%r==0:
            ans=ans+1
print(ans)
### BUGGY CODE ###

from operator import mul
from functools import reduce

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

ans = reduce(mul, lis)

if ans > 10**18:
    print("-1")
else:
    print(ans)

### FIXED CODE ###

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

if 0 in lis:
    print(0)
    exit()

ans = 1
for i in lis:
    ans *= i
    if ans >= 10**18:
        break

if ans > 10**18:
    print("-1")
else:
    print(ans)
### BUGGY CODE ###

import fractions
n=int(input())
a = list(map(int, input().split()))
ans = a[0]
for i in range(1, n):
    ans = ans * a[i] // fractions.gcd(ans, a[i])

count=0
for i in range(n):
  count+=(ans//a[i])
print(count%(10**9+7))
  

### FIXED CODE ###

import fractions
n=int(input())
a = list(map(int, input().split()))
ans = a[0]
for i in range(1, n):
    ans = ans * a[i] // fractions.gcd(ans, a[i])

count=0
for i in range(n):
  count+=(ans//a[i])
print(count%(10**9+7))
### BUGGY CODE ###

import sys
from collections import deque
from itertools import groupby
def input(): return sys.stdin.readline().rstrip()

def main():
    N, K = map(int, input().split())
    S = input()

    ans = 0
    l = 0
    que = deque()
    que_sum = 0
    for k, v in groupby(S):
        if k == '0':
            l0 = len(tuple(v))
            que.append(l + l0)
            if len(que) > K:
                que.popleft()
            que_sum = sum(que)
        else:
            l = len(tuple(v))
            que_sum += l
        if que_sum > ans:
            ans = que_sum
    print(ans)

if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
from collections import deque
from itertools import groupby
def input(): return sys.stdin.readline().rstrip()

def main():
    _, K = map(int, input().split())
    S = input()

    ans = 0
    l = 0
    que = deque()
    que_sum = 0
    for k, v in groupby(S):
        if k == '0':
            l0 = len(tuple(v))
            que_sum += l0
            que.append(l + l0)
            if len(que) > K:
                que_sum -= que.popleft()
        else:
            l = len(tuple(v))
            que_sum += l
        if que_sum > ans:
            ans = que_sum
    print(ans)

if __name__ == '__main__':
    main()
### BUGGY CODE ###

k,s = map(int,input().split())
ans = 0
for i in range(k + 1):
    for j in range(k + 1):
        for k in range(k + 1):
            if i + j + k == s:ans+=1
print (ans)


### FIXED CODE ###

k,s = map(int,input().split())
ans = 0
for i in range(k + 1):
    for j in range(k + 1):
        x = s - (i + j)
        if x <= k and x >= 0 : ans+=1
print (ans)
### BUGGY CODE ###

import numpy as np
n,m,k=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
ans=0
a=np.cumsum(A)
b=np.cumsum(B)
a=np.insert(a,0,0)
b=np.insert(b,0,0)

for i in range(n+1):
    if a[i] > k:
        break
    t=k-a[i]
    st=m
    for j in reversed(range(st+1)):
        if b[j] <= t:
            st=j
            ans=max(i+j,ans)
            break
print(ans)



### FIXED CODE ###

import numpy as np
n,m,k=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
ans=0
a=np.cumsum(A)
b=np.cumsum(B)
a=np.insert(a,0,0)
b=np.insert(b,0,0)
j=m
for i in range(n+1):
    if a[i] > k:
        break
    while b[j] > k-a[i]:
        j-=1
    ans=max(ans,i+j)
print(ans)
### BUGGY CODE ###

import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################


H,W = II()
A = []
B = []
for i in range(H):
    A.append(III())
for i in range(H):
    B.append(III())


d = [[set() for _ in range(W)] for _ in range(H)]
for i in range(H):
    for j in range(W):
        if i==0 and j==0:
            d[i][j].add(abs(A[i][j]-B[i][j]))
        elif j==0:
            for k in d[i-1][j]:
                d[i][j].add(abs(k+A[i][j]-B[i][j]))
                d[i][j].add(abs(-k+A[i][j]-B[i][j]))
        elif i==0:
            for k in d[i][j-1]:
                d[i][j].add(abs(k+A[i][j]-B[i][j]))
                d[i][j].add(abs(-k+A[i][j]-B[i][j]))
        else:
            for k in d[i-1][j]:
                d[i][j].add(abs(k+A[i][j]-B[i][j]))
                d[i][j].add(abs(-k+A[i][j]-B[i][j]))
            for k in d[i][j-1]:
                d[i][j].add(abs(k+A[i][j]-B[i][j]))
                d[i][j].add(abs(-k+A[i][j]-B[i][j]))

ans = 2*(H+W-1)*80+1
for i in d[H-1][W-1]:
        now = abs(i)
        if ans>now:
            ans = now
print(ans)


### FIXED CODE ###

import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################

H,W = II()
A,B,C = [],[],[]
for i in range(H):
    A.append(III())
for i in range(H):
    B.append(III())
for i in range(H):
    C.append([abs(A[i][j]-B[i][j]) for j in range(W)])

def calc(n):
    return n+80*(H+W-1)

d = [[0]*W for _ in range(H)]
for i in range(H):
    for j in range(W):
        if i==0 and j==0:
            d[i][j] = 2**calc(-C[i][j]) | 2**calc(C[i][j])
        elif i==0:
            d[i][j] = (d[i][j-1]<<C[i][j]) | (d[i][j-1]>>C[i][j])
        elif j==0:
            d[i][j] = (d[i-1][j]<<C[i][j]) | (d[i-1][j]>>C[i][j])
        else:
            d[i][j] = (d[i][j-1]<<C[i][j]) | (d[i][j-1]>>C[i][j]) | \
                        (d[i-1][j]<<C[i][j]) | (d[i-1][j]>>C[i][j])

ans = (H+W-1)*80+1
for i in range(160*(H+W-1)+1):
    if (d[H-1][W-1]>>i)&1:
        val = abs(i-80*(H+W-1))
        if val<ans:
            ans = val

print(ans)
### BUGGY CODE ###

import numpy as np

N = int(input())
A = np.fromstring(input(), sep=" ", dtype=np.int64)
Q = int(input())
B = np.zeros(Q)
C = np.zeros(Q)
D = np.fromstring(" ".join([input() for q in range(Q)]), sep=" ", dtype=np.int64).reshape((Q, 2))
B = D[:, 0]
C = D[:, 1]

for q in range(Q):
    A[A == B[q]] = C[q]
    print(np.sum(A))

### FIXED CODE ###

import numpy as np

N = int(input())
A = np.fromstring(input(), sep=" ", dtype=np.int64)
Q = int(input())
B = np.zeros(Q)
C = np.zeros(Q)
D = np.fromstring(" ".join([input() for q in range(Q)]), sep=" ", dtype=np.int64).reshape((Q, 2))
B = D[:, 0]
C = D[:, 1]

d = dict(np.c_[np.unique(A, return_counts=True)])
s = np.sum(A)
for q in range(Q):
    if(B[q] in d):
        n = d.pop(B[q])
        s += n * (C[q] - B[q])
        if(C[q] in d):
            d[C[q]] += n
        else:
            d[C[q]] = n
    print(s)
### BUGGY CODE ###

N = int(input())
A = list(map(int,input().split()))
MOD = 10**9 + 7
ans = 0

for i in range(60):
    count0 = 0
    count1 = 0
    for j in range(N):
        a = A[j]>>i & 1
        if a==0:
            count0 += 1
        else:
            count1 += 1
    tmp = 2**i
    n = count0 * count1
    tmp = tmp * n
    ans += tmp
    ans = ans % MOD
print(ans)

### FIXED CODE ###

import numpy as np
N = int(input())
A = np.array(list(map(int,input().split())))

MOD = 10**9 + 7
ans = 0

for i in range(60):
    a = (A >> i) & 1
    count0 = np.count_nonzero(a)
    count1 = N - count0
    tmp = 2**i
    n = count0 * count1
    tmp = tmp * n
    ans += tmp
    ans = ans % MOD
print(ans)
### BUGGY CODE ###

def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 10 ** 9 + 7
N = 10 ** 6  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)


n, a, b = map(int, input().split())
print(((2**n-1)%p - cmb(n, a, p) - cmb(n, b, p))%p)


### FIXED CODE ###

def cmb(n, r, p):
    r = min(r, n - r)
    res = 1
    for i in range(r):
        res *= pow(i+1, p-2, p)
        res *= n-i
        res %= p
    return res

p = 10 ** 9 + 7
n, a, b = map(int, input().split())
ans = pow(2, n, p) - 1
ans -= cmb(n, a, p) + cmb(n, b, p)
print(ans % p)
### BUGGY CODE ###

N,K,Q=map(int, input().split())

points=[K for i in range(N)]

for i in range(Q):
    temp=int(input())
    points=[j-1 for j in points]
    points[temp-1]+=1

for p in points:
    if p>0:
        print('Yes')
    else:
        print('No')

### FIXED CODE ###

import collections

N,K,Q=map(int, input().split())
List=[int(input()) for i in range(Q)]
cc=collections.Counter(List)

points=[K-Q for i in range(N)]
for j in range(N):
    points[j]+=cc[j+1]

for p in points:
    if p>0:
        print('Yes')
    else:
        print('No')
### BUGGY CODE ###

from sys import setrecursionlimit
setrecursionlimit(3*10**5)

N, Q = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N-1)]
PX = [tuple(map(int, input().split())) for _ in range(Q)]
T = [[] for _ in range(N+1)]
for a, b in AB:
    T[a].append(b)
    T[b].append(a)

C = [0 for _ in range(N+1)]
for p, x in PX:
    C[p] += x


def rec(v, p=0):
    for t in T[v]:
        if t == p:
            continue
        C[t] += C[v]
        rec(t, v)


rec(1)
print(*C[1:])

### FIXED CODE ###

from collections import deque

N, Q = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N-1)]
PX = [tuple(map(int, input().split())) for _ in range(Q)]
T = [[] for _ in range(N+1)]
for a, b in AB:
    T[a].append(b)
    T[b].append(a)

C = [0 for _ in range(N+1)]
for p, x in PX:
    C[p] += x

S = deque([(1, 0)])
while S:
    v, p = S.popleft()
    for t in T[v]:
        if t == p:
            continue
        C[t] += C[v]
        S.append((t, v))

print(*C[1:])
### BUGGY CODE ###

N, M = map(int,input().split())
edges = [[] for _ in range(N)]

for i in range(M):
	a, b = map(int,input().split())
	edges[a-1] += [b-1]
	edges[b-1] += [a-1]

res = False
for u in edges[0]:
	if u in edges[N-1]:
		res = True
		break

print("POSSIBLE" if res == True else "IMPOSSIBLE")

### FIXED CODE ###

N, M = map(int,input().split())

from1 = []
gotoN = []

for i in range(M):
	a, b = map(int,input().split())
	if a == 1:
		from1.append(b)
	if b == N:
		gotoN.append(a)

res = set(from1) & set(gotoN)

print("POSSIBLE" if len(res) > 0 else "IMPOSSIBLE")
### BUGGY CODE ###

N = int(input())

mult = 1
A = list(map(int, input().split()))
try:
    for a in A:
        mult *= a
        
    if mult > 10**18:
        print("-1")
    else:
        print(mult)
except:
    print("-1")

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
maxi = 10**18
def main():
    if 0 in A:
        print(0)
        return
    mult = 1
    for a in A:
        mult *= a
        if mult > maxi:
            print(-1)
            return 
    print(mult)

main()
### BUGGY CODE ###

#!/usr/bin/env python

import sys

s= sys.stdin.readline().strip()
n= len(s)

s0= [0]*n
for i in range(n):
    if s[i]=="L": s0[i]= i-1
    if s[i]=="R": s0[i]= i+1
#
table=[1,100,104,105,106,107,111,113,114,115,117,123,125,127,131,136,137,142,145,148,149,151,153,155,157,159,162,163,164,165,166,169,171,172,176,181,185,186,187,191,198,201,202,203,206,207,209,211,215,216,217,220,221,222,223,224,225,227,233,234,235,238,239,242,246,247,250,255,256,257,258,261,264,265,267,272,273,275,277,278,279,282,283,284,285,286,288,289,294,295,298,300,303,304,306,309,312,314,315,317,319,320,323,326,329,332]

now= [ i for i in range(n) ]
prev= s0
for i in range(1,333):
    nxt = [None]*n
    for j in range(n): nxt[j]= prev[prev[j]]
    if i in table: now= [ nxt[now[j]] for j in range(n) ]
    prev= nxt
#
ans= [0]*n
for i in range(n): ans[now[i]]+=1
    
for i in range(n): 
    print( ans[i],end=" " )


### FIXED CODE ###

import sys

s0=[]
for i,c in enumerate(sys.stdin.readline().strip()):
    if c=="L": s0.append(i-1)
    if c=="R": s0.append(i+1)
n= len(s0)
#
now= [ i for i in range(n) ]
prev= s0
for i in range(17):
    now = [ prev[i] for i in prev ]
    prev= now
#
ans= [0]*n
for i in range(n): ans[now[i]]+=1
for i in range(n): print( ans[i],end=" " )
### BUGGY CODE ###

n, k = list(map(int, input().split(" ")))
while True:
    a = abs(n-k)
    if n <= a:
        break
    else:
        n = a
print(n)

### FIXED CODE ###

n, k = list(map(int, input().split(" ")))
n = n - k * (n // k)
n_last = abs(n - k)
if n_last < n:
    n = n_last
print(n)