Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
n,m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()

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

x = 0
y = 0
for i in range(m):
    for j in range(b[i][0]):
        if (a[x] < b[i][1]):
            a[x] = b[i][1]
            x += 1
        else:
            y = 1
            break
    if (y==1):
        break
print(sum(a))

Fixed submission:
n,m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()

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

x = 0
y = 0
for i in range(m):
    for j in range(b[i][0]):
        if (a[x] < b[i][1]):
            a[x] = b[i][1]
            x += 1
            if (x == n):
                y = 1
                break
        else:
            y = 1
            break
    if (y==1):
        break
print(sum(a))

Buggy python submission (verdict: Runtime Error):
n,m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()

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

x = 0
for i in range(m):
    for j in range(b[i][0]):
        if (a[x] < b[i][1]):
            a[x] = b[i][1]
            x += 1
print(sum(a))

Fixed submission:
n,m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()

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

x = 0
y = 0
for i in range(m):
    for j in range(b[i][0]):
        if (a[x] < b[i][1]):
            a[x] = b[i][1]
            x += 1
            if (x == n):
                y = 1
                break
        else:
            y = 1
            break
    if (y==1):
        break
print(sum(a))
Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int, input().split())
d = m//n
if m==1000000000 and n==3:
    print(250000000)
    exit()

for i in range(0,d):
    if (m % (d-i) == 0):
        print(d-i)
        break

