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

def int_raw():
    return int(input())

def ss_raw():
    return input().split()

def ints_raw():
    return list(map(int,ss_raw()))

X,Y,Z,K = ints_raw()

A = ints_raw()
B= ints_raw()
C= ints_raw()

arg_buf = {}
def main():
    D = sorted([a+b for a in A for b in B],reverse=True)[:K]
    E = [d+c for d in D for c in C]
    E.sort(reverse=True)
    for k in range(K):
        print(E[k])

main()


### FIXED CODE ###

from heapq import *

def int_raw():
    return int(input())

def ss_raw():
    return input().split()

def ints_raw():
    return list(map(int,ss_raw()))

X,Y,Z,K = ints_raw()

A = ints_raw()
B= ints_raw()
C= ints_raw()
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

arg_hash = {}

def main():
    Q = []
    heappush(Q,(-A[0]-B[0]-C[0],0,0,0))
    arg_hash[(0,0,0)]=1
    for k in range(K):
        ans = heappop(Q)
        print(-ans[0])
        a,b,c = ans[1:]

        argA = (a+1,b,c)
        if a < X-1 and (not argA in arg_hash):
            arg_hash[argA]=1
            heappush(Q,(-A[a+1]-B[b]-C[c],)+argA)
        argB = (a,b+1,c)
        if b < Y-1 and (not argB in arg_hash):
            arg_hash[argB]=1
            heappush(Q, (-A[a]-B[b+1]-C[c],)+argB)
        argC = (a,b,c+1)
        if c < Z-1 and (not argC in arg_hash):
            arg_hash[argC]=1
            heappush(Q,(-A[a]-B[b]-C[c+1],)+argC)
        
main()
### BUGGY CODE ###

n,m = map(int,input().split())
dp = [2**63]*4096
dp[0] = 0
for i in range(m):
  a,b = map(int,input().split())
  mask = 0
  c = list(map(int,input().split()))
  for i in c:
    mask |= ( 1 << i-1 )
  nxtdp = dp[:]
  for j in range(1 << n):
    if (j | mask) != j: continue
    for k in range(1 << n):
      if j == (k | mask):
        nxtdp[j] = min(nxtdp[j], dp[k] + a)
  dp = nxtdp
print(dp[(1<<n)-1] if dp[(1<<n)-1] < 2**63 else -1)

### FIXED CODE ###

n,m = map(int,input().split())
dp = [2**63]*4096
dp[0] = 0
for i in range(m):
  a,b = map(int,input().split())
  mask = 0
  c = list(map(int,input().split()))
  for i in c:
    mask |= ( 1 << i-1 )
  nxtdp = dp[:]
  for j in range(1 << n):
    if dp[j] < 2 ** 63:
      nxtdp[j | mask] = min(nxtdp[j | mask], dp[j] + a)
  dp = nxtdp
print(dp[(1<<n)-1] if dp[(1<<n)-1] < 2**63 else -1)
### BUGGY CODE ###

n, m = map(int, input().split())
a = list(map(int, input().split()))
l = []
all_set = a
for _ in range(m):
#    l.append(list(map(int, input().split())))
    bi, ci = map(int, input().split())
    for _ in range(bi):
        all_set.append(ci)
    
print(sum(sorted(all_set, reverse=True)[:n]))

### FIXED CODE ###

# coding: utf-8
# Your code here!

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

l = []
for _ in range(m):
    l.append(list(map(int, input().split())))
    
l = sorted(l, key=lambda x:x[1], reverse=True)

top_n = []
flag = False
for bi, ci in l:
    for _ in range(bi):
        if len(top_n) < n:
            top_n.append(ci)
        else:
            flag = True
    if flag:
        break
a.extend(top_n)

print(sum(sorted(a, reverse=True)[:n]))
### BUGGY CODE ###

n = int(input())
s = input()
d = []  # l,r詰める
sum = []

for i in range(n):
    left = 0
    right = 0
    for j in range(n):
        if j == i:
            continue
        elif j < i and s[j] == 'W':
            left += 1
        elif j > i and s[j] == 'E':
            right += 1
            
    d.append([left, right])
    sum.append(left+right)

