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

import sys
readline = sys.stdin.buffer.readline

N, M = map(int, readline().split())
ans = []
graph = [[] for _ in range(N+1)]

for _ in range(M):
    a, b = map(int, readline().split())
    graph[a].append(b)
    graph[b].append(a)

root = 1
q = [root]
checked = [0] * (N + 1)
checked[root] = 1
order = []

while q:
    x = q.pop(0)
    for y in graph[x]:
        if checked[y]:
            continue
        ans.append(x)
        order.append(y-2)
        checked[y] = 1
        q.append(y)

if len(order) != N - 1:
    print('No')
else:
    print('Yes')
    for i in range(len(order)):
        j = order.index(i)
        print(ans[j])

### FIXED CODE ###

import sys
readline = sys.stdin.buffer.readline
read = sys.stdin.buffer.read

N, M = map(int, readline().split())
m = map(int, read().split())
AB = list(zip(m, m))
graph = [[] for _ in range(N+1)]
for a, b in AB:
    graph[a].append(b)
    graph[b].append(a)

root = 1
q = [root]
checked = [0] * (N + 1)
checked[root] = 1
order = []
D = [0 for _ in range(N+1)]
d = 0
while q:
    x = q.pop(0)
    d = D[x]
    for y in graph[x]:
        if checked[y]:
            continue
        D[y] = d + 1
        checked[y] = 1
        q.append(y)

ans = [0 for _ in range(N+1)]
for a, b in AB:
    d_a, d_b = D[a], D[b]
    if d_a < d_b:
        ans[b] = a
    elif d_a > d_b:
        ans[a] = b

print('Yes')
print('\n'.join(map(str, ans[2:])))
### BUGGY CODE ###

def main():
    N = int(input())
    A = list(map(int, input().split()))
    MOD = 10 ** 9 + 7
    ans = 0
    for i in range(N):
        for j in range(i+1,N):
            ans += A[i] * A[j]
            ans %= MOD

    print(ans % MOD)




if __name__ == "__main__":
    main()

### FIXED CODE ###

from itertools import accumulate

def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = [0] + list(accumulate(A))
    MOD = 10 ** 9 + 7
    ans = 0
    for i in range(N):
        ans += A[i] * (B[N] - B[i+1])
        ans %= MOD

    print(ans)




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

import math
import numpy as np

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def getNearestValue(list, num):
    """
    概要: リストからある値に最も近い値を返却する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return list[idx]

n = int(input())
a = [int(i) for i in input().split()]
check = 0
a.sort()
for i in range(1,n):
    b = a[-i]
    c = getNearestValue(a,b/2)
    an = combinations_count(b,c)
    if check < an:
        check = an
        ans = str(b) + ' ' + str(c)
print(ans)

### FIXED CODE ###

import math
import numpy as np

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def getNearestValue(list, num):
    """
    概要: リストからある値に最も近い値を返却する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return list[idx]

n = int(input())
a = [int(i) for i in input().split()]
check = 0
a.sort()
b = a[-1]
c = getNearestValue(a,b/2)
ans = str(b) + ' ' + str(c)
print(ans)
### BUGGY CODE ###

s = input()
l = len(s)
r = 0
b = [0]*2019
m = b[:]
for i in range(3,l):
    m2 = b[:]
    for j in range(1,2019):
        j2 = (j*10+int(s[i])) % 2019
        m2[j2] += m[j]
    m2[int(s[i-3:i+1]) % 2019] += 1
    r += m2[0]
    m = m2[:]
print(r)

### FIXED CODE ###

s = input()
s_r = s[::-1]
lmod = [0]
cmod = 0
for n,i in enumerate(s_r):
    cmod += int(i)*pow(10,n,2019) % 2019
    cmod = cmod % 2019
    lmod.append(cmod)

from collections import Counter
r = 0
for _,i in Counter(lmod).items():
    r += i*(i-1)/2

print(int(r))
### BUGGY CODE ###

n,m,q= map(int,input().split())
lrList=[list(map(int,input().split()))for i in range(m)]
pqList=[list(map(int,input().split()))for i in range(q)]
for pq in pqList:
    ans = list(filter(lambda x: pq[0]<=x[0],lrList))
    ans = list(filter(lambda x: pq[1]>=x[1],ans))
    print(len(ans))


### FIXED CODE ###

n,m,q= map(int,input().split())
dataList = [[0 for xIndex in range(n+1)]for yIndex in range(n+1)]