Fixed submission:
n,m = map(int, input().split())
d = m//n
ans = []
for i in range(1,min(d+1,int(m**0.5)+1)):
    if (m % i == 0):
        ans.append(i)
        if (m//i<=d):
            ans.append(m//i)
print(max(ans))

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int, input().split())
d = m//n
for i in range(0,d):
    if (m % (d-i) == 0):
        print(d-i)
        break


Fixed submission:
n,m = map(int, input().split())
d = m//n
ans = []
for i in range(1,min(d+1,int(m**0.5)+1)):
    if (m % i == 0):
        ans.append(i)
        if (m//i<=d):
            ans.append(m//i)
print(max(ans))
Buggy python submission (verdict: Time Limit Exceeded):
n,m,q = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(m)]
B = [list(map(int, input().split())) for _ in range(q)]
l = [[0]*n for _ in range(n)]
for i in range(m):
    l[A[i][0]-1][A[i][1]-1] += 1
#print(l)

a = [[0]*n for _ in range(n)] #差に対して
for i in range(n):
    for j in range(n-i):
        if (j==0):
            a[i][j] = l[0][i]
        else:
            a[i][j] = a[i][j-1] + l[j][j+i]
#print(a)
        
ans = [[0]*n for _ in range(n)]
for i in range(n):
    for j in range(i,n):
        x = 0
        for k in range(j-i+1):
            #print(k)
            if (i!=0):
                x += a[k][j-k]-a[k][i-1]
            else:
                x += a[k][j-k]
        ans[i][j] = x
        #print(i,j,x)
#print(ans)
            

for i in range(q):
    print(ans[B[i][0]-1][B[i][1]-1])
       

Fixed submission:
n,m,q = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(m)]
B = [list(map(int, input().split())) for _ in range(q)]
l = [[0]*n for _ in range(n)]
for i in range(m):
    l[A[i][0]-1][A[i][1]-1] += 1
#print(l)

a = [[0]*n for _ in range(n)] #差に対して
for i in range(n):
    for j in range(n-i):
        if (j==0):
            a[i][j] = l[0][i]
        else:
            a[i][j] = a[i][j-1] + l[j][j+i]
#print(a)
        
ans = [[0]*n for _ in range(n)]
for i in range(n):
    for j in range(i,n):
        x = 0
        for k in range(j-i+1):
            #print(k)
            if (i!=0):
                x += a[k][j-k]-a[k][i-1]
            else:
                x += a[k][j-k]
        ans[i][j] = x
        #print(i,j,x)
#print(ans)
            

for i in range(q):
    print(ans[B[i][0]-1][B[i][1]-1])
Buggy python submission (verdict: Runtime Error):
def calc_distance(maze):
    sx,sy,gx,gy = 0,0,w-1,h-1
    #maze = maze.split()
    INF = 10000000000
    field_x_length = w
    field_y_length = h
        
    distance = []
    for i in range(field_y_length):
        distance.append([])
        for j in range(field_x_length):
            distance[i].append(INF)
        
    queue = []
    queue.insert(0, (sy,sx))
    distance[sy][sx] = 0
        
    while True:
        y, x = queue.pop()
        if(x == gx and y == gy):
            break
                
        for i in range(0,4):
            nx, ny = x +[1,0,-1,0][i], y +[0,1,0,-1][i]
                
            if(0 <= nx and nx < field_x_length and 0 <= ny and ny < field_y_length and maze[ny][nx] != '#' and distance[ny][nx] == INF):
                queue.insert(0,(ny, nx))
                distance[ny][nx] = distance[y][x] + 1
    return distance[gy][gx]
    
h,w = map(int, input().split())
maze = [input() for _ in range(h)]

import collections
count = 0
for i in range(0,h):
    c = collections.Counter(maze[i])
    count += c["#"]


print(w*h - count - calc_distance(maze) -1)

Fixed submission:
def calc_distance(maze):
    sx,sy,gx,gy = 0,0,w-1,h-1
    #maze = maze.split()
    INF = 10000000000
    field_x_length = w
    field_y_length = h
        
    distance = []
    for i in range(field_y_length):
        distance.append([])
        for j in range(field_x_length):
            distance[i].append(INF)
        
    queue = []
    queue.insert(0, (sy,sx))
    distance[sy][sx] = 0
        
    while queue:
        y, x = queue.pop()
        if(x == gx and y == gy):
            break
                
        for i in range(0,4):
            nx, ny = x +[1,0,-1,0][i], y +[0,1,0,-1][i]
                
            if(0 <= nx and nx < field_x_length and 0 <= ny and ny < field_y_length and maze[ny][nx] != '#' and distance[ny][nx] == INF):
                queue.insert(0,(ny, nx))
                distance[ny][nx] = distance[y][x] + 1
    return distance[gy][gx]
    
h,w = map(int, input().split())
maze = [input() for _ in range(h)]

import collections
count = 0
for i in range(0,h):
    c = collections.Counter(maze[i])
    count += c["#"]

if (calc_distance(maze) < 200):
    print(w*h - count - calc_distance(maze) -1)
else:
    print(-1)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
c = [list(map(int, input().split())) for _ in range(n-1)]

def tr(s,n):
    time = c[s-1][1] + c[s-1][0]
    for j in range(s,n-1):
        for i in range (0,100000):
            if (time <= c[j][1]+c[j][2]*i):
                time = c[j][1]+c[j][2]*i+c[j][0]
                break
    return time

ans = []
for k in range(1,n):
    ans.append(tr(k,n))
ans.append(0)
print(*ans, sep='\n')

Fixed submission:
n = int(input())
c = [list(map(int, input().split())) for _ in range(n-1)]

def tr(s,n):
    time = c[s-1][1] + c[s-1][0]
    for j in range(s,n-1):
        if (time <= c[j][1]):
            time = c[j][1]+c[j][0]
        else:        
            a = (time - c[j][1]) // c[j][2]
            if ((time - c[j][1])%c[j][2] != 0):
                a = a+1
            time = c[j][1]+c[j][2]*(a)+c[j][0]
    return time

ans = []
for k in range(1,n):
    ans.append(tr(k,n))
ans.append(0)
print(*ans, sep='\n')
Buggy python submission (verdict: Time Limit Exceeded):
import copy
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
a.sort()
b.sort()
p = 0
c.sort()
q = 0
count = 0
x = 0
for i in range(n):
    p = 0
    q = 0
    while p<n:
        if (b[p] > a[i]):
            while q<n:
                if (c[q] > b[p]):
                    count += (n-q)
                    
                    p += 1
                    break
                else:
                    q += 1
        else:
            p += 1
print(count)

Fixed submission:
import bisect
n= int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
a.sort()
b.sort()
c.sort()
ans = 0
for i in range(n):
    x = bisect.bisect_left(a,b[i])
    y = bisect.bisect_right(c,b[i])
    ans += x*(n-y)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):