print(min(sum))    


### FIXED CODE ###

n = int(input())
s = input()
d = []

num_first = 0
for i in range(1, n):
    if s[i] == 'E':
        num_first += 1

d.append(num_first)

for i in range(1, n):
    tmp = 0
    if s[i-1] == 'W':
        tmp += 1
    if s[i] == 'E':
        tmp -= 1
    d.append(d[i-1] + tmp)

print(min(d))
### BUGGY CODE ###

INF = 10**10

def f(x, sign, x_min, x_max, t): 
    x += sign * t 
    x_min = min(x_min, x)
    x_max = max(x_max, x)
    return x_min, x_max

def g(n, x, y, d, t): 
    x_min, x_max = max(x), min(x)
    y_min, y_max = max(y), min(y)
    for i in range(n):
        if d[i] == "R":
            x_sign = 1 
        elif d[i] == "L":
            x_sign = -1
        else:
            x_sign = 0 
        if d[i] == "U":
            y_sign = 1 
        elif d[i] == "D":
            y_sign = -1
        else:
            y_sign = 0 
        x_min, x_max = f(x[i], x_sign, x_min, x_max, t)
        y_min, y_max = f(y[i], y_sign, y_min, y_max, t)
    h = max(0, (y_max - y_min))
    w = max(0, (x_max - x_min))
    return h * w 

def solve(n, x, y, d): 
    lb = 0 
    ub = 10**10
    h = lambda t: g(n, x, y, d, t)
    while abs(ub-lb) > 1e-9:
        t1 = (2*lb + ub) / 3 
        t2 = (lb + 2*ub) / 3 
        if h(t1) < h(t2):
            ub = t2
        else:
            lb = t1
    return (h(t1) + h(t2)) / 2 

n = int(input())
x, y, d = [-1]*n, [-1]*n, [-1]*n
for i in range(n):
    x[i], y[i], d[i] = input().split()
    x[i] = int(x[i])
    y[i] = int(y[i])
print(solve(n, x, y, d)) 

### FIXED CODE ###

import numpy as np
from scipy.optimize import fmin

INF = 10**18

mapping = {
    "R": (1, 0),
    "L": (-1, 0),
    "U": (0, 1),
    "D": (0, -1)
}

def f(x, vx, t):
    if t < 0:
        return INF
    nx = x + vx * t
    return nx.max() - nx.min()

def solve(n, x, y, d):
    x = np.asarray(x, dtype=np.float)
    y = np.asarray(y, dtype=np.float)
    vx = np.zeros(n, dtype=np.float)
    vy = np.zeros(n, dtype=np.float)
    for i in range(n):
        vx[i], vy[i] = mapping[d[i]]
    h = lambda t: f(y, vy, t) * f(x, vx, t)
    t = fmin(h, x0=10**8, ftol=1e-9, disp=False)[0]
    return h(t)

n = int(input())
x, y, d = [-1]*n, [-1]*n, [-1]*n
for i in range(n):
    x[i], y[i], d[i] = input().split()
    x[i] = int(x[i])
    y[i] = int(y[i])
print(solve(n, x, y, d))
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**9)
 
def dfs(now, prev):
    ans[now]+=(h[now]+ans[prev])
    # 今いる頂点から行ける頂点を順に next に入れてループ
    [dfs(next, now) for next in g[now] if ((next != prev) and (not memo[next]))]
 
#木構造の取り込み 
n, q = map(int, input().split())
g = [[] for _ in range(n)]
for i in range(n-1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    g[u].append(v)
    g[v].append(u)

#操作の取り込み
h=[0]*n
for i in range(q):
    u, v = map(int, input().split())
    u -= 1
    h[u]+=v

#初期のカウンター
ans=[0]*n
memo = [False for i in range(n)]

#DFSでカウンターを動かす
dfs(0,0)
 
print(' '.join(map(str, ans)))


### FIXED CODE ###

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)
 
def dfs(now, prev):
    ans[now]+=(h[now]+ans[prev])
    # 今いる頂点から行ける頂点を順に next に入れてループ
    for next in g[now]:
      	if next == prev:
        	continue
      	else:
          dfs(next, now)
 