for i in range(m):
    xTemp,yTemp = map(int,input().split())
    dataList[yTemp][xTemp]+=1
pqList=[list(map(lambda x:int(x),input().split()))for i in range(q)]
for yIndex in range(1,n+1):
    for xIndex in range(1,n+1):
        dataList[yIndex][xIndex]+=dataList[yIndex][xIndex-1]
for yIndex in range(1,n+1):
    for xIndex in range(1,n+1):
        dataList[yIndex][xIndex]+=dataList[yIndex-1][xIndex]

for pq in pqList:
    p = pq[0]-1
    q = pq[1]
    print(dataList[q][q]+dataList[p][p]-dataList[p][q]-dataList[q][p])
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))
l = []
for i in range(1, N+1):
    a = A.index(i)
    l.append(a+1)
    
print(' '.join(map(str, l)))

### FIXED CODE ###

import numpy as np
N = int(input())
A = list(map(int, input().split()))
B = np.argsort(A)+1
print (' '.join(map(str,B)))
### BUGGY CODE ###

import itertools

h, w, d = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(h)]
q = int(input())
l, r = [], []
for _ in range(q):
    x, y = map(int, input().split())
    l.append(x)
    r.append(y)

coordinates = {a[x][y] : (x, y) for x, y in itertools.product(range(h), range(w))}
for i in range(q):
    score = 0
    while l[i] != r[i]:
        x1, y1 = coordinates[l[i]]
        x2, y2 = coordinates[l[i] + d]
        score += abs(x1 - x2) + abs(y1 - y2)
        l[i] = l[i] + d
    print(score)


### FIXED CODE ###

h, w, d = map(int, input().split())

coordinates = {}
for i in range(h):
    row = list(map(int, input().split()))
    for j in range(w):
        coordinates[row[j]] = (i, j)

costs = [0] * (h * w + 1)
for i in range(d + 1, h * w + 1):
    x1, y1 = coordinates[i]
    x2, y2 = coordinates[i - d]
    costs[i] = abs(x1 - x2) + abs(y1 - y2) + costs[i - d]

q = int(input())
for _ in range(q):
    l, r = map(int, input().split())
    print(costs[r] - costs[l])
### BUGGY CODE ###

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

for k in range(min(a), max(a) + 1):
	s.append(a.count(k) + a.count(k + 1) + a.count(k - 1))
print(max(s))


### FIXED CODE ###

from bisect import bisect_left, bisect_right

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

ans = 1
for i in range(N):
    X = a[i]
    res = bisect_right(a, X+1) - bisect_left(a, X-1)
    ans = max(res, ans)

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

N = int(input())
S = list(input() for i in range(N))  #スペース抜きの単語を複数行

import collections
A = collections.Counter(S)

max_k_list = [kv[0] for kv in A.items() if kv[1] == max(A.values())]
max_k_list = sorted(max_k_list)

for i in max_k_list:
    print(i)

### FIXED CODE ###

N = int(input())
S = list(input() for i in range(N))  #スペース抜きの単語を複数行

A = {}
for i in S:
    A.setdefault(i, 1)
    A[i] += 1

B = max(A.values())
C = [i for i in A if A[i] == B]
C = sorted(C)

for i in C:
    print(i)
### BUGGY CODE ###

n = int(input())
p = [2,3,5,7,11,13,17,19,23,29]
for i in range(2,int(n**0.5)+1):
    x = 1
    for j in range(len(p)):
        if(i%p[j] == 0):
            x = 0
            break
    if(x == 1):
        p.append(i)
t = {}
for i in range(len(p)):
    if(n%p[i] == 0):
        t[p[i]] = 1
        n //= p[i]
        while(n%p[i] == 0):
            n //= p[i]
            t[p[i]] += 1
        if(n == 1):
            break
if(n != 1):
    z = 1
else:
    z = 0
k = list(t.keys())
for i in range(len(k)):
    j = 1
    while(1):
        if(t[k[i]] - j >= 0):
            t[k[i]] -= j
            z += 1
            j += 1
        else:
            break
print(z)

### FIXED CODE ###