n = int(input())
a = [list(map(int, input().split())) for _ in range(n)]
#print(a)

for k in range(n-1,-1,-1):
    for i in range(n-1,-1,-1):
        for j in range(n-1,-1,-1):            
            #print(k,i,j,a)
            if (i==j or k==i or k==j):
                continue
            if (a[i][k]!=-1 and a[k][j]!=-1 and a[i][j] == a[i][k]+a[k][j]):
                a[i][j] = -1
            elif (a[i][k]!=-1 and a[k][j]!=-1 and a[i][j] > a[i][k]+a[k][j]):
                print(-1)
                exit()
            
#print(a)

ans = 0
for i in range(0,n-1):
    for j in range(i,n):
        if (a[i][j]!=-1):
            ans += a[i][j]
print(ans)

Fixed submission:
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    a = [list(map(int, input().split())) for _ in range(n)]
    #print(a)

    for k in range(n-1,-1,-1):
        for i in range(n-1,-1,-1):
            for j in range(n-1,-1,-1):            
                #print(k,i,j,a)
                if (i==j or k==i or k==j):
                    continue
                if (a[i][k]!=-1 and a[k][j]!=-1 and a[i][j] == a[i][k]+a[k][j]):
                    a[i][j] = -1
                elif (a[i][k]!=-1 and a[k][j]!=-1 and a[i][j] > a[i][k]+a[k][j]):
                    print(-1)
                    exit()
            
    #print(a)

    ans = 0
    for i in range(0,n-1):
        for j in range(i,n):
            if (a[i][j]!=-1):
                ans += a[i][j]
    print(ans)
    
if __name__=="__main__":
    main()
Buggy python submission (verdict: Runtime Error):
n = int(input())
p = list(map(int, input().split()))
st = []
c = 0
for i in range(0,n):
    if (p[i] == i+1):
        st.append(1)
    else:
        st.append(0)
def cal(a):
    global c
    for j in range(a,n-1):
        if (st[j] == 1 and  st[j+1]==1):
            c += 1
            cal(j+2)
    return c
           
x = cal(0)
y = st.count(1)
print(y-x)
        

Fixed submission:
n = int(input())
p = list(map(int, input().split()))
st = []
c = 0
for i in range(0,n):
    if (p[i] == i+1):
        st.append(1)
    else:
        st.append(0)
def cal(a):   
    global c    
    if (st[a]== 1 and  st[a+1]==1):
        c += 1
        return a+2
    return a+1
p=0           
while (p < n-1):
    p = cal(p)
    
y = st.count(1)
print(y-c)
Buggy python submission (verdict: Runtime Error):
import sys
n = int(input())
a = list(map(int, input().split()))
a.sort()
a.reverse()
ans = []
for i in range(0,n-1):
    if(a[i]==a[i+1]):
        ans.append(a[i])
if (ans[0]==ans[1] and ans[1]!=ans[2]):
    print(ans[0]*ans[2])
    sys.exit()
if len(ans) <2:
    print(0)
    sys.exit()
print(ans[0]*ans[1])

Fixed submission:
import sys
n = int(input())
a = list(map(int, input().split()))
a.sort()
a.reverse()
ans = []
for i in range(0,n-1):
    if(a[i]==a[i+1]):
        ans.append(a[i])
if len(ans) <2:
    print(0)
    sys.exit()
        
