Problem:


Score : 300 points

Problem Statement

Given any integer x, Aoki can do the operation below.

Operation: Replace x with the absolute difference of x and K.

You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more times.

Constraints

0 ≤ N ≤ 10^{18}

1 ≤ K ≤ 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the minimum possible value taken by N after Aoki does the operation zero or more times.

Sample Input 1

7 4

Sample Output 1

1

Initially, N=7.

After one operation, N becomes |7-4| = 3.

After two operations, N becomes |3-4| = 1, which is the minimum value taken by N.

Sample Input 2

2 6

Sample Output 2

2

N=2 after zero operations is the minimum.

Sample Input 3

1000000000000000000 1

Sample Output 3

0



Sample input:
7 4


Sample output:
1

Submission status: Wrong Answer

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))


N, K = LI()

ans = mod
ans = min(ans, N % K, abs(N % K - K))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
n,k = map(int,input().split())
m = n
c = 0
r = True

while r == True:
    if k >= 2*m:
        r = False
    elif k >= m:
        c += 1
        r = False
    else:
        print(n%k)
        sys.exit()

print(abs(n-(c*k)))

Submission status: Wrong Answer

Solution code:
line1 = input().split()
n = int(line1[0])
k = int(line1[1])
list1 = [n]
m = abs(n - k)
while m not in list1:
  n = m
  m = abs(n - k)
  list1.append(m)

print(min(list1))

Submission status: Wrong Answer

Solution code:
import sys
n,k = map(int,input().split())
m = n
c = 0
r = True
if n%k == 0:
    print(0)
    sys.exit()
while r == True:
    if k >= 2*m:
        r = False
    elif k >= m:
        c += 1
        r = False
    else:
        print(n%k)
        sys.exit()

print(abs(n-(c*k)))

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
ans = 0


if (N < K):
    ans = N
elif(N % K == 0):
    ans = 0
else:
    N = N % K
    if(N <= abs(N-K)):
        ans = N
    else:
        ans = abs(N-K)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
ans = 0


if (N < K):
    ans = N
elif(N % 2 == 0):
    ans = 0
else:
    N = N % K
    if(N <= abs(N-K)):
        ans = N
    else:
        ans = abs(N-K)



print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
ans = 0
preN =N
count = 0

if (N < K):
    ans = N
elif(N % 2 == 0):
    ans = 0
else:
    while (count == 0):
        preN = preN - K
        if (preN<0):
            count = 1
            prepreN = preN + K
    preNa = abs(preN)
    if(preNa > prepreN):
        ans = prepreN
    else:
        ans = preNa



print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
minn = n
minmin = n
nmin = 0
mc = 0
mcc = 1000000
while (True):
  if minn - k > 0:
    minn = abs(minn - 2 * k)
  elif minn - k < 0:
    minn = minn
  else:
    minn = k
  if minmin <= minn:
    break
  else:
    minmin = minn
    mc += 1
  if mc == mcc:
    minmin = 0
    break
print(minmin)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
minn = n
minmin = n
nmin = 0
mc = 0
mcc = 10
while (True):
  if minn - k > 0:
    minn = abs(minn - 2 * k)
  elif minn - k < 0:
    minn = minn
  else:
    minn = k
  if minmin <= minn:
    break
  else:
    minmin = minn
    mc += 1
  if mc == mcc:
    minmin = 0
    break
print(minmin)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
print(N%K)

Submission status: Wrong Answer

Solution code:
def resolve():
    n, k = map(int, input().split())
    if n < k:
        print(n)
    else:
        _n = n % k
        if _n == 0:
            print(0)
        else:
            print(abs(_n - k))


resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    import math
    n, k = map(int, input().split())
    if n < k:
        print(n)
    elif k == 1:
        print(0)
    else:
        while True:
            _n = abs(n-k)
            if n > _n:
                n = _n
            else:
                print(_n)
                break

resolve()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())

if N < K:
    print(N)
    exit()
else:
    wari = N // K
    print(min(N - (K * wari), (K *(wari + 1) - N)))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
def sousa(x):
    x = abs(x - k)
    return x
before_n = n
cnt = -1
while before_n >= n:
    before_n = n
    n = sousa(n)
    cnt = cnt + 1
print(cnt)

Submission status: Wrong Answer

Solution code:
n,k =map(int, input().split())
m = n % k
print(min(n, k - m))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())