n = int(input())
t = []
for i in range(2,int(-(-n**0.5//1))+1):
    if(n%i == 0):
        cmp = 0
        while(n%i == 0):
            cmp += 1
            n //= i
        t.append([i,cmp])
    if(n == 1):
        break
if(n != 1):
    z = 1
else:
    z = 0
z -= len(t)
for i in range(len(t)):
    j = 1
    while(t[i][1] >= 0):
        t[i][1] -= j
        j += 1
        z += 1
print(z)
### BUGGY CODE ###

import sys
input = sys.stdin.readline

N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
C = []
for _ in range(M):
    b, c = map(int, input().split())
    C.extend([c]*b)
C.sort()

ans = 0
for i in range(N):
    if C:
        if C[-1] >= A[-1]:
            ans += C.pop()
            continue
    ans += A.pop()
print(ans)


### FIXED CODE ###

import sys
input = sys.stdin.readline

N, M = map(int, input().split())
A = [int(i) for i in input().split()]
table = []
for i in range(N):
    table.append((A[i], 1))
for i in range(M):
    b, c = map(int, input().split())
    table.append((c, b))

table = sorted(table)
cnt = N
ans = 0
while cnt > 0:
    #print(ans,s)
    c, b = table.pop()
    if b >= cnt:
        ans += c*cnt
        break
    ans += c*b
    cnt -= b
print(ans)
### BUGGY CODE ###

n,q = map(int,input().split())
s = input()
a = [list(map(int,input().split())) for i in range(q)]

for i in range(q):
    print(s[a[i][0]-1:a[i][1]].count("AC"))

### FIXED CODE ###

N,Q=map(int,input().split())
S=input()
count=[0]*N
for i in range(1,N):
    count[i]+=count[i-1]
    if S[i-1:i+1]=="AC":count[i]+=1
for i in range(Q):
    l,r=map(int,input().split())
    print(count[r-1]-count[l-1])
### BUGGY CODE ###

import sys
input=sys.stdin.readline
from collections import Counter
def get_counter(s):
    return Counter(s.upper().replace(' ', ''))

n=int(input())
a=[get_counter(input().rstrip('\r\n')) for i in range(n)]
cnt=0
for i in range(n):
    for j in range(i+1,n):
        if a[i]==a[j]:
            cnt+=1
print(cnt)

### FIXED CODE ###

import sys
input=sys.stdin.readline
from collections import Counter
def get_counter(s):
    return Counter(s.upper().replace(' ', ''))

n=int(input())
#a=[get_counter(input().rstrip('\r\n')) for i in range(n)]
a=["".join(sorted(input().rstrip('\r\n'))) for i in range(n)]
def r(n):
    cnt=0
    for i in range(n):
        cnt+=i+1
    return cnt

my_dic={}
for i in a:
    if i in my_dic:
        my_dic[i]+=1
    else:
        my_dic[i]=1

res=0
for i in my_dic.values():
    res+=r(i-1)
        
print(res)
### BUGGY CODE ###

s=list(input())
mod=10**9+7
for i in range(len(s)):
    if s[i]=='?':
        s[i]=-1
    else:
        s[i]=int(s[i])

dp=[[0]*13 for i in range(len(s)+1)]
s.reverse()
y=[]

for i in range(len(s)):
    if s[i]!=-1:
        x=(s[i]*10**i)%13
        if i==0:
            dp[i+1][x]=1
        else:
            for j in range(13):
                dp[i+1][(j+x)%13]+=(dp[i][j])%mod
    else:
        for j in range(10):
            y.append((j*10**i)%13)
        if i==0:
            for k in range(len(y)):
                dp[i+1][y[k]]+=1
                    
        for k in range(13):
            for l in range(len(y)):
                dp[i+1][(k+(y[l]))%13]+=(dp[i][k])%mod
        y=[]
print(dp[len(s)][5]%mod)
            
        
        


     

    


### FIXED CODE ###

s=list(input())
mod=10**9+7
for i in range(len(s)):
    if s[i]=='?':
        s[i]=-1
    else:
        s[i]=int(s[i])

dp=[[0]*13 for i in range(len(s)+1)]
s.reverse()
dp[0][0]=1
y=[]
x=1
for i in range(len(s)):
    if s[i]!=-1:
        z=s[i]*x

        for j in range(13):
            dp[i+1][(j+z)%13]+=dp[i][j]%mod
    else:
        for j in range(10):
            y.append(x*j)

        for k in range(13):
            for l in range(len(y)):
                dp[i+1][(k+y[l])%13]+=dp[i][k]%mod
        y=[]
    x=(x*10)%13
        
print(dp[len(s)][5]%mod)
### BUGGY CODE ###

N = int(input())
S = [input() for i in range(N)]
count_dict = {}
for s in S:
  if not s in count_dict.keys():
    count_dict[s] = 1
  else:
    count_dict[s] += 1
count_dict = dict(filter(lambda x: x[1] >= max(count_dict.values()), count_dict.items()))
ans = list(count_dict.keys())
ans.sort()
for s in ans:
  print(s)

### FIXED CODE ###

N = int(input())
S = [input() for i in range(N)]
count_dict = {}
for s in S:
  if not s in count_dict.keys():
    count_dict[s] = 1
  else:
    count_dict[s] += 1
maxnum = max(count_dict.values())
count_dict = dict(filter(lambda x: x[1] == maxnum, count_dict.items()))
ans = list(count_dict.keys())
ans.sort()
for s in ans:
  print(s)
### BUGGY CODE ###

l = [0]*10**6
l[0] = input()
i = 1

while True:
    l[i] = l[i-1].replace("10", "").replace("01","")
    if "10" not in l[i] and "01" not in l[i]:
        print(len(l[0]) - len(l[i]))
        exit()
    i += 1

### FIXED CODE ###

s = input()
print(min(s.count("1"), s.count("0")) *2)
### BUGGY CODE ###

n = int(input())
a = list(map(int,input().split()))
q = int(input())
bc = [0]*q
ans =[0]*q
for i in range(q) :
    bc[i] = list(map(int,input().split()))

for i in range(q) :
    a = [bc[i][1] if j == bc[i][0] else j for j in a]
    ans[i] = sum(a)

for i in range(q) :
    print(ans[i])




### FIXED CODE ###

n = int(input())
A = list(map(int,input().split()))
q = int(input())
ans = sum(A)
many =[0]*(10**5 + 1)
for a in A :
    many[a] += 1

for i in range(q) : 
    B,C = (int(a) for a in input().split())
    many[C] += many[B]
    ans += many[B] * (C-B)
    many[B] = 0
    print(ans)
### BUGGY CODE ###

#162C
#Sum of gcd of Tuples (Easy)

import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd,numbers)

k=int(input())+1
ans=0
for a in range(1,k):
    for b in range(1,k):
        for c in range(1,k):
            ans+=gcd(a,b,c)
print(ans)

### FIXED CODE ###

#162C
#Sum of gcd of Tuples (Easy)

import math

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

import sys
from collections import Counter

read = sys.stdin.read


def sieve_of_eratosthenes(n):
    sieve = [True] * n
    for i in range(3, int(n ** 0.5) + 1, 2):
        if sieve[i]:
            sieve[i * i::2 * i] = [False] * ((n - i * i - 1) // (2 * i) + 1)
    return [2] + [i for i in range(3, n, 2) if sieve[i]]


def prime_factorize(n):
    a = []
    for i in primes:
        if n % i != 0:
            continue
        while n % i == 0:
            a.append(i)
            n //= i
        if n == 1:
            return a


N, *A = map(int, read().split())
mod = 10 ** 9 + 7

primes = sieve_of_eratosthenes(10 ** 6)
dic = {i: 0 for i in primes}

n = len(primes)
p = [0] * n
for i in A:
    if i == 1:
        continue
    tmp = Counter(prime_factorize(i))
    for key, value in tmp.items():
        # print(key, value)
        if dic[key] < value:
            dic[key] = value

lcm = 1
for i, j in dic.items():
    if j == 0:
        continue
    lcm *= pow(i, j, mod)
    lcm %= mod

answer = 0
for i in A:
    answer += lcm * pow(i, mod - 2, mod)
    answer %= mod

print(answer)


### FIXED CODE ###

import sys
from collections import Counter

read = sys.stdin.read


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def sieve_of_eratosthenes(n):
    sieve = [True] * n
    for i in range(3, int(n ** 0.5) + 1, 2):
        if sieve[i]:
            sieve[i * i::2 * i] = [False] * ((n - i * i - 1) // (2 * i) + 1)
    return [2] + [i for i in range(3, n, 2) if sieve[i]]


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


N, *A = map(int, read().split())
mod = 10 ** 9 + 7

primes = sieve_of_eratosthenes(max(A) + 1)
dic = {i: 0 for i in primes}

n = len(primes)
p = [0] * n
for i in A:
    tmp = Counter(prime_factorize(i))
    for key, value in tmp.items():
        #print(key, value)
        if dic[key] < value:
            dic[key] = value

lcm = 1
for i, j in dic.items():
    if j == 0:
        continue
    lcm *= pow(i, j, mod)
    lcm %= mod

answer = 0
for i in A:
    answer += lcm * pow(i, mod - 2, mod)
    answer %= mod

print(answer)
### BUGGY CODE ###

import numpy as np
n=int(input())
action=np.array([list(map(int,input().split())) for _ in range(n)])
# happy[i][j] i日目にaction j を選んだ場合の幸福度の最大値
happy=np.zeros((n+1,3))
for i in range(1,n+1):
    for j in range(3):
        for k in range(3):
            if j!=k:
                happy[i][j]=max(happy[i][j],happy[i-1][k]+action[i-1][j])
        # happy[i][j]=max(np.concatenate((happy[i-1][0:j]+action[i-1][j],happy[i-1][j+1:3]+action[i-1][j]),axis=None))
# print(happy)
print(int(max(happy[n])))


### FIXED CODE ###

n=int(input())
action=[list(map(int,input().split())) for _ in range(n)]
# happy[i][j] i日目にaction j を選んだ場合の幸福度の最大値
happy=[[0,0,0]for _ in range(n+1)]
for i in range(1,n+1):
    for j in range(3):
        for k in range(3):
            if j!=k:
                happy[i][j]=max(happy[i][j],happy[i-1][k]+action[i-1][j])
# print(happy)
print(max(happy[n]))
### BUGGY CODE ###

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

output = 1
for i in a:
    output *= i
    
if output > 10**18:
    output = -1
    
print(output)

### FIXED CODE ###


n = int(input())
a = list(map(int, input().split()))
if 0 in a:
    output = 0
    
else:

    output = 1

    for i in a:
        output *= i
        if output > 10**18:
            output = -1
            break
        
    
print(output)
### BUGGY CODE ###

N, M = map(int, input().split())
A = [0] * (N+1)
for i in range(M):
    l, r = map(int, input().split())
    for i in range(l, r + 1):
        A[i] += 1

print(A.count(M))

### FIXED CODE ###

from collections import Counter

N, M = map(int, input().split())
L = []
R = []

for i in range(M):
    l, r = map(int, input().split())
    L.append(l)
    R.append(r)

maxL = max(L)
minR = min(R)

ans = max(0, minR - maxL + 1)
print(ans)
### BUGGY CODE ###

N, W = map(int, input().split())
wv = [[0 for i in range(2)] for j in range(N)]
dp = [[0 for i in range(N+1)] for j in range(W+1)]

for i in range(N):
  wv[i] = list(map(int, input().split()))

for i in range(N):
  for w in range(1,W+1):
    if w < wv[i][0]:
      dp[w][i+1] = dp[w][i]
    else:
      dp[w][i+1] = max(dp[w-wv[i][0]][i]+wv[i][1], dp[w][i])

print(dp[-1][-1])

### FIXED CODE ###

N, W = map(int, input().split())
wv = [[0 for i in range(2)] for j in range(N)]
dp = [[0 for i in range(N+1)] for j in range(W+1)]

for i in range(N):
  wv[i] = list(map(int, input().split()))

for i in range(N):
  for w in range(1,W+1):
    if w < wv[i][0]:
      dp[w][i+1] = dp[w][i]
    else:
      dp[w][i+1] = max(dp[w-wv[i][0]][i]+wv[i][1], dp[w][i])

print(dp[-1][-1])
### BUGGY CODE ###

import math

def solve(n, m, a, b):
    islands = []

    for i in range(m):
        if(a[i] == 1):
            islands.append(b[i])
        if(b[i] == 1):
            islands.append(a[i])

    for i in range(m):
        if(a[i] == n and b[i] in islands):
            return "POSSIBLE"
        if(b[i] == n and a[i] in islands):
            return "POSSIBLE"
    return "IMPOSSIBLE"

(n, m) = map(int, input().split())
a = []
b = []

for i in range(m):
    (ai, bi) = map(int, input().split())
    a.append(ai)
    b.append(bi)

print(solve(n, m, a, b))


### FIXED CODE ###

def solve(n, m, a, b):
    islands = set()

    for i in range(m):
        if(a[i] == 1):
            islands.add(b[i])
        if(b[i] == 1):
            islands.add(a[i])

    for i in range(m):
        if(a[i] == n and b[i] in islands):
            return "POSSIBLE"
        if(b[i] == n and a[i] in islands):
            return "POSSIBLE"
    return "IMPOSSIBLE"

(n, m) = map(int, input().split())
a = []
b = []

for i in range(m):
    (ai, bi) = map(int, input().split())
    a.append(ai)
    b.append(bi)

print(solve(n, m, a, b))
### BUGGY CODE ###

# -*- coding: utf-8 -*-

import sys
input = sys.stdin.readline

N, W = map(int, input().split())
wv = [list(map(int, input().split())) for i in range(N)]

value = [[-1 for j in range(W + 1)] for i in range(N + 1)]

value[0] = [0 for j in range(W + 1)]
for i in range(1, N + 1):
    for j in range(W + 1):
        w, v = wv[i-1]
        if j >= w:
            value[i][j] = max(value[i-1][j], value[i-1][j-w] + v)
        else:
            value[i][j] = value[i-1][j]

ans = max(value[N])
print(ans)


### FIXED CODE ###

# -*- coding: utf-8 -*-

import sys
input = sys.stdin.readline

N, W = map(int, input().split())
wv = [list(map(int, input().split())) for i in range(N)]

value = [[-1 for j in range(W + 1)] for i in range(N + 1)]

value[0] = [0 for j in range(W + 1)]
for i in range(1, N + 1):
    for j in range(W + 1):
        w, v = wv[i-1]
        if j >= w:
            value[i][j] = max(value[i-1][j], value[i-1][j-w] + v)
        else:
            value[i][j] = value[i-1][j]

ans = max(value[N])
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()))
AB = [a + b for a in A for b in B]
AB.sort()
AB = AB[-K:]
ABC = [ab + c for ab in AB for c in C]
ABC.sort()
for v in ABC[-1:-K-1:-1]:
    print(v)


### FIXED 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()))
AB = [a + b for a in A for b in B]
AB.sort()
AB = AB[-K:]
ABC = [ab + c for ab in AB for c in C]
ABC.sort()
for v in ABC[-1:-K-1:-1]:
    print(v)
### BUGGY CODE ###

a = int(input())
ar = []
for i in range(a):
    l = list(map(int,input().split(" ")))
    ar.insert(0,l)
count = 0
for r in ar:
    x = r[0] + count
    y = r[1]
    m = x % y
    if m != 0:
        count += y - m
print(count)

### FIXED CODE ###

a = int(input())
ar = []
for i in range(a):
    l = list(map(int,input().split(" ")))
    ar.append(l)
ar.reverse()
count = 0
for r in ar:
    x = r[0] + count
    y = r[1]
    m = x % y
    if m != 0:
        count += y - m
print(count)
### BUGGY CODE ###

from itertools import combinations


n = int(input())
l = list(map(int, input().split()))
l.sort(reverse=True)
cnt = 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]:
                cnt += 1
print(cnt)


### FIXED CODE ###

import bisect



def find_num(lst, num):
    insert_index = bisect.bisect_left(lst, num)
    return insert_index


n = int(input())
l = list(map(int, input().split()))
l.sort()
cnt = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        cnt += find_num(lst=l[j+1:], num=l[i] + l[j])
print(cnt)
### BUGGY CODE ###

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

ans = 0
roop_init = -1
roop_sum = 0
roop_len = 0
x = X
ans += x
flug = False    # 周期を数えるフラグ
for i in range(2, N+1):
    old_x = x
    x = old_x**2 % M
    if x == 0:
        print(ans)
        break
    if old_x**2 >= M and not flug:
        # print(old_x, x)
        flug = True
        roop_init = x
        roop_sum = x
        roop_len = 1
    elif x == roop_init and flug:
        # print(i)
        ans += (N - (i-1)) // roop_len * roop_sum
        for j in range((N - (i-1)) % roop_len):
            if j != 0:
                old_x = x
                x = old_x**2 % M
            ans += x
        print(ans)
        break
    elif flug:
        roop_len += 1
        roop_sum += x
    ans += x
else:
    print(ans)



### FIXED CODE ###

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

id_ = [-1] * M
a = []
len_ = 0
ans = 0
x = X
while id_[x] == -1:
    id_[x] = len_
    len_ += 1
    ans += x
    a.append(x)
    x = x**2 % M

c = len_ - id_[x]
s = 0
for i in range(id_[x], len_):
    s += a[i]

if len_ >= N:
    ans = 0
    for i in range(N):
        ans += a[i]
else:
    n = N
    n -= len_
    ans += s * (n // c)
    n %= c
    for i in range(n):
        ans += a[id_[x]+i]
print(ans)
### BUGGY CODE ###

import sys
N, K = map(int, input().split())

if K == 1:
    print(0)
    sys.exit()
while True:
    x = abs(N - K)
    if x > N:
        break
    N = min(x, N)
print(N)

### FIXED CODE ###

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

t = N % K
N = min(t, K - t)

print(N)