if (ans[0]==ans[1] and ans[1]!=ans[2]):
    print(ans[0]*ans[2])
    sys.exit()

print(ans[0]*ans[1])
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = [int(input()) for _ in range(n)]
now = 1
ans = 0
while True:
    now = a[now-1]
    ans += 1
    if (now==2):
        print(ans)
        break
    elif (now==1):
        print(-1)
        break


Fixed submission:
n = int(input())
a = [int(input()) for _ in range(n)]
now = 1
ans = 0
while True:
    now = a[now-1]
    ans += 1
    if (now==2):
        print(ans)
        break
    elif (now==1):
        print(-1)
        break
    elif (ans>len(a)):
        print(-1)
        break
Buggy python submission (verdict: Time Limit Exceeded):
import copy
h,w,K = map(int, input().split())
c = [[] for _ in range(w)]
for i in range(h):
    a = input()
    for j in range(w):
        c[j].append(a[j])
#print(c)
ans = 0
for i in range(h):
    for j in range(w):
        C = copy.deepcopy(c)
        C[j].pop(i)
        ren = 0
        point = 0
        while True:
            cnt = 0
            for k in range(30,0,-1):
                #print(k)
                start = 0
                while start <= w-K:
                    if (len(C[start]) < k):
                        start += 1
                        continue
                    num = C[start][-k]
                    end = start
                    while True:
                        #print(C,k,start,end)
                        if (end<w-1 and len(C[end+1])>=k and C[end+1][-k]==num):
                            end += 1
                        else:
                            break
                    if (end+1-start >= K):
                        point += (end+1-start)*int(C[start][-k])*(2**ren)
                        for p in range(start,end+1):
                            #print(C,p,k,start,end,point)
                            C[p].pop(-k)
                            cnt += 1
                    start = end + 1
            if (cnt == 0):
                break
            ren += 1
        if (point > ans):
            ans = point
print(ans)

Fixed submission:
import sys
input = sys.stdin.readline

def main():
    import copy
    h,w,K = map(int, input().split())
    c = [[] for _ in range(w)]
    for i in range(h):
        a = input()
        for j in range(w):
            c[j].append(a[j])
    #print(c)
    ans = 0
    for i in range(h):
        for j in range(w):
            C = copy.deepcopy(c)
            C[j].pop(i)
            ren = 0
            point = 0
            while True:
                cnt = 0
                for k in range(30,0,-1):
                    #print(k)
                    start = 0
                    while start <= w-K:
                        if (len(C[start]) < k):
                            start += 1
                            continue
                        num = C[start][-k]
                        end = start
                        while True:
                            #print(C,k,start,end)
                            if (end<w-1 and len(C[end+1])>=k and C[end+1][-k]==num):
                                end += 1
                            else:
                                break
                        if (end+1-start >= K):
                            point += (end+1-start)*int(C[start][-k])*(2**ren)
                            for p in range(start,end+1):
                                #print(C,p,k,start,end,point)
                                C[p].pop(-k)
                                cnt += 1
                        start = end + 1
                if (cnt == 0):
                    break
                ren += 1
            if (point > ans):
                ans = point
    print(ans)
    
if __name__=="__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())
    a = list(map(int, input().split()))
    a.reverse()
    
    a_sum = []
    for i in range(1, len(a)):
        a_sum.append(sum(a[:len(a)-i]))

    total = 0
    a.reverse()

    for i in range(len(a_sum)):
        total += a[i]*a_sum[i]
    
    print(total % (10**9+7))

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    a.reverse()
    
    temp = 0
    a_sum = []

    for i in range(len(a)):
        temp += a[i]
        a_sum.append(temp)

    total = 0
    a_sum.reverse()
    a.reverse()

    for i in range(1, len(a_sum)):
        total += a[i-1]*a_sum[i]
    
    print(total % (10**9+7))

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())

    sum_div = sum(range(1, n+1))
    for i in range(2, n+1):
        sum_div += sum(range(i, n+1, i))
    print(sum_div)

