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

A = [0] * (5 * 10**5)

S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        exit()

ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1

print(ans)


### FIXED CODE ###

from collections import defaultdict
from bisect import bisect_right

def sol():
    S = input()
    T = input()

    setS = set(S)
    setT = set(T)
    for t in setT:
        if not t in setS:
            print('-1')
            return

    posS = defaultdict(list)

    for i, s in enumerate(S):
        posS[s].append(i)

    for s in enumerate(S):
        posS[s].sort()

    prev = -1
    ans = 0
    N = len(S)

    for t in T:
        next = bisect_right(posS[t], prev)
        if next >= len(posS[t]):
            ans += N - prev
            ans += posS[t][0]
            prev = posS[t][0]
        else:
            ans += posS[t][next] - prev
            prev = posS[t][next]

    print(ans)

sol()
### BUGGY CODE ###

n, c = map(int, raw_input().split())
A = map(int, raw_input().split())
B = map(int, raw_input().split())

MOD = 10**9+7

memo = [[0]*401 for i in xrange(401)]
for i in xrange(401):
    temp = 0
    for j in xrange(401):
        temp = (temp + pow(j, i, MOD)) % MOD
        memo[i][j] = temp

dp = {}
def dfs(i, r):
    if i==-1:
        return r==0
    if (i, r) in dp:
        return dp[i, r]
    res = 0
    for j in xrange(0, r+1):
        res = (res + (memo[j][B[i]]-memo[j][A[i]-1])*dfs(i-1, r-j)) % MOD
    dp[i, r] = res
    return res
print dfs(n-1, c)

### FIXED CODE ###

n, c = map(int, raw_input().split())
A = map(int, raw_input().split())
B = map(int, raw_input().split())

MOD = 10**9+7

memo = [[0]*401 for i in xrange(401)]
for i in xrange(401):
    temp = 0
    for j in xrange(401):
        temp = (temp + pow(j, i, MOD)) % MOD
        memo[j][i] = temp
S = [memo[A[i]-1] for i in xrange(n)]
T = [memo[B[i]] for i in xrange(n)]

dp = [[-1]*(c+1) for i in xrange(n)]
def dfs(i, r):
    if i==-1:
        return r==0
    if dp[i][r] != -1:
        return dp[i][r]
    res = 0
    s = S[i]; t = T[i]
    for j in xrange(0, r+1):
        res = (res + (t[j]-s[j])*dfs(i-1, r-j)) % MOD
    dp[i][r] = res
    return res
print dfs(n-1, c)
### BUGGY CODE ###

n, k = map(int, input().split())
a = list(map(int, input().split()))
befscore = 1
score = 1
f = True
for i in range(n):
    score *= a[i]

    if i >= k:
        score //= a[i - k]
        if befscore >= score:
            print("No")
        else:
            print("Yes")

    befscore = score


### FIXED CODE ###

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

a = list(map(lambda x: x - ma, a))

befscore = 1
score = 1
f = True
for i in range(n):
    score = score + a[i]

    if i >= k:
        score = int(score - a[i - k])
        if befscore >= score:
            print("No")
        else:
            print("Yes")

    befscore = score
### BUGGY CODE ###

S = list(input())
Q = int(input())
inversion_flg = False
# L = [input().split() for _ in range(Q)]

for i in range(Q):
    L = input().split()
    if L[0] == '1':
        if inversion_flg:
            inversion_flg = False
        else:
            inversion_flg = True
    if L[0] == '2':
        if not inversion_flg:
            if L[1] == '1':
                S = [L[2]] + S
            else:
                S = S + [L[2]]
        else:
            if L[1] == '2':
                S = [L[2]] + S
            else:
                S = S + [L[2]]



# print(L)
if not inversion_flg: 
    print(''.join(S))
else:
    S.reverse()
    print(''.join(S))

### FIXED CODE ###

s = input()
q = int(input())
query = []
lead = ''
end = ''
inversion_flg = False
 
for j in range(q):
    i = input().split()
    if i[0] == "1":
        if inversion_flg:
            inversion_flg = False 
        else:
            inversion_flg = True
    else:
        if inversion_flg:
            if i[1] == "1":
                i[1] = "2"
            else:
                i[1] = "1"
        if i[1] == "1":
            lead+=i[2]
        else:
            end+=i[2]
 
lead = lead[::-1]
s = lead+s+end
if inversion_flg:
    s = s[::-1]
print(s)
### BUGGY CODE ###