if K == 1:
    print(0)

elif N-K==0:
    print(0)

else:
    shou, amari_1 = divmod(N, abs(N-K))
    N_min_1 = N - (shou * abs(N-K))
    N_min_2 = abs(N_min_1 - abs(N-K))
    print(min(N_min_1, N_min_2))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
if k==1 or k==n or n==0:
  print(0)
elif n<k:
  print(k%n)
else:
  n = k+ n%k
  n1 = n%k
  while n1<n:
    n=n1
    n1 = abs(k-n1)
  print(n)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())

before = abs(N - int(N / K) * K)
after = abs(before - K)

if before > after:
    print(after)
else:
    print(before)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())

before = N - int(N / K) * K
after = abs(before - K)

if before > after:
    print(after)
else:
    print(before)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
if abs(k-n)>n:
  print(n)
elif k==1 or k==n:
  print(0)
elif n<k:
  print(k%n)
else:
  print(n%abs(k-n))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
if abs(k-n)>n:
  print(n)
elif k==1 or k==n:
  print(0)
else:
  print(n%abs(k-n))

Submission status: Wrong Answer

Solution code:
N,K=list(map(int,input().split()))
if N>=K:
    ans_1=N-K*int(N/K)
    ans_2=abs(ans_1-K)
    print(min(ans_1,ans_2))

else:
    ans_1=N
    ans_2=abs(ans_1-K)
    print(min(ans_1,ans_2))

Submission status: Wrong Answer

Solution code:
import math

n,k = map(int,input().split())
abs(n)
t = math.floor(n/k)
a = abs(n-k*t)
b = abs(n-k*(t+1))
print(min([a,b]))

Submission status: Wrong Answer

Solution code:
import math

n,k = map(int,input().split())
abs(n)
t = math.floor(n/k)
a = abs(n-k*t)
b = abs(n-k*(t+1))
print(min([a,b,n]))

Submission status: Wrong Answer

Solution code:
N,K=list(map(int,input().split()))
if N>=K:
    ans_1=N-K*int(N/K)
    ans_2=abs(N-K*int((N/K)+1))
    print(min(ans_1,ans_2))

else:
    ans_1=N
    ans_2=abs(N-K)
    print(min(ans_1,ans_2))

Submission status: Wrong Answer

Solution code:
N,K=list(map(int,input().split()))
if N>K:
    ans_1=N-K*int(N/K)
    ans_2=abs(N-K*int((N/K)+1))
    print(min(ans_1,ans_2))

else:
    ans_1=N
    ans_2=abs(N-K)
    print(min(ans_1,ans_2))

Submission status: Wrong Answer

Solution code:
N,K=list(map(int,input().split()))
if N>K:
    ans_1=N-K*int(N/K)
    ans_2=abs(N-K*int(N/K+1))
    print(min(ans_1,ans_2))

else:
    ans_1=N
    ans_2=abs(N-K)
    print(min(ans_1,ans_2))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
while n>=0:
  if n-k<=0:
    print(max(abs(n-k),n))
    break
  n=n-k

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
while n>=0:
  if n-k<=0:
    print(abs(n-k))
    break
  n=n-k

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
while n>=0:
  n=abs(n-k)
  if n-k<=0:
    print(abs(n-k))
    break

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
while n>=0:
  n=abs(n-k)
  if n-k<=0:
    print(abs(n-1))
    break

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
while n>=0:
  n=abs(n-k)
  if n-k<=0:
    print(n)
    break

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
t=0
N=abs(n-k)
while N<n:
    n=N
    N=abs(n-k)
    t=1
if t==1:
    print(N)
else:
    print(n)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
s = N // K
min(abs(N - s * K), abs(N- (s+1) * K))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
import fractions
a=fractions.gcd(N,K)
if K==1:
  print(0)
else:
  print(a)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
print(max(n%k,(k-n%k)%k))

Submission status: Wrong Answer

Solution code:
N,K=map(int, input().split())
if N>=1000000000000000000 or K>=1000000000000000000:
  print(0)
  exit(0)
while True:
  n=abs(N-K)
  if N <= n:
    break
  else:
    N = n
print(N)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())

def gcd(a,b):
    if b == 0:
        return a
    return gcd(b,a%b)

if n < 2 or k < 2:
    print(0)
else:
    print(gcd(n,k))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
res = [N]
if N % K == 0:
  print(0)