main()

Fixed submission:
n = int(input())

sum_div = sum(range(1, n+1))
for i in range(2, n+1):
    sum_div += sum(range(i, n+1, i))
print(sum_div)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

sum_div = sum(range(1, n+1))
for i in range(2, n+1):
    sum_div += sum(range(i, n+1, i))
print(sum_div)

Fixed submission:
n = int(input())

sum_div = sum(range(1, n+1))
for i in range(2, n+1):
    sum_div += sum(range(i, n+1, i))
print(sum_div)
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n, a, b = map(int, input().split())
    if (b-a) % 2 == 0:
        print((b-a)//2)
    else:
        temp = [(min(n-a, b-1))]

        cnt1 = 0
        cnt2 = 0
        for i in range(0, (min(n-a, b-1))):
            if (b-(a-1) - i) % 2 == 0 and cnt1 == 0:
                temp.append(a - 1 + i + (b-(a-1) - i) // 2)
                cnt1 += 1
            elif (n - (a+(n-b)+i)) % 2 == 0 and cnt2 == 0:
                temp.append(n - b + i + (n - (a+(n-b)+i)) // 2)
                cnt2 += 1

        print(min(temp))

if __name__ == "__main__":
    main()

Fixed submission:
def main():
    n, a, b = map(int, input().split())
    if (b-a) % 2 == 0:
        print((b-a)//2)
    else:
        temp = [(min(n-a, b-1))]

        cnt1 = 0
        cnt2 = 0
        for i in range(0, (min(n-a, b-1))):
            if (b-(a-1) - i) % 2 == 0 and cnt1 == 0:
                temp.append(a - 1 + i + (b-(a-1) - i) // 2)
                cnt1 += 1
            elif (n - (a+(n-b)+i)) % 2 == 0 and cnt2 == 0:
                temp.append(n - b + i + (n - (a+(n-b)+i)) // 2)
                cnt2 += 1

            if cnt1 == 1 and cnt2 == 1:
                break

        print(min(temp))

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())

    print(sum(range(1,n)))

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    n = int(input())

    print(sum(range(1,n)))

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
f = []

for i in range(n):
  f.append(list(map(int, input().split())))

num = [0 for i in range(m+1)]
  
for i in range(n):
  for j in range(1, len(f[i])):
    num[f[i][j]] += 1
    
    
cnt = 0
for i in num:
  if i = n:
    cnt += 1
    
    
print(cnt)

Fixed submission:
n, m = map(int, input().split())
f = []

for i in range(n):
  f.append(list(map(int, input().split())))

num = [0 for i in range(m+1)]
  
for i in range(n):
  for j in range(1, len(f[i])):
    num[f[i][j]] += 1
    
    
cnt = 0
for i in num:
  if i == n:
    cnt += 1
    
    
print(cnt)
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
res = 0
for i in range(1, n + 1, 2):
    if a[i] % 2 != 0:
        res += 1

print(res)


Fixed submission:
n = int(input())
a = list(map(int, input().split()))
res = 0
for i in range(0, n, 2):
    if a[i] % 2 != 0:
        res += 1

print(res)
Buggy python submission (verdict: Runtime Error):
import itertools

n = int(input())
s = 'abcdefghijklmnopqrstuvwxyz'
t = 0
if n <= 26:
    print(s[n - 1])
    exit()
for i in range(2, 100):
    t += 26 ** i
    n -= 26 ** (i - 1)
    if n <= t:
        t = i
        break
res = list(itertools.product(s, repeat=t))[n - 1]

print("".join(res))

Fixed submission:
n = int(input())
s = 'abcdefghijklmnopqrstuvwxyz'
t = 0
res = ""
while n > 26:
    n -= 1
    res += s[n % 26]
    n //= 26

res += s[n - 1]
print(res[::-1])
Buggy python submission (verdict: Time Limit Exceeded):
import collections
n = int(input())
a = list(map(int, input().split()))
q = int(input())
data = collections.Counter(a)
count, key = list(data.values()), list(data.keys())

for i in range(q):
    b, c = map(int, input().split())
    if b in key and c in key:
        count[key.index(c)] += count[key.index(b)]
        count[key.index(b)] = 0
    elif b in key:
        key.append(c)
        count.append(count[key.index(b)])
        count[key.index(b)] = 0
    res = 0
    for j in range(len(key)):
        res += count[j] * key[j]
    print(res)




Fixed submission:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
data = [0] * (10**5 + 1)

for i in a:
    data[i] += 1
res = sum(a)

for j in range(q):
    b, c = map(int, input().split())
    res += (c - b) * data[b]
    data[c] += data[b]
    data[b] = 0
    print(res)
Buggy python submission (verdict: Runtime Error):
a, v = map(int, input().split())
b, w = map(int, input().split())
t = int(input())
x = b - a
y = v - w
n = (a + v * t) - (b + w)
if y <= 0:
    print("NO")
if　n >= 0:
    print("YES")
else:
    print("NO")

Fixed submission:
a, v = map(int, input().split())
b, w = map(int, input().split())
t = int(input())
if abs(b - a) <= (v - w) * t:
    print("YES")
else:
    print("NO")
Buggy python submission (verdict: Runtime Error):
a, b = map(str, input().split())
n = len(a)
al = int(a[:n//2])
ar = int(a[n//2:])
b = float(b)
print(al, ar)
res = str(int(al*b)) + str(int(ar * b))
print(res)


Fixed submission:
from decimal import Decimal, ROUND_DOWN, getcontext
a, b = map(str, input().split())
getcontext().prec = 19
getcontext().rounding = ROUND_DOWN
x = Decimal(a)
y = Decimal(b)
print("{:}".format(Decimal(int(x * y)), rounding=ROUND_DOWN))

Buggy python submission (verdict: Runtime Error):
a, b = map(str, input().split())
n = len(a)
al = int(a[:n//2])
ar = int(a[n//2:])
print(al, ar)
res = str(al*b) + str(ar * b)
print(res)


Fixed submission:
from decimal import Decimal, ROUND_DOWN, getcontext
a, b = map(str, input().split())
getcontext().prec = 19
getcontext().rounding = ROUND_DOWN
x = Decimal(a)
y = Decimal(b)
print("{:}".format(Decimal(int(x * y)), rounding=ROUND_DOWN))
Buggy python submission (verdict: Time Limit Exceeded):
s = str(input())
n = len(s)
s = s[::-1]
x, tot = 1, 0
m = 2019
ans = 0
cnt = [int() for _ in range(m)]
for i in range(n):
    cnt[tot] += 1
    tot += (int(s[i])) * x
    tot %= m
    ans += cnt[tot]
    x = x * 10 
print(ans)


Fixed submission:
s = str(input())
n = len(s)
s = s[::-1]
x, tot = 1, 0
m = 2019
ans = 0
cnt = [int() for _ in range(m)]
for i in range(n):
    cnt[tot] += 1
    tot += (int(s[i])) * x
    tot %= m
    ans += cnt[tot]
    x = x * 10 % m
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int,input().split())
while True:
    if N%K == 0:
        print(0)
        break
    if abs(N-K) < N:
        N = abs(N-K)
    else:
        print(N)
        break

Fixed submission:
N, K = map(int,input().split())
while True:
    if N%K == 0:
        print(0)
        break
    else:
        N=N%K
        N = min(N,abs(N-K))
        print(N)
        break

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int,input().split())
while True:
    if abs(N-K) < N:
        N = abs(N-K)
    else:
        print(N)
        break




Fixed submission:
N, K = map(int,input().split())
while True:
    if N%K == 0:
        print(0)
        break
    else:
        N=N%K
        N = min(N,abs(N-K))
        print(N)
        break