n, m = map(int, input().split())
l = [list(input().split()) for _ in range(m)]
subs = [[] for _ in range(n)]

for i in range(m):
    subs[int(l[i][0])-1].append(l[i][1])

ac, p = 0, 0
for j in range(n):
    for k in range(len(subs[j])):
        if subs[j][k] == 'AC':
            ac += 1
        
        if 'AC' in subs[j]:
            if subs[j][k] == 'WA':
                p += 1
            else:
                break

print(ac, p)

### FIXED CODE ###

n, m = map(int, input().split())
l = [tuple(input().split()) for _ in range(m)]
subs = [[] for _ in range(n)]

for i in range(m):
    subs[int(l[i][0])-1].append(l[i][1])

ac, p = 0, 0

for j in range(n):
    if 'AC' in subs[j]:
        ac += 1
        for k in range(len(subs[j])):
                if subs[j][k] == 'WA':
                    p += 1
                else:
                    break

print(ac, p)
### BUGGY CODE ###

K,S = map(int,input().split())
ans=0
for x in range(0,K+1):
    for y in range(0,K+1):
        for z in range(0,K+1):
            if x+y+z==S:
                ans+=1
print(ans)

### FIXED CODE ###

K,S = map(int,input().split())
ans=0
for x in range(0,K+1):
    for y in range(0,K+1):
        if 0<=S-x-y<=K:
            ans+=1
print(ans)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**9)
n,m=map(int,input().split())
root=[[] for i in range(n+1)]
for i in range(m):
    a,b=map(int,input().split())
    root[a].append(b)
    root[b].append(a)
dep=[-1]*(n+1)
sign=[0]*(n+1)
dep[0]=0
dep[1]=0
def search(x):
    for i in root[x]:
        if dep[i]==-1:
            dep[i]=dep[x]+1
            sign[i]=x
            search(i)
        elif dep[i]>dep[x]+1:
            dep[i]=dep[x]+1
            sign[i]=x
            search(i)
search(1)
print("Yes")
for i in range(2,n+1):
    print(sign[i])


### FIXED CODE ###

from collections import deque
n,m=map(int,input().split())
r=[[] for i in range(n+1)]
for i in range(m):
    a,b=map(int,input().split())
    r[a].append(b)
    r[b].append(a)
dep=[-1]*(n+1)
dep[0]=0
dep[1]=0
data=deque([1])
d=0
while len(data)>0:
    p=data.popleft()
    for i in r[p]:
        if dep[i]==-1:
            dep[i]=dep[p]+1
            data.append(i)

if not all(dep[i+1]>=0 for i in range(n)):
    print("No")
else:
    print("Yes")
    for i in range(2,n+1):
        for j in r[i]:
            if dep[j]==dep[i]-1:
                print(j)
                break
### BUGGY CODE ###

n,k = map(int,input().split())
a = [int(i) for i in input().split()]

from itertools import combinations
b = list(combinations(a, k))
ans = 0
for i in range(len(b)):
    ans += max(b[i]) - min(b[i])
ans %= pow(10, 9) + 7

print(ans)

### FIXED CODE ###

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

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

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

n,k = map(int,input().split())
a = [int(i) for i in input().split()]
a.sort()
if k > 1:
    ans = 0
    for i in range(k-1, n):
        maxs = a[i]
        ncr = cmb(i, k-1)
        #print(maxs, ncr, maxs * ncr)
        ans += maxs * ncr
        ans %= mod
    for i in range(n-k+1):
        mins = a[i]
        ncr = cmb(n-i-1, k-1)
        #print(mins, ncr, mins * ncr)
        ans -= mins * ncr
        ans %= mod
    print(ans)
else:
    print(0)
### BUGGY CODE ###

n,m=map(int,input().split())

par = [i for i in range(n+1)]
def find(x):
    if par[x] == x:
        return x
    else:
        par[x] = find(par[x])
        return par[x]
def same(x,y):
    return find(x) == find(y)
def unite(x,y):
    x = find(x)
    y = find(y)
    if x == y:
        return 0
    par[x] = y

L=[[0 for _ in range(n)] for _ in range(n)]
for _ in range(m):
    a,b=map(int,input().split())
    unite(a,b)

print(len(set(par))-2)

### FIXED CODE ###

n,m=map(int,input().split())
g = [[] for _ in range(n)]

for _ in range(m):
    a, b = map(int,input().split())
    g[a-1].append(b-1)
    g[b-1].append(a-1)