elif N > K and N % K != 0:
  b = N % K
  print(int(K/b))
elif N < K:
  while True:
    res.append(abs(N - K))
    N = abs(N - K)
    if N in res:
      break
  print(min(res))

Submission status: Wrong Answer

Solution code:
#abc 161c
import math
n,k = map(int, input().split())
i= (n/k)
ilow = int(i)
iup = i+1
print(min(n,abs(n-k*iup),n-k*ilow))

Submission status: Wrong Answer

Solution code:
#abc 161c
import math
n,k = map(int, input().split())
i = int(n/k)
ilow = i
iup = i+1
print(min(abs(n-k*iup),n-k*ilow))

Submission status: Wrong Answer

Solution code:
#abc 161c
import math
n,k = map(int, input().split())
i = int(n/k)
ilow = i
iup = i+1
print(min(n,abs(n-k*iup),n-k*ilow))

Submission status: Wrong Answer

Solution code:
#abc 161c
import math
n,k = map(int, input().split())
ilow = math.floor(n/k)
iup = ilow+1
print(min(n,abs(n-k*iup),n-k*ilow))

Submission status: Wrong Answer

Solution code:
#abc 161c
import math
n,k = map(int, input().split())
iup = math.floor(n/k)
ilow = math.ceil(n/k)
print(min(n,abs(n-k*iup),abs(n-k*ilow)))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
N0=N
while True:
    add=abs(N0-K)
    if N0>0:
        N0-=K
    else:
        N1=N0+K
        break

print(max(N0,N1))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
if(N < K):
  print(N)
else:
  print(min(abs(N % K - K),N % K))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
if(N >K):
  print(N)
elif(N % K == 0):
  print(0)
else:
  print(abs(N % K - K))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())

max_i = max(n, k)


if n>=k:
    n = n%k
else:
    for _ in range(max_i):
        tmp = abs(n-k)
        if tmp >= n:
            break
        else :
            n = tmp
    
    print(n)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())

max_i = max(n, k)


if n>=k:
    n = n%k
if n==0 or n==1 or k==1:
    print(0)
else:
    for _ in range(max_i):
        tmp = abs(n-k)
        if tmp >= n:
            break
        else :
            n = tmp
    
    print(n)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())

ans = n
if n > k:
    ans = 0 if n % k == 0 else abs((n % k) - k)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())

if k == 1:
    print(0)
elif n < k:
    print(n)
elif n >= k:
    if n % k == 0:
        print(0)
    elif n % k != 0:
        n = n%k
        while abs(n-k) < n:
            n -= k
        print(abs(n))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())

if k == 1:
    print(0)
elif n < k:
    print(n)
elif n >= k:
    if n % k == 0:
        print(0)
    elif n % k != 0:
        print(min(abs((n%k)-k),(n%k)))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())

if k == 1 or n == k:
    print(0)
elif n < k:
    print(n)
elif n > k:
    if n % k == 0:
        print(0)
    elif n % k != 0:
        print(min(abs((n%k)-k),(n%k)))

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())

result1 = N
result = abs(N - K)

if result1 < result:
  print(result1)
  
else:
  while result1 > result or result == 0:
    result1 = result
    result = abs(result - K)
    
  else:
    print(result)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())

if k == 1 or n == k:
    print(0)
elif n < k:
    print(n)
elif n > k:
    if n % k == 0:
        print(0)
    elif n % k != 0:
        print(abs((n%k)-k))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())

x = (K+N)//K -1
print(x)
N -= x*K


print(N)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
print(min(n%k,n-n%k))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
for i in range(10**6):
    N=min(N,abs(N-K))
print(N)

Submission status: Wrong Answer

Solution code:
n,k = map(int, raw_input().split(' '))
seen = {}

while(n not in seen):
	seen[n] = abs(n - k)
	n %= k

print min(seen.values())

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
if n<k:
  print(n)
elif n==k:
  print(0)
else:
  r=n%k
  print(min(r,abs(r-k)))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
if n<k:
  print(n)
elif n==k:
  print(0)
else:
  m=n//k
  r=n-m*k
  print(min(r,abs(r-k)))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
m = n
while m > 0:
  n = min(abs(n - k), n)
  if m > n:
      m = n
  else:
      print(m)
      break

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
m = n
while m > 0:
  n = min(abs(n - k), n)
  if m > n:
      m = n
  else:
      print(n)
      break