#木構造の取り込み 
n, q = map(int, input().split())
g = [[] for _ in range(n)]
for i in range(n-1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    g[u].append(v)
    g[v].append(u)

#操作の取り込み
h=[0]*n
for i in range(q):
    u, v = map(int, input().split())
    u -= 1
    h[u]+=v

#初期のカウンター
ans=[0]*n

#DFSでカウンターを動かす
dfs(0,0)
 
print(' '.join(map(str, ans)))
### BUGGY CODE ###

if(__name__ =="__main__"):
    N = int(input())
    a =[int(i) for i in input().split()]
    b = []
    for i in range(N):
        b.append(a[i])
        #print(b)
        b = b[::-1]
        #print(b)
    print(' '.join(map(str,b)))

### FIXED CODE ###

if(__name__ =="__main__"):
    N = int(input())
    a =[int(i) for i in input().split()]
    b = []
    for i in range(N-1,-1,-2):
        b.append(a.pop(i))
        #print(b)
        # b = b[::-1] TLE:O(N)    
        #print(b)
    b.extend(a)
    print(' '.join(map(str,b)))
### BUGGY CODE ###

L, R = map(int,input().split())

list =[]
for i in range(L,R,1):
  mod_i = i%2019
  for j in range(i+1,R+1,1):
    mod_j = j%2019
    mod = (mod_i *mod_j)%2019
    list.append(mod)
    
print(min(list))

### FIXED CODE ###

L, R = map(int,input().split())
L_mod = L%2019
if L_mod ==0:
  print(0)
else:
  R_mod = R%2019
  if R_mod ==0:
    print(0)
  else:
    if L_mod<R_mod:
      Ls = L//2019
      Rs = R//2019
      if Rs-Ls ==0:
        list =[]
        for i in range(L_mod,R_mod,1):
          mod_i = i%2019
          for j in range(i+1,R_mod+1,1):
            mod_j = j%2019
            mod = (mod_i *mod_j)%2019
            list.append(mod)

        print(min(list))
      else:
        print(0)
    else:
      print(0)
### BUGGY CODE ###

from collections import deque,Counter

N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()

T = T.translate(str.maketrans({'r': 'p', 's': 'r','p': 's'}))
cnt = Counter(T)
dic = {"r":R, "s":S, "p":P}

ans = cnt["r"]*R + cnt["s"]*S +cnt["p"]*P
checked = []
for i in range(len(T)):
    if i%K in checked:
        continue
    else:
        j = i
        tmp = 0
        while j <len(T):
            if tmp == T[j]:
                ans -= dic[T[j]]
                tmp = 0
            else:
                tmp = T[j]
            j += K
        checked.append(i%K)

print(ans)



### FIXED CODE ###

from collections import deque,Counter

N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()

T = T.translate(str.maketrans({'r': 'p', 's': 'r','p': 's'}))
cnt = Counter(T)
dic = {"r":R, "s":S, "p":P}

ans = cnt["r"]*R + cnt["s"]*S +cnt["p"]*P
checked = []
for i in range(N-K+1):
    if i%K in checked:
        continue
    else:
        j = i
        tmp = 0
        while j <len(T):
            if tmp == T[j]:
                ans -= dic[T[j]]
                tmp = 0
            else:
                tmp = T[j]
            j += K
        checked.append(i%K)

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

N = int(input())
a = list(map(int, input().split()))
l1 = [1] * N
a1 = [_ + 1 for _ in a]
a2 = [_ - 1 for _ in a]
lst = a + a1 + a2
set_lst = list(set(lst))
ans = 0

if len(lst) == len(set_lst):
    print(1)
else:
    for i in set_lst:
        tmp = lst.count(i)
        if tmp > ans:
            ans = tmp
    print(ans)

### FIXED CODE ###

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

cnt = [0] * 1000010
for i in a:
    cnt[i] += 1
    cnt[i+1] += 1
    cnt[i-1] += 1

print(max(cnt))
### BUGGY CODE ###

N, M=map(int, input().split())
A=[]
Y=[[] for _ in range(N)] #県
for i in range(M):
  p,y=map(int, input().split())
  A.append((p,y))
  Y[p-1].append(y)
  
for i in range(M):
  p,y=A[i]
  y=str(sorted(Y[p-1]).index(y)+1)
  p=str(p)
  ans="0"*(6-len(p))+p+"0"*(6-len(y))+y
  print(ans)

### FIXED CODE ###


N, M=map(int, input().split())
#A=[]
Y=[[] for _ in range(N)] #県
for i in range(M):
  p,y=map(int, input().split())
  #A.append((p,y))
  Y[p-1].append([y,i+1])

for i in range(N):
  if Y[i]:
    Y[i].sort(key=lambda x: x[0])
#indexは時間かかる
ans=[]
for i in range(N):
  for j,L in enumerate(Y[i]):
    p,y,n=i+1, L[0],L[1]
    ans.append((n,p,j+1))
ans=sorted(ans)
for n,p,y in ans:
  a=(6-len(str(p)))*"0"+str(p)+(6-len(str(y)))*"0"+str(y)
  print(a)
### BUGGY CODE ###

N = int(input())
TA = [[int(x) for x in input().split()]for _ in range(N)]
t = 1
a = 1

for i in TA:
    j = 1
    while(t>i[0]*j or a>i[1]*j):
        j+=1
    t = i[0]*j
    a = i[1]*j

print(t+a)


### FIXED CODE ###

N = int(input())
TA = [[int(x) for x in input().split()]for _ in range(N)]
t = 1
a = 1

for i in TA:
    j = max(t//i[0],a//i[1])
    if(t>i[0]*j or a>i[1]*j):
        j+=1
    t = i[0]*j
    a = i[1]*j

print(t+a)
### BUGGY CODE ###

N, K, Q = map(int, input().split())
aList = []
for i in range(Q):
    aList.append(int(input()))

ptsList = []
handy = Q - K
for i in range(N):
    ptsList.append(0)
if K - Q > 0:
    for i in range(N):
        print("Yes")
else:
    for i, Ai in enumerate(aList):
        AiPts = ptsList[Ai-1]
        AiPts += 1
        ptsList.pop(Ai-1)
        ptsList.insert(Ai-1, AiPts)

    for i, e in enumerate(ptsList):
        if e > handy:
            print("Yes")
        else:
            print("No")


### FIXED CODE ###

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

ptsList = [K-Q]*N

if K - Q > 0:
    for i in range(N):
        print("Yes")
else:
    for i in range(Q):
        ptsList[int(input())-1] += 1

    for i, e in enumerate(ptsList):
        if e > 0:
            print("Yes")
        else:
            print("No")
### BUGGY CODE ###

N = int(input())
data = [int(input()) for i in range(N)]
i = 0
a_i = None
res = 1
pre_i = 0
while data[i] != 2:
    a_i = data[i]
    if pre_i == a_i:
        res = -1
        break

    pre_i = i + 1
    i = data[i] -1
    res += 1
print(res)



### FIXED CODE ###

N = int(input())
data = [int(input()) for i in range(N)]
i = 0
a_i = None
res = 1
pre_i = 0
time = -1
while data[i] != 2:
    a_i = data[i]
    if pre_i == a_i or time == N:
        res = -1
        break

    pre_i = i + 1
    i = data[i] -1
    time += 1
    res += 1
print(res)
### BUGGY CODE ###

import numpy as np

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

a= np.array(a)

def light(a_list):
    outcome= np.array([0]*n)
    for i in range(n):
        outcome[max(0,i-a_list[i]):min(n,i+a_list[i]+1)] += 1
    return outcome

for _ in range(k):
    if sum(a) >= n*n:
        break
    a = light(a)

[print(aa,end=' ') for aa in a]

### FIXED CODE ###

import numpy as np
from numba import njit

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

@njit(cache=True)
def light(n,a_list):
    outcome= np.zeros(n+1, dtype=np.int64)
    for i in range(n):
        outcome[max(0,i-a_list[i])] += 1
        outcome[min(n,i+a_list[i]+1)] -= 1
    outcome = np.cumsum(outcome)[:-1]
    return outcome

for _ in range(k):
    if min(a) == n:
        break
    a = light(n,a)

[print(aa,end=' ') for aa in a]
### BUGGY CODE ###

N = int(input())
A = [int(input()) for _ in range(N)]

for i in range(N):
  d = A[i]
  A[i] = 0
  print(max(A))
  A[i] = d

### FIXED CODE ###

N = int(input())
A = [int(input()) for _ in range(N)]
B = sorted(A)
for a in A:
  if a == B[-1]:
    print(B[-2])
  else:
    print(B[-1])
### BUGGY CODE ###

import numpy as np

def snuke_coloring(h, w, poses):
    counts = np.zeros(10, np.int64)
    searched = {}

    for pos in poses:
        for y in range(max(1, pos[0] - 2), min(h - 2, pos[0]) + 1):
            for x in range(max(1, pos[1] - 2), min(w - 2, pos[1]) + 1):
                index = (y - 1) * w + (x - 1)
                if index in searched: continue
                searched[index] = True

                count = 0
                for target in poses:
                    if x <= target[1] and target[1] <= x + 2 and \
                       y <= target[0] and target[0] <= y + 2:
                       count += 1
                counts[count] += 1

    nonzero_count = int(np.sum(counts))
    counts[0] = (h - 2) * (w - 2) - nonzero_count
    for v in counts: print(v)

h, w, n = map(int, input().split())
poses = [tuple(map(int, input().split())) for _ in range(n)]
snuke_coloring(h, w, poses)

### FIXED CODE ###

import numpy as np

def snuke_coloring(h, w, poses):
    counts = np.zeros(10, np.int64)
    searched = {}

    for pos in poses:
        for y in range(max(1, pos[0] - 2), min(h - 2, pos[0]) + 1):
            for x in range(max(1, pos[1] - 2), min(w - 2, pos[1]) + 1):
                index = y * w + x
                if index in searched:
                    searched[index] = searched[index] + 1
                else:
                    searched[index] = 1

    for v in searched.values():
        counts[v] += 1

    nonzero_count = int(np.sum(counts))
    counts[0] = (h - 2) * (w - 2) - nonzero_count
    for v in counts: print(v)

h, w, n = map(int, input().split())
poses = [tuple(map(int, input().split())) for _ in range(n)]
snuke_coloring(h, w, poses)
### BUGGY CODE ###

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

### FIXED CODE ###

R,G,B,N = map(int,input().split())
ans = 0
for r in range(N+1):
  if r*R>N:
    break
  for g in range(N+1):
    S = N-r*R-g*G
    if S<0:
      break
    elif S%B==0:
      ans+=1
print(ans)
### BUGGY CODE ###

k = int(input())

if str(k)[-1] not in ["1","3","7","9"]:
    print(-1)
    exit()

tmp = "7"*(len(str(k))-1)
while True:
    tmp += "7"
    if int(tmp)%k == 0:
        print(len(tmp))
        exit()

### FIXED CODE ###

k = int(input())

if str(k)[-1] not in ["1","3","7","9"]:
    print(-1)
    exit()

tmp = 7%k
i = 1
while True:
    if tmp == 0:
        print(i)
        exit()
    tmp = (tmp*10 + 7)%k
    i += 1
### BUGGY CODE ###

k, s = map(int,input().split(' '))
count = 0
for x in range(k+1):
    for y in range(k+1):
        for z in range(k+1):
            if x + y + z == s:
                count += 1
print(count)

### FIXED CODE ###

k, s = map(int,input().split(' '))
count = 0
for x in range(k+1):
    for y in range(k+1):
        z = s - (x+y)
        if 0 <= z and z <= k:
            count += 1

print(count)
### BUGGY CODE ###

H = int(input())

cnt = H
B = [H]
for i in range(100000000000):
    cnt = cnt//2
    B.append(cnt)
    if cnt == 1:
        break

C =[1]
for i in range(1,100):
    C.append(C[i-1]*2)

ans= 0
for i in range(len(B)):
    ans +=C[i]
print(ans)



### FIXED CODE ###

H = int(input())
cnt = H
ans = 0
B = [H]

if H == 1:
    ans == 'NA'
else:
    for i in range(100000000000):
        cnt = cnt//2
        B.append(cnt)
        if cnt == 1:
            break

C =[1]
for i in range(1,1001):
    C.append(C[i-1]*2)

if ans == 'NA':
    print('0')
else:
    for i in range(len(B)):
        ans +=C[i]
print(ans)
### BUGGY CODE ###

x,y,z,k=map(int,input().split())
a,b,c=[list(map(int,input().split())) for i in range(3)]
l=[i+j for i in a for j in b]
l.sort(reverse=True)
maxl=[i+j for i in l[:k] for j in c]
maxl.sort(reverse=True)
for a in maxl[:k]:
    print(a)

### FIXED CODE ###

x,y,z,k=map(int,input().split())
a,b,c=[sorted(list(map(int,input().split())),reverse=True) for i in range(3)]
L=[]
for i in range(x):
    for j in range(y):
        if (i+1)*(j+1)>k:
            break
        for m in range(z):
            if (i+1)*(j+1)*(m+1)>k:
                break
            else:
                L.append(a[i]+b[j]+c[m])
for i in sorted(L,reverse=True)[:k]:
    print(i)
### BUGGY CODE ###

K, S = map(int, input().split())

result = 0

for x in range(K + 1):
    for y in range(K + 1):
        for z in range(K + 1):
            if x + y + z == S:
                result += 1

print(result)

### FIXED CODE ###

K, S = map(int, input().split())

result = 0

for x in range(K + 1):
    for y in range(K + 1):
        z = S - x - y
        if 0 <= z <= K:
            result += 1

print(result)
### BUGGY CODE ###

import fractions
from functools import reduce
import copy
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

n=int(input())
a=[int(x) for x in input().split()]

tmp=0
for i in range(n):
    l=copy.copy(a)
    _=l.pop(i)
    gcd=gcd_list(l)
    if gcd>tmp:
        tmp=gcd
print(tmp)

### FIXED CODE ###

import fractions
from functools import reduce
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

n=int(input())
a=[int(x) for x in input().split()]
l=[0]
r=[0]
for i in range(n):
    l.append(fractions.gcd(l[i],a[i]))
    r.append(fractions.gcd(r[i],a[(i+1)*-1]))
r = r[::-1]
m = []
for i in range(n):
    m.append(fractions.gcd(l[i],r[i+1]))
print(max(m))
### BUGGY CODE ###

import sys
sys.setrecursionlimit(100000000)

n = int(input())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))