from collections import deque

d = [None] * n
def bfs(u):
    queue = deque([u])
    d[u] = 0
    while queue:
        v = queue.popleft()
        for i in g[v]:
            if d[i] is None:
                d[i] = d[v] + 1
                queue.append(i)
cnt=0
for i in range(n):
    if d[i] is None:
        cnt+=1
        bfs(i)
print(cnt-1)
### BUGGY CODE ###

N=int(input())
L=list(map(int, input().split()))
#a<b+c
#b<c+a
#c<a+b
#a<=b<=c
#c<a+b
L=sorted(L)
ans=0
for i in range(N-2):
    for j in range(i+1,N-1):
        for k in range(j+1,N):
            if L[k]>=L[j]+L[i]:
                break
            ans+=1
print(ans)

### FIXED CODE ###

import bisect

N=int(input())
L=list(map(int, input().split()))
L=sorted(L)

count=0
for i in range(N-2):
  a=L[i]
  for j in range(i+1, N-1):
    b=L[j]
    r=bisect.bisect_left(L, a+b)
    count+=r-1-j
    
print(count)
### BUGGY CODE ###

n, m = map(int, input().split())
lst = list(map(int, input().split()))
imod = []
for i in range(n):
    imod = imod + [sum(lst[0:i]) % m]
imod = imod + [sum(lst) % m]
s = 0
for i in range(n):
    for j in range(i+1,n+1):
        if imod[i] == imod[j]:
            s = s + 1
print(s)

### FIXED CODE ###

n, m = map(int, input().split())
lst = list(map(int, input().split()))
dp = {}
dp[0] = 1
a, s = 0, 0
for i in lst:
    a += i
    a %= m
    if a in dp:
        s += dp[a]
        dp[a] += 1
    else:
        dp[a] = 1
print(s)
### BUGGY CODE ###

N, K, Q = map(int, input().split())
A = []
result = [K] * N
answers = [0] * N

for _ in range(Q):
    A.append(int(input()) - 1)

for i in A:
    answers[i] += 1

for i, val in enumerate(answers):
    result = list(map(lambda x: x-val, result))
    result[i] += val

for i in list(map(lambda x: "Yes" if x > 0 else "No", result)):
    print(i)


### FIXED CODE ###

N, K, Q = map(int, input().split())
A = []
result = [K] * N
answers = [0] * N

for _ in range(Q):
    A.append(int(input()) - 1)


for i in A:
    answers[i] += 1

result = list(map(lambda x: x-Q, result))

for i in range(N):
    result[i] += answers[i]

for i in list(map(lambda x: "Yes" if x > 0 else "No", result)):
    print(i)
### BUGGY CODE ###

def BC133_E():
    n, k = map(int, input().split())
    d = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = map(int, input().split())
        d[a - 1].append(b - 1)
        d[b - 1].append(a - 1)
    stack = [(0, 0)]
    visited = set()
    res = []
    while stack:
        i, l = stack.pop(0)
        visited.add(i)
        c = len(set(d[i]) - visited)
        if c != 0:
            if l == len(res):
                res.append([c])
            else:
                res[l].append(c)
        for j in d[i]:
            if j not in visited: stack.append((j, l + 1))
    ans = k
    MOD = 10**9 + 7
    for i in range(len(res)):
        if i == 0:
            c = k - 1
            for m in range(res[0][0] - 1):
                c = ((k - m - 2) * c) % MOD
            ans = (ans * c) % MOD
        else:
            for j in res[i]:
                c = k - 2
                for m in range(j - 1):
                    c = ((k - m - 3) * c) % MOD
                ans = (ans * c) % MOD
    print(ans)


BC133_E()

### FIXED CODE ###

def BC133_E():
    n, k = map(int, input().split())
    d = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = map(int, input().split())
        d[a - 1].append(b - 1)
        d[b - 1].append(a - 1)
    stack = [(0, 0)]
    visited = set()
    res = []
    while stack:
        i, l = stack.pop(0)
        visited.add(i)
        c = len(set(d[i]) - visited)
        if c != 0:
            if l == len(res):
                res.append([c])
            else:
                res[l].append(c)
        for j in d[i]:
            if j not in visited: stack.append((j, l + 1))
    ans = k
    MOD = 10**9 + 7
    for i in range(len(res)):
        if i == 0:
            c = k - 1
            for m in range(res[0][0] - 1):
                c = ((k - m - 2) * c) % MOD
            ans = (ans * c) % MOD
        else:
            for j in res[i]:
                c = k - 2
                for m in range(j - 1):
                    c = ((k - m - 3) * c) % MOD
                ans = (ans * c) % MOD
    print(ans)