Submission status: Wrong Answer

Solution code:
import sys
import math
input = sys.stdin.readline


def main():
    N, K = map(int, input().split())
    if N < K:
        print(N)
    N %= K
    print(min(N, abs(N-K)))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))

counter = 0
x_min = n
x = n
while not counter == 3:
    x = x % k
    if x_min > x:
        x_min = x
        counter = 1
    elif x_min == x:
        counter = counter + 1

print(x_min)

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))

res = n
while res > 2:
    res = res % k
    if res <= 2:
        break
    else:
        res = k - res


if res == 0:
    print(res)
elif res == 1:
    print(res)
elif res == 2:
    print(res)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())

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

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())

shou,lis = n//k,[]
if shou!=0: n=n%(shou*k)
for i in range(10):
  n=abs(n-k)
  lis.append(n)
print(lis)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())

if N % K == 0:
    print(0)
else:
    num = N
    while abs(num - K) < num:
        num = num - K
    print(num)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

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

coeff = round(float(N)/float(K))
print(int(abs(N-coeff*K)))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
if k>n:
    print(n)
elif n==k or n%k==0:
    print(0)
else:
    print(abs((n%k)-k))

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))
# print(n, k)


min_result = n
flag = 0
dic = {}

if n > k:
    z = n // k
    zz = n //z
    result = zz
else:
    result = n

while flag==0:
    result = abs(result - k) 
    # print('result',result)
    # print('min_result',min_result)
    if result <= min_result:
        min_result = result
        dic[result] = dic.get(result, 0) + 1

        if dic[result] > 1:
            flag=1
print(min_result)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
n_b=N
a=N%K
if(K/2<a):
    print(a-K)
else:
    print(a)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
print(min(n%k,n%k-k))

Submission status: Wrong Answer

Solution code:
s = input()
n = int(s.split()[0])
k = int(s.split()[1])

minV = n
current = n
absV = 0
if current - k > 100000000:
    current = current % n
while True:
    absV = abs(current - k)
    if current < absV:
        print(current)
        exit()
    else:
        current = absV

Submission status: Wrong Answer

Solution code:
N , K = (int(x) for x in input().split())

remin = N%K
print(K-remin)
print(remin)
ans = min(remin,K-remin)

print(ans)

Submission status: Wrong Answer

Solution code:
N , K = (int(x) for x in input().split())

remin = N%K


if remin > abs(K-remin) :
  print(K-remin)
else:
  print(K)

Submission status: Wrong Answer

Solution code:
n, k = map(int ,input().split())
if k == 1 or k == n:
    print(0)
else:
    ab = abs(n - k)
    if n == abs(ab - k):
        print(n)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
def sovle(x, y):
    while(x>=y):
        x=int(x-x/y*y)
    if(x>y-x):
        return y-x
    else:return x



if __name__ == '__main__':
    nums = list(map(int, input().split(" ")))
    print(sovle(nums[0], nums[1]))

Submission status: Wrong Answer

Solution code:
def sovle(x, y):
    while(x>=y):
        x=int(x-x/y)
    if(x>y-x):
        return y-x
    else:return x



if __name__ == '__main__':
    nums = list(map(int, input().split(" ")))
    print(sovle(nums[0], nums[1]))

Submission status: Wrong Answer

Solution code:
n, k = map(int ,input().split())
if k == 1:
    print(0)
else:
    ab = abs(n - k)
    if n == abs(ab - k):
        print(n)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))

while(True):
    x = abs(n-k)
    print(n,k,x)
    if n < x:
        print(n)
        break
    n = x

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
M=N%K
print(min(M,N-M))

Submission status: Wrong Answer

Solution code:
import fractions

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

print(fractions.gcd(n,k))

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))

print(n % k)

Submission status: Wrong Answer

Solution code:
N, K =map(int, input().split())
import fractions
gcd=fractions.gcd(N,K)
ans=min(gcd, K-gcd) 
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
if N == 0:
    print(0)
else:
    while True:
        if N//K >= 2:
            N = N//K
        else:
            break

    x0 = N #7
    x1 = abs(x0-K) #3
    while True:
        if x0 < x1:
            print(x0)
            break
        else:
            x0,x1 = x1,abs(x0-K)
            continue

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
while True:
    if N//K >= 2:
        N = N//K
    else:
        break
    