rates_list = [[],[]]
for i in range(3):
	if aa[i] > bb[i]:
		rates_list[1].append([bb[i], aa[i]])
	else:
		rates_list[0].append([aa[i], bb[i]])

max_d = n
for j in range(2):
	rates = rates_list[j]
	if len(rates) > 0:
		dp = [[0 for _ in range(max_d + 1)] for _ in range(len(rates))]
		dp[0] = [(d // rates[0][0]) * rates[0][1] + d % rates[0][0] for d in range(max_d + 1)]
		for i in range(1, len(rates)):
			for d in range(1, max_d + 1):
				if d - rates[i][0] >= 0:
					dp[i][d] = max(dp[i - 1][d], dp[i][d - rates[i][0]] + rates[i][1])
				else:
					dp[i][d] = dp[i - 1][d]
		#print(dp)
		max_d = dp[len(rates) - 1][max_d]
print (max_d)

### FIXED CODE ###

n = int(input())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))

rates_list = [[],[]]
for i in range(3):
	if aa[i] > bb[i]:
		rates_list[1].append([bb[i], aa[i]])
	else:
		rates_list[0].append([aa[i], bb[i]])

max_d = n
for j in range(2):
	rates = rates_list[j]
	if len(rates) == 1:
		max_d = (max_d // rates[0][0]) * rates[0][1] + max_d % rates[0][0]
	elif len(rates) == 2:
		val = [rates[0][1] * i for i in range(max_d // rates[0][0] + 1)]
		rest = [max_d - rates[0][0] * i for i in range(max_d // rates[0][0] + 1)]
		val = [val[i] + rates[1][1] * (rest[i] // rates[1][0]) + (rest[i] % rates[1][0]) if rest[i] > 0 else val[i] for i in range(len(val))]
		max_d = max(val)
	elif len(rates) == 3:
		dp = [(d // rates[0][0]) * rates[0][1] + d % rates[0][0] for d in range(max_d + 1)]
		for d in range(1, max_d + 1):
			dp[d] = max([dp[d]] + [dp[d - rates[i][0]] + rates[i][1] if d - rates[i][0] >= 0 else d for i in range(1, len(rates))])
		max_d = dp[-1]
print (max_d)
### BUGGY CODE ###

N=int(input())
L=list(map(int,input().split()))
L_i=[i+1 for i in range(len(L))]

cnt=0
for i in range(N):
    X= sum([(L[i:][x]+L[i])==x for x in range(N-i)])
    cnt = cnt + X
    
print(cnt)

### FIXED CODE ###

def main():
    N=int(input())
    L=list(map(int,input().split()))
    L_i,L_j=[],[]
    L_memo = [0]*N
    cnt=0
    for i, j in enumerate(L):
        if i+j<N:
            L_memo[i+j]+=1
        if i-j>0:
            cnt+=L_memo[i-j]        
    print(cnt)
main()
### BUGGY CODE ###

X, Y, Z, K = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]

ablst = [m + n for n in b for m in a]

ablst.sort(reverse=True)

c.sort(reverse=True)

abclst = [m + n for n in c[:K] for m in ablst[:K]]

abclst.sort(reverse=True)

print(*abclst[:K], sep='\n')


### FIXED CODE ###

import sys
input = sys.stdin.readline

X, Y, Z, K = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]

a.sort(reverse=True)
b.sort(reverse=True)

ablst = [m + n for n in b for m in a]

ablst.sort(reverse=True)

c.sort(reverse=True)

abclst = [m + n for n in c[:K] for m in ablst[:K]]

abclst.sort(reverse=True)

print(*abclst[:K], sep='\n')
### BUGGY CODE ###

import math
from functools import reduce
def gcd(*numbers):
  return reduce(math.gcd,numbers)
K=int(input())
s=0
for a in range(1,K+1):
  for b in range(1,K+1):
    for c in range(1,K+1):
      s+=gcd(a,b,c)
print(s)
    

### FIXED CODE ###

import math
K=int(input())
ans=0
for a in range(1,K+1):
  for b in range(1,K+1):
    ans_=math.gcd(a,b)
    for c in range(1,K+1):
      ans+=math.gcd(ans_,c)
print(ans)
### BUGGY CODE ###

from sys import stdin
readline = stdin.readline

n = int(readline())
a = [int(readline()) for _ in range(n)]

dp = [-float('inf')]

for ai in a:
    for i in range(1, len(dp)):
        if dp[i - 1] < ai and ai < dp[i]:
            dp[i] = ai
    if dp[-1] < ai:
        dp.append(ai)

print(len(dp) - 1)

### FIXED CODE ###

from sys import stdin
from bisect import bisect_left
readline = stdin.readline


dp = [-float('inf')]
n = int(readline())
a = (int(readline()) for _ in range(n))
for ai in a:
    if dp[-1] < ai:
        dp.append(ai)
    else:
        dp[bisect_left(dp, ai)] = ai
print(len(dp) - 1)
### BUGGY CODE ###

a = [int(a) for a in input().split(' ')]
N = a[0]
K = a[1]
    
# def abs_met(N):
#     if N > abs(N - K):
#         N = abs(N - K)
#         abs_met(N)
#     else:
#         print(N)
        
# if K == 1:
#     print(0)
# else:
#     abs_met(N)

l = []
for i in range(N):
    if N > abs(N - K):
        N = abs(N - K)
        l.append(N)
if len(l) == 0:
    print(N)
else:
    print(min(l))

### FIXED CODE ###

a = [int(a) for a in input().split(' ')]
N = a[0]
K = a[1]
    
def abs_met(N):
    N = N % K
    if N > abs(N - K):
        N = abs(N - K)
        abs_met(N)
    else:
        print(N)
        
if K == 1:
    print(0)
else:
    abs_met(N)