BC133_E()
### BUGGY CODE ###

import numpy as np

N = int(input())

for n in range(1, N + 1):
    cnt = 0
    th = int(np.sqrt(n)) + 1
    for x in range(1, th):
        thy = int(np.sqrt(n - x ** 2)) + 1
        for y in range(1, thy):
            thy = int(np.sqrt(n - x ** 2 - y ** 2)) + 1
            for z in range(1, th):
                if x**2 + y**2 + z**2 + x*y + y*z + z*x == n:
                    cnt += 1
    print(cnt)

### FIXED CODE ###

import numpy as np

N = int(input())
res = np.zeros(N+1)

th = int(np.sqrt(N)) + 1
for x in range(1, th):
    for y in range(1, x + 1):
        for z in range(1, y + 1):
            n = x ** 2 + y ** 2 + z ** 2 + x * y + y * z + z * x
            if n > N:
                continue
            if x == y and y == z:
                res[n] += 1
            elif x == y or y == z or z == x:
                res[n] += 3
            else:
                res[n] += 6
for i in range(1, N+1):
    print(int(res[i]))
### BUGGY CODE ###

from collections import deque

H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]

def next_positions(current_position):
    output = []
    for udrl in [[0,1],[1,0]]:
        y = current_position[0]+udrl[0]
        x = current_position[1]+udrl[1]
        if 0 <= y <= H-1 and 0 <= x <= W-1:
            output.append([y,x,s[y][x]])
    if len(output) == 2:
        if output[0][2] == current_position[2] and output[1][2] != current_position[2]:
            del output[1]
        elif output[0][2] != current_position[2] and output[1][2] == current_position[2]:
            del output[0]
    return output

d = deque([[0,0,s[0][0]]])
distance = [[float("inf")]*W for _ in range(H)]
if s[0][0] == ".":
    distance[0][0] = 0
else:
    distance[0][0] = 1

while d:
    current_position = d.popleft()
    for q in next_positions(current_position):
        if s[int(q[0])][int(q[1])] == s[0][0] and current_position[2] != s[0][0]:
            distance[int(q[0])][int(q[1])] = min(distance[int(q[0])][int(q[1])], distance[current_position[0]][current_position[1]] + 1)
        else:
            distance[int(q[0])][int(q[1])] = min(distance[int(q[0])][int(q[1])], distance[current_position[0]][current_position[1]])
        d.append(q)

print(distance[H-1][W-1])

### FIXED CODE ###

from collections import deque

H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]

d = deque([[0,0,s[0][0]]])
distance = [[float("inf")]*W for _ in range(H)]
if s[0][0] == ".":
    distance[0][0] = 0
else:
    distance[0][0] = 1

while d:
    y, x, c = d.popleft()
    for new_y, new_x in ([int(y)+1, int(x)], [int(y), int(x)+1]):
        if 0 <= new_y <= H-1 and 0 <= new_x <= W-1:
            if c == "." and s[new_y][new_x] == "#":
                new_cost = distance[y][x] + 1
            else:
                new_cost = distance[y][x]
            if new_cost < distance[new_y][new_x]:
                distance[new_y][new_x] = new_cost
                d.append([new_y, new_x, s[new_y][new_x]])

print(distance[H-1][W-1])
### BUGGY CODE ###

# 

N,K = map(int,input().split())
p = list(map(int,input().split()))
ex=[]

ans=[]

for i in p:
    ex.append((1+i)/2)

for i in range(N-K+1):
    ans.append(sum(ex[i:i+K]))
    
print(max(ans))





### FIXED CODE ###

# ruisekiwa by itertools

from itertools import accumulate

N,K = map(int,input().split())
p = list(map(int,input().split()))

b1=[0]+list(accumulate(p))

ans=[]

for i in range(N-K+1):
    ans.append(b1[K+i]-b1[i])
    
print((max(ans)+K)/2)
### BUGGY CODE ###

R, G, B, n = map(int, input().split())
ans = 0
for r in range(n+1):
    for g in range(n+1):
        v = r*R + g*G
        if n >= v and (n-v) % B == 0:
            ans += 1
print(ans)


### FIXED CODE ###

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