x0 = N #7
x1 = abs(x0-K) #3
while True:
    if x0 < x1:
        print(x0)
        break
    else:
        x0,x1 = x1,abs(x0-K)
        continue

Submission status: Wrong Answer

Solution code:
N, K = (int(x) for x in input().split())
print(N % K)

Submission status: Wrong Answer

Solution code:
n,k=(map(int,input().split()))

ans=0

if n<k:
  print(ans)
else:
  ans=min(int(n%k),abs(int(n%k)-k))
  print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int ,input().split())
if k == 1:
    print(0)
else:
    ab = abs(n - k)
    if ab == (n + k) / 2:
        print(n)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
ans = N
x = abs(N - K)
while ans > x:
    ans = x
    x = abs(x - K * (x // K))
print(ans)

Submission status: Wrong Answer

Solution code:
# input
N, K = map(int, input().split())

# check
if K == 1:
    print("0")
elif abs(N - K) > N:
    print(N)
else:
    print(abs(N % K - K))

Submission status: Wrong Answer

Solution code:
# input
N, K = map(int, input().split())

# check
if N == 1 or K == 1:
    print("0")
elif abs(N - K) > N:
    print(N)
else:
    print(abs(N % K - K))

Submission status: Wrong Answer

Solution code:
# input
N, K = map(int, input().split())

# check
if N == 1 or K == 1:
    print("0")
elif abs(N - K) > N:
    print(N)
else:
    ans = N
    while True:
        aft = abs(ans - K)

        if aft <= ans:
            ans = aft
        else:
            break

    print(ans)

Submission status: Wrong Answer

Solution code:
# input 
N, K = map(int, input().split())

# check
if N == 1 or K == 1:
    print("1")
elif abs(N - K) > N:
    print(N)
else:
    ans = N
    while True:
        aft = abs(ans - K)
        
        if aft <= ans:
            ans = aft
        else:
            break
    
    print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())

if n <= 1 or k == 1:
    print(n)
else:
    while(abs(n-k) < n):
        n = abs(n-k)
    print(n)

Submission status: Wrong Answer

Solution code:
inp=[int(x) for x in input().split()]
n,k=inp[0],inp[1]
if n>k:
  print(n%k,k-n%k)
elif n<k:
  print(min(n,k-n))
else:
  print(0)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
while n>0:
  n=n-k
print(abs(n))

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())

m = (a - a%b) / b
print(min(a - m * b, (m + 1) * b -a))

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())

m = (a - a%b) / b
print(int(min(a - m * b, (m + 1) * b -a)))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
a = N % K
if N >= K:
    print(min(a, abs(a-K)))
else:
    print(N)

Submission status: Wrong Answer

Solution code:
num = list(map(int, input().split()))

loop = [0]

for i in loop:
  if num[0] < num[1]:
    num[0] = abs(num[0] - num[1])
    break
  elif num[1] == 1:
    num[0] = 0
    break
  elif num[1] == 0:
    break
  num[0] = abs(num[0] - num[1])
  loop.append(i+1)

print(num[0])

Submission status: Wrong Answer

Solution code:
num = list(map(int, input().split()))

loop = [0]

for i in loop:
  num[0] = abs(num[0] - num[1])
  if num[0] < num[1]:
    num[0] = abs(num[0] - num[1])
    break
  elif num[1] == 1:
    num[0] = 0
    break
  elif num[1] == 0:
    break
  loop.append(i+1)

print(num[0])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())

if N%K == 0:
    print(0)

elif N<K and N<=K-N:
    print(N)

elif N<K and N>K-N:
    print(K-N)

elif N>K:
    print(N%K)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())

m=n%k
d=abs(m-k)

if m<k:
    print(m)
else:
    print(k)

Submission status: Wrong Answer

Solution code:

n,k=map(int,input().split())
#n=7
#k=4

m=n%k
d=abs(m-k)

if m<k:
    print(m)
elif k<m:
    print(k)
else:
    print(k)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
if K==1:
  if N==0:
    print("1")
    exit()
  else:
    print("0")
    exit()
if N==0:
  print(K)
  exit()