def main():
    from fractions import gcd

    MOD = 10 ** 9 + 7

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

    tmp = 1
    for a in A:
        tmp *= a // gcd(tmp, a)

    ans = 0
    for a in A:
        ans += (tmp // a) % MOD
        ans %= MOD

    print (ans)

if __name__ == '__main__':
    main()

### FIXED CODE ###

def main():
    from fractions import gcd

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

    MOD = 10 ** 9 + 7

    tmp = 1
    for a in A:
        tmp *= a // gcd(tmp, a)

    ans = 0
    for a in A:
        ans += tmp // a
    ans %= MOD

    print (ans)

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

n, m = map(int, input().split(' '))
s = [list(map(int, input().split(' '))) for _ in range(m)]

dic = [x for x in range(n)]
result = [int(n*(n-1)/2)]
for i in range(1,m):
    if dic[s[-1*i][0]-1] == dic[s[-1*i][1]-1]:
        result.append(result[-1])
    else:
        p = [x for x in dic if x == dic[s[-1*i][0]-1]]
        q = [x for x in dic if x == dic[s[-1*i][1]-1]]
        result.append(result[-1]-len(p)*len(q))
        if len(p) < len(q):
            dic = [dic[s[-1*i][1]-1] if x == dic[s[-1*i][0]-1] else x for x in dic]
        else:
            dic = [dic[s[-1*i][0]-1] if x == dic[s[-1*i][1]-1] else x for x in dic]
for i in range(1,m+1):
    print(result[-1*i])

### FIXED CODE ###

n, m = map(int, input().split(' '))
a = [0]*m
b = [0]*m
for i in range(m):
    a[i], b[i] = list(map(int, input().split(' ')))

c = [-1]*(m+2)

def find(num):
    if c[num] < 0:
        return num
    else:
        k = find(c[num])
        c[num] = k
        return k

result = [int(n*(n-1)/2)]
for i in range(m-1, 0,-1):
    x = find(a[i])
    y = find(b[i])

    if x != y:
        result.append(result[-1]-c[x]*c[y])
        x, y = sorted([x,y])
        c[x] += c[y]
        c[y] = x
    else:
        result.append(result[-1])

for i in result[::-1]:
    print(int(i))
### BUGGY CODE ###

from fractions import gcd

k=int(input())
ans=0
for i in range(1,k+1):
  for j in range(1,k+1):
    for k in range(1,k+1):
      ans+=gcd(i,gcd(j,k))
print(ans)

### FIXED CODE ###

from math import gcd

k=int(input())
ans=0
for i in range(1,k+1):
  for j in range(1,k+1):
    for g in range(1,k+1):
      ans += gcd(gcd(i,j), g)

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

s = input()
t = input()

ls = len(s)
lt = len(t)

dp = [[0 for _ in range(lt + 1)] for __ in range(ls + 1)]

for ns in range(ls):
    for nt in range(lt):
        if s[ns] == t[nt]:
            dp[ns + 1][nt + 1] = max(dp[ns][nt] + 1, dp[ns + 1][nt + 1])
        dp[ns + 1][nt + 1] = max(dp[ns + 1][nt + 1], dp[ns + 1][nt], dp[ns][nt + 1])

ans = ''
i, j = ls, lt
while i > 0 and j > 0:
    if dp[i][j] == dp[i - 1][j]:
        i -= 1
    elif dp[i][j] == dp[i][j - 1]:
        j -= 1
    else:
        ans = s[i - 1] + ans
        i -= 1
        j -= 1
print(ans)


### FIXED CODE ###

s = input()
t = input()

ls = len(s)
lt = len(t)

dp = [[0 for _ in range(lt + 1)] for __ in range(ls + 1)]

for ns in range(ls):
    for nt in range(lt):
        if s[ns] == t[nt]:
            dp[ns + 1][nt + 1] = max(dp[ns][nt] + 1, dp[ns + 1][nt + 1])
        dp[ns + 1][nt + 1] = max(dp[ns + 1][nt + 1], dp[ns + 1][nt], dp[ns][nt + 1])

ans = ''
i, j = ls, lt
while i > 0 and j > 0:
    if dp[i][j] == dp[i - 1][j]:
        i -= 1
    elif dp[i][j] == dp[i][j - 1]:
        j -= 1
    else:
        ans = s[i - 1] + ans
        i -= 1
        j -= 1
print(ans)
### BUGGY CODE ###

import queue
h,w=map(int,input().split())
maze=[]
def func(n):
    if n=="#":
        return -1
    else:
        return 0


for i in range(h):
    row=input()
    maze.append(row)
#print(maze)
root=[[-1 for i in range(h*w)] for i in range(h*w)]
for i in range(h):
    for j in range(w):
       # print(maze[i][j])
        if maze[i][j]==".":
            now=w*i+j
            hidari=w*i+j-1
            migi=w*i+j+1
            ue=w*(i-1)+j
            sita=w*(i+1)+j
            if j>0:
                if maze[i][j-1]!="#":
                    root[now][hidari]=0
            if i+1<h:
                if maze[i+1][j]!="#":
                    root[now][sita]=0
            if j+1<w:
                if maze[i][j+1]!="#":
                    root[now][migi]=0
            if 0<i:
                if maze[i-1][j]!="#":
                    root[now][ue]=0
        else:
            now=w*i+j
            now=w*i+j
            hidari=w*i+j-1
            migi=w*i+j+1
            ue=w*(i-1)+j
            sita=w*(i+1)+j
            if j>0:
                root[now][hidari]=-1
            if i+1<h:
                root[now][sita]=-1
            if j+1<w:
                root[now][migi]=-1
            if 0<i:
                root[now][ue]=-1
#print(root)
max=0
for start in range(h*w):
    dist=[-1]*(h*w)
    now=start
    dist[start]=0
    visted=[0]*(h*w)
    toVis=queue.Queue()
    visted[now]=1
    for i in range(h*w):
        if root[now][i]!=-1:
            toVis.put(i)
            dist[i]=dist[now]+1
    while toVis.empty()!=True:
        now=toVis.get()
        visted[now]=1
        for i in range(h*w):
            if root[now][i]!=-1 and visted[i]!=1:
                toVis.put(i)
                dist[i]=dist[now]+1
    #print("s:",start,"g:",goal,"len",dist[goal])
    farest=dist[now]
    if max<farest:
        max=farest
print(max)
        

### FIXED CODE ###

h,w=map(int,input().split())
n=h*w
inf=10**6
field=[]
for i in range(h):
    field.append(input())
dist=[[inf]*n for i in range(n)]
for i in range(h):
    for j in range(w):
        if field[i][j]=="#":continue
        if j+1<w and field[i][j+1]!="#":
            dist[i*w+j][i*w+j+1]=1
            dist[i*w+j+1][i*w+j]=1
        if i+1<h and field[i+1][j]!="#":
            dist[i*w+j][(i+1)*w+j]=1
            dist[(i+1)*w+j][i*w+j]=1
for i in range(n):
    for j in range(n):
        for k in range(n):
            dist[j][k]=min(dist[j][k],dist[j][i]+dist[i][k])
ans=0
for i in range(n):
    for j in range(n):
        if i==j:
            continue
        if dist[i][j]==inf:
            continue
        ans=max(ans,dist[i][j])
print(ans)
### BUGGY CODE ###

x,y,z,k = map(int,input().split())
A = list(map(int , input().split()))
B = list(map(int , input().split()))
C = list(map(int , input().split()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True) 
AB = []
ABC = []
for i in A:
        for j in B:
                AB.append(i+j)
AB.sort(reverse = True)
AB = AB[:min(3001,x*y*z+1)]
for i in AB:
        for j in C:
                ABC.append(i+j)
ABC.sort(reverse = True)
for i in range(k):
        print(ABC[i])

### FIXED CODE ###

x,y,z,k = map(int,input().split())
lisa = list(map(int,input().split()))
lisb = list(map(int,input().split()))
lisc = list(map(int,input().split()))
lisa.sort(reverse=True)
lisb.sort(reverse=True)
lisc.sort(reverse=True)
an = []
ans = []
for num in lisb:
    for nu in lisa:
        an.append(num+nu)
an.sort(reverse=True)
an = an[:min(3001,x*y*z+1)]
for num in lisc:
    for nu in an:
        ans.append(num+nu)
ans.sort(reverse=True)
for i in range(k):
    print(ans[i])
### BUGGY CODE ###

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

while n > k:
    n = n-k
if n < k-n:
    print(n)
else:
    print(k-n)

### FIXED CODE ###

n, k = map(int, input().split())
if n < k:
    if n < k-n:
        print(n)
    else:
        print(k-n)
else:
    n = n%k
    if n < k-n:
        print(n)
    else:
        print(k-n)