if N > K :
  N-=(N//K)*K
if abs(K-N) < N:
  print(abs(K-N))
else:
  print(N)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
if K==1:
  if N==0:
    print("1")
    exit()
  else:
    print("0")
    exit()
if N==0:
  print(abs(K-N))
  exit()
if N > K :
  N-=(N//K)*K
if abs(K-N) < N:
  print(abs(K-N))
else:
  print(N)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
if K==1:
  if N==0:
    print("1")
  else:
    print("0")
if N > K :
  N-=(N//K)*K
if abs(K-N) < N:
  print(abs(K-N))
else:
  print(N)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
if K==1:
  print("0")
if N > K :
  N-=(N//K)*K
if abs(K-N) < N:
  print(abs(K-N))
else:
  print(N)

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    
    a,b = divmod(N, K)
    l = list()
    for i in range(N):
        mi = 0
        if b < abs(b - K):
            mi = b
        else:
            mi = abs(b - K)
        if mi in l:
            print(min(l))
            break
        else:
            l.append(mi)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    if N== 1:
        print(1)
    else:
        if N % K == 0:
            print(0)
        else:
            a,b = divmod(N, K)
            l = list()
            for i in range(N):
                mi = 0
                if b < abs(b - K):
                    mi = b
                else:
                    mi = abs(b - K)
                if mi in l:
                    print(min(l))
                    break
                else:
                    l.append(mi)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    if N % K == 0:
        print(0)
    else:
        a,b = divmod(N, K)
        l = list()
        for i in range(N):
            mi = 0
            if b < abs(b - K):
                mi = b
            else:
                mi = abs(b - K)
            if mi in l:
                print(min(l))
                break
            else:
                l.append(mi)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
x = abs(N - K)
 
if N > 1000000000000000*K:
    while x > K:
        x = abs(x - 1000000000000000*K)
        if x < K:
            break
 
while x > K:
    x = abs(x - K)
    if x < K:
        break
 
A = []
for i in range(5):
    A.append(abs(x - K))     
print(min(A))

Submission status: Wrong Answer

Solution code:

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

po=(N//K+1)*N-N
ne=N//K

print(min(po,ne))

Submission status: Wrong Answer

Solution code:

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



y = abs(N - K)
N = y
y2 = abs(N - K)
c=1
dif = (y, y2)

if dif[c-1] <= dif[c]:
    print(dif[-1])
    
if dif[c-1] > K:

    dif += (dif[c-1]%K,)
    c += 1

while dif[c-1] > dif[c]:

    yy = abs(dif[c] - K)
    dif += (yy,)
    c += 1


print(min(dif))

Submission status: Wrong Answer

Solution code:
#c Aoki-kun

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



y = abs(N - K)
N = y
y2 = abs(N - K)
c=1
dif = (y, y2)

if dif[c-1] <= dif[c]:
    print(dif[-1])
    
if dif[c-1] > K:

    dif += (dif[c-1]-(dif[c-1]-K),)
    c += 1

while dif[c-1] > dif[c]:

    yy = abs(dif[c] - K)
    dif += (yy,)
    c += 1


print(min(dif))

Submission status: Wrong Answer

Solution code:
#c Aoki-kun

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



y = abs(N - K)
N = y
y2 = abs(N - K)
c=1
dif = (y, y2)

if dif[c-1] <= dif[c]:
    print(dif[-1])
    
if dif[c-1] > K:

    dif += (dif[c-1]-(dif[c-1]-K),)

while dif[c-1] > dif[c]:

    yy = abs(dif[c] - K)
    dif += (yy,)
    c += 1


print(min(dif))

Submission status: Wrong Answer

Solution code:

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



y = abs(N - K)
N = y
y2 = abs(N - K)
c=1
dif = (y, y2)

if dif[c-1] <= dif[c]:
    print(N)

while dif[c-1] > dif[c]:

    yy = abs(dif[c] - K)
    dif += (yy,)
    c += 1


print(min(dif))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
a = abs(K-N*(N//K))
b = abs(K-N*(N//K+1))
if a >= b:
    print(b)
else:
    print(a)

Submission status: Wrong Answer

Solution code:
a, b = list(map(int, input().split()))

n = max(a, b)
k = min(a, b)
prev = 0

while (k != 0):
    prev = k
    a = int(n/k)
    b = n % k
    n = max(a, b)
    k = min(a, b)
    # print(n, k, prev)

print(prev)

Submission status: Wrong Answer

Solution code:
a, b = list(map(int, input().split()))

n = max(a, b)
k = min(a, b)
prev = 0

while (k != 0):
    prev = k
    a = int(n/k)
    b = n % k
    n = max(a, b)
    k = min(a, b)
    print(n, k, prev)

print(prev)

Submission status: Wrong Answer

Solution code:
n , k = map(int , input().split())
ans = n % k
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = (int(x) for x in input().split())
i = int(n/k)
f = n/k - i
if f<= 0.5:
  print(abs(n - i*k))
else:
  print(abs(n - (i + 1)*k))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
if n%k == 0:
  print(0)
else:
  i = int(n/k)
  f = abs(n/k - i)
  if f<= 0.5:
    print(abs(n - i*k))
  else:
    print(abs(n - (i + 1)*k))

Submission status: Wrong Answer

Solution code:
n, k = (int(x) for x in input().split())
if n%k == 0:
  print(0)
elif n < 0 and k > 0:
  print(abs(n))
else:
  i = int(n/k)
  f = abs(n/k - i)
  if f<= 0.5:
    print(abs(n - i*k))
  else:
    print(abs(n - (i + 1)*k))

Submission status: Wrong Answer

Solution code:
n, k = (int(x) for x in input().split())
if n%k == 0:
  print(0)
else:
  i = int(n/k)
  f = abs(n/k - i)
  if f<= 0.5:
    print(abs(n - i*k))
  else:
    print(abs(n - (i + 1)*k))

Submission status: Wrong Answer

Solution code:
n, k = (int(x) for x in input().split())
if n%k == 0:
  print(0)
else:
  i = int(n/k)
  f = abs(n/k) - (i)
  if f<= 0.5:
    print(abs(n - i*k))
  else:
    print(abs(n - (i + 1)*k))

Submission status: Wrong Answer

Solution code:
n, k = (int(x) for x in input().split())
if n%k == 0:
  print(0)
else:
  i = int(n/k)
  f = abs(n/k) - abs(i)
  if f<= 0.5:
    print(abs(n - i*k))
  else:
    print(abs(n - (i + 1)*k))

Submission status: Wrong Answer

Solution code:
n, k = (int(x) for x in input().split())
if n%k == 0:
  print(0)
else:
  i = int(n/k)
  f = n/k - i
  if f<= 0.5:
    print(abs(n - i*k))
  else:
    print(abs(n - (i + 1)*k))

Submission status: Wrong Answer

Solution code:
n, k = (float(x) for x in input().split())
if n%k == 0:
  print(0)
else:
  i = int(n/k)
  f = n/k - i
  if f<= 0.5:
    print(abs(n - i*k))
  else:
    print(abs(n - (i + 1)*k))

Submission status: Wrong Answer

Solution code:
import math
N,K=map(int,input().split())
small=math.floor(N/K)
b=abs(N-K*small)
a=abs(K-b)
print(min(a,b))

Submission status: Wrong Answer

Solution code:
import math
N,K=map(int,input().split())
small=math.floor(N/K)
b=abs(N-K*small)
a=abs(b-K)
print(min(a,b))

Submission status: Wrong Answer

Solution code:
import math
N,K=map(int,input().split())
small=math.floor(N/K)
b=abs(N-K*small)
a=abs(b-K)
print(min(N,a,b))

Submission status: Wrong Answer

Solution code:
import math
N,K=map(int,input().split())
large=math.ceil(N/K)
small=math.floor(N/K)
a=abs(N-K*large)
b=abs(N-K*small)
print(min(N,a,b))

Submission status: Wrong Answer

Solution code:
i = [int(x) for x in input().split()]
n = i[0]
k = i[1]

if n % k == 0:
    print(0)
    exit()

n = n % k
if k % n != 0:
    n = k - n
print(n)

Submission status: Wrong Answer

Solution code:
LOOP_COUNT = 100
i = [int(x) for x in input().split()]
n = i[0]
k = i[1]

for i in range(LOOP_COUNT):
    if n >= k:
        n = n % k
    elif n == 0:
        break
    else:
        n = k - n
print(n)

Submission status: Wrong Answer

Solution code:
TRY_COUNT = 10000

def abs(i):
    if i < 0:
        i *= -1
    return i

i = [int(x) for x in input().split()]
n = i[0]
k = i[1]
l = []

for i in range(TRY_COUNT):
    n = abs(n - k)
    l.append(n)

print(min(l))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())

if n<=(k/2):
    print(n)

n = n - (n//k)*k
if n<= k/2:
    print(n)
else:
    print(abs(n-k))