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: Accepted

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

N = N - (N//K)*K
while True:
    N1 = abs(K-N)
    if N1 >= N:
        break
    N = N1
print(N)

Submission status: Accepted

Solution code:
import numpy as np

tmp = list(map(int, input().split()))

N = tmp[0]
K = tmp[1]


# i = N % K


# if N > K:
#     N = N + K

# solve = [0] * (K+1)

# for i in range(0, K-1):
#     solve[i] = N % (K - i)

solve = N % K
if solve > abs(solve - K):
    solve = abs(solve - K)

print(solve)

# solve.reverse()

# for iter in range(0,K-1):
#     if solve[iter] == 0:
#         continue
#     else:
#         print(solve[iter])
#         break
# print(0)

Submission status: Accepted

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

N = a[0]
K = a[1]

if K >= 2*N:
    print(N)
elif K > N :
    print(abs(N-K))
elif N >= K:
    res = N%K
    min = res
    cul_result1 = abs(res-K)
    if cul_result1 < min:
        min = cul_result1
    cul_result2 = abs(cul_result1-K)
    if cul_result2 < min:
        min = cul_result2
    print(min)

Submission status: Accepted

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

  if N >= K:
    x = N%K
  else:
    x = abs(N-K)

  while(True):
    tmp_abs = abs(x-K)
    if x > tmp_abs:
      x = tmp_abs
    else:
      break
  
  print(x)

if __name__ == "__main__":
  main()

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
if N % K > K / 2:
    F = N // K + 1
else: 
    F = N // K
r = [
    abs(N),
    abs((N - (K * F)))
]
print(min(r))

Submission status: Accepted

Solution code:
import numpy as np
import fractions
N, K = [int(i) for i in input().split(' ')]

if N == 0:
	result = 0
elif K == 0:
	result = N
elif N % K == 0:
	result = 0
elif K % N == 0:
	result = K/N if N > K/N else N
elif N > K:
  result = N % K if N % K < K - N % K else K-N%K
elif N < K:
  result = K-N if N > K - N else N
  
print("{}".format(result))

#lis = np.array([int(n) for n in input().split(' ')])
#print("{} {}".format(sum(lis) / 4 / int(M), lis[int(M)-1]))

#count = sum(sum(lis) / 4.0 / int(M) <= lis)

Submission status: Accepted

Solution code:
"""
青木君は任意の整数 x

に対し、以下の操作を行うことができます。

操作: x
を x と K

の差の絶対値で置き換える。

整数 N
の初期値が与えられます。この整数に上記の操作を 0 回以上好きな回数行った時にとりうる N

の最小値を求めてください。
制約
"""
n,k=map(int,input().split())
x=n
if k==x:
    r=0
elif k>x:
    r=min(x,(k-x))
else:
    if (x-k)>=k:
        r=min(k-(x-k)%k,(x-k)%k)
    else:
        r=min(k-k%(x-k),k%(x-k))
print(r)

Submission status: Accepted

Solution code:
def absolute(N,K):
    return abs(N-K)
    
N,K=map(int,input().split())
N=N%K
valuelist = [N%K]

for i in range (0,pow(10,1)):
    i+=1
    N=absolute(N,K)
    valuelist.append(absolute(N,K))
    
print(min(valuelist))

Submission status: Accepted

Solution code:
aa=list(map(int,input().split()))
n=aa[0]
k=aa[1]
minn=n%k
print(min(n%k,abs((n%k)-k)))

Submission status: Accepted

Solution code:
array = list(map(int, input().split()))
tmp = array[0] % array[1]
if (tmp <= array[1] / 2):
  print(tmp)
else:
  print(abs(tmp-array[1]))

Submission status: Accepted

Solution code:
#coding: utf-8
A = list(map(int, input().split()))
N = A[0]
K = A[1]

n = 0
sw = 0
while True:
  if N%K == 0:
    N = 0
    break
  if (N/K) //1:
    C = N//K
    N = N-C*K

  sw = abs(N-K)
  n += 1
  if N > sw:
    N = sw
  else:
    break
print(N)

Submission status: Accepted

Solution code:
N,K = map(long, raw_input().split())
d = long(N/K)
p = [N, abs(N - K*(d-1)), abs(N - K*d), abs(N - K*(d+1))]
print int(min(p))

Submission status: Accepted

Solution code:
import numpy as np
l_input = [int(e) for e in input().split()]
N = l_input[0]
K = l_input[1]
result = [N%K]
for i in range(1000000000000000000000000000000):
    result.append(abs(result[i]-K))
    ans = result[i+1] in result
    if result[i+1] < K/2:
        break
print(min(result))

Submission status: Accepted

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

Submission status: Accepted

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

if n > k:
    if n%k == 0:
        print(0)
    else:
        ans = n%k
        if ans < abs(ans-k):
            print(ans)
        else:
            print(abs(ans-k))
        
elif n < k:
        if n < abs(n-k):
            print(n)
        else:
            print(abs(n-k))
else:
    print('0')

Submission status: Accepted

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

q, r = divmod(n, k)

if r == 0:
    print(0)
    exit()

if q < 1:
    if n < (k - r):
        print(n)
    else:
        print(k - r)

else:
    s = n - q * k
    t = abs(s - k)
    if s < t:
        print(s)
    else:
        print(t)

Submission status: Accepted

Solution code:
N, K = list(map(int, input().split()))
M = abs(N - K)
mini = M + 1

if (not N < K):
    if (N%K == 0):
        print(0)
    else:
        M = (N % K)
        while (M < mini):
            mini = min(mini, M)
            M = abs(M - K)
        print(mini)

else:
    while (M < mini):
        mini = min(mini, M)
        M = abs(M - K)
    print(mini)

Submission status: Accepted

Solution code:
N,K = map(int,input().split(' '))
first = N - (K*(N//K))
second = abs(first - K)
final = [first,second,K]
print(min(final))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
ans = int(N)
N_1 = N
N_2 = N
k_range = N // K
# print(k_range)

if k_range == 0:
    N = N - K
    if ans > abs(N):
        ans = abs(N)
else:
    N_1 = N - (K * k_range)
    N_2 = N - (K * (k_range + 1))

if abs(N_1) < abs(N_2):
    if ans > abs(N_1):
        ans = abs(N_1)
    else:
        pass
else:
    if ans > abs(N_2):
        ans = abs(N_2)
    else:
        pass
print(ans)

Submission status: Accepted

Solution code:
import fractions
import sys
N,K = map(int,input().split())
L = []
L.append(N)
i = 0
if N == K:
    print(0)
    sys.exit(0)
if N == 0:
    print(0)
    sys.exit(0)
if K == 1:
    print(0)
    sys.exit(0)
Q = 0
N-=((N//K)*K)
while True:
  L.append(abs(N-K))
  N = abs(N-K)
  i += 1
  if i > 10**5:
    Q = min(L)
    break
if N%K == 0 and N>=K:
    print(0)
else:
    if N < K:
        print(min(N,Q))
    else:
        print(min(N%K,Q,fractions.gcd(N,K)))

Submission status: Accepted

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

Submission status: Accepted

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

a=n%k

b=abs(a-k)

if a<b:
  print(a)
else:
  print(b)

Submission status: Accepted

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

rest = N % K

sol = min([N, rest, abs(rest - K)])
print(sol)

Submission status: Accepted

Solution code:
#!/usr/bin/env python

t = input().split()
n = int(t[0])
k = int(t[1])

a = 0
if n%k == 0:
    a = 0
else:
    e = n%k
    a = abs(k-e)
    if a > e:
        a = e
print(a)

Submission status: Accepted

Solution code:
n, k = map(int,input().split())
if n % k == 0:
    print(0)
else:
    l = [n]
    p = n//k
    c = n-(p*k)
    half = k//2
    while c > half:
        c = abs(c-k)
    l.append(c)
    print(min(l))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

# def solve(n: int, k: int) -> int:
def solve(n, k):
    n -= max(0, n // k - 10) * k
    ans = n
    for _ in range(100):
        n = abs(n - k)
        ans = min(ans, n)
    return ans

# generated by online-judge-template-generator (https://github.com/kmyk/online-judge-template-generator)
def main():
    N, K = map(int, input().split())
    ans = solve(N, K)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

def absdiff(x, K):
    return abs(x - K)

def culc(N, K):
    if K == 1:
        print(0)
        return

    if N*2 == K:
        print(N)
        return

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

    x = N % K
    min = x
    while min >= x:
        #print(min, x)
        min = x
        x = absdiff(x, K)
        if min == 1:
            break

    print(min)
    return


culc(N, K)

Submission status: Accepted

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

tmp = N % K

if tmp == 0:
    print(0)
elif N < tmp:
    print(N)
elif tmp < (K - tmp):
    print(tmp)
else:
    print(abs(K - tmp))

Submission status: Accepted

Solution code:
#coding:utf-8
import math
n, k = map(int, input().split())

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
if n % k == 0:
    print(0)
else:
    l = [n]
    p = n // k
    c = n - (p * k)
    half = k // 2
    while c > half:
        c = abs(c - k)
    l.append(c)
    print(min(l))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
c = 0
a = []

while 1:
    if K==1:
        print(0)
        break
    if N==0:
        print(0)
        break
    if N==1:
        print(1)
        break
    
    for i in range(2):
        if i==0:
            N = N-((N//K)*K)
        N = abs(N-K)
        a.append(N)
    b = min(a)
    print(b)  
    break

Submission status: Accepted

Solution code:

n, k = [int(i) for i in input().split()]
if n >= k:
	remain = n % k
	if remain > (k//2): 
		result = abs(remain-k)
	else:
		result = remain
else:
	if n > (k//2): 
		result = abs(n-k)
	else:
		result = n

print(result)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
if n%k==0:
 print("0")
 exit()
a=n%k
b=c=ans=abs(a-k)
while b!=abs(c-k):
 c=abs(c-k)
 if c<ans:
  ans=c 
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
if k==1 or n%k==0:
    ans=0
else:
    if n/2<k:
        x=abs(n-k)
        y=abs(x-k)
        ans=min(n,min(x,y))
    else:
        x=n%k
        y=abs(x-k)
        ans=min(x,y)
print(ans)

Submission status: Accepted

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

E = N % K

if (E == 0):
  print('0')
else:
  if E < abs(E - K):
    print(E)
  else:
    print(abs(E - K))

Submission status: Accepted

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

while n > abs(n-k):
    if k == 1:
        n = 0
        break
    elif n%k == 0:
        n = 0
        break

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

print(n)

Submission status: Accepted

Solution code:
import math
def main(cell):

    n=cell[0]
    k=cell[1]
    if k==0:
        return n
    else:

        risk=math.floor(n/k)
        if risk ==0:
            n=n
        else:
            n=abs(n-k*risk)
        pren=n
        while 2>0:
            n=abs(n-k)
            if n >pren:
                break
            else:
                pren=n

        return pren
    


cell=list(map(int,input().split()))
pren=main(cell)
print(pren)

Submission status: Accepted

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

x = n//abs(k)
ans = n-(x*k)
ans_x = abs(n-(k*(x+1)))
if ans >= ans_x:
  print(ans_x)
else:
  print(ans)

#print(ans_x,ans)

Submission status: Accepted

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

Submission status: Accepted

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

def okikae(t, k):
    if k==0:
        print(t)
    elif int(k/2)+1 > t:
        print(t)
    elif t%k==0:
        print(0)
    elif t>k and t%k > 0:
        t = k+(t%k)
        okikae(abs(t-k), k)
    else:
        okikae(abs(t-k), k)

okikae(N, K)

Submission status: Accepted

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

Submission status: Accepted

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



for i in reversed(range(18)):
    while N > 10 ** i * K:
        N=N-10 ** i * K


while abs(N-K)<N:

    N=abs(N-K)

print(N)

Submission status: Accepted

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

a = N % K
b = abs(a-K)
if a<b:
    print(a)
else:
    print(b)

Submission status: Accepted

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

n = a % b

print(min(a,abs(n - b),n))

Submission status: Accepted

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

while N!=0:
  if N>=abs(N-K):
    N = abs(N-K)
  elif N==0:
    break  
  else:
    break
print(N)

Submission status: Accepted

Solution code:
import itertools
import math
import fractions

n,k = map(int,input().split())
ans = n
s =[]
if k == 1 or n == 0 or n==k :
    print("0")
    exit()
elif n < k:
    ans = abs(n-k)
    print(min(ans,n))
    exit()

elif n > k:
    ans = n%k

while(1):
    s.append(ans)
    ans = abs(ans-k)
   # print(ans)
    if s.count(ans):
        ans=abs(ans-k)
        break

print(ans)

Submission status: Accepted

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

# print(N % K)
# print(N % abs(N-K))

# SN = N
# PN = float('inf')
# while PN > N:
#     PN = N
#     N = abs(N-K)
#     print(PN, N)

if N == K:
    print(0)

elif N > K:
    val = N % K
    print(min(abs(val), abs(val-K)))
else:
    SN = N
    PN = float('inf')
    while PN > N:
        PN = N
        N = abs(N-K)
    print(min(SN, PN))

Submission status: Accepted

Solution code:
n, k = map(int, input().strip().split(" "))
abs = lambda x: -x if x < 0 else x
x = n % k
if abs(x - k) > x:
    print(x)
else:
    print(abs(x - k))

Submission status: Accepted

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

middle = N % K
answer = middle
while True:
  if abs(answer-K) > N:
    answer = N
    break
  if abs(answer - K) < answer:
    answer = abs(answer-K)
  else:
    break
    
print(str(answer))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
 
q = N // K
Na = N - q*K
Nb = abs(Na - K)
 
if Na >= Nb:
  	print(Nb)
else:
  	print(Na)

Submission status: Accepted

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

if N%K != 0:
    print(min(N%K, K-N%K))
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

min_n = 1000000000000000000000000000
for i in range(1000):
    if n > k:
        n %= k
    n = abs(n - k)
    min_n = min(min_n, n)

print(min_n)

Submission status: Accepted

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

if k == 0:
	ans = n
else:
	rem = n % k
	a = rem - k
	ans = min(abs(a), rem)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
l = N//K
a = 0
b = 0

if N%K == 0:
    print(0)
else:
    a = abs(N-(K*l))
    b = abs(a-K)
    if a >= b:
        print(b)
    elif a <= b:
        print(a)

Submission status: Accepted

Solution code:
def inputs(): return [int(x) for x in input().split()]
n,k = inputs()
a = n//k
b = (n+k-1)//k
ans = min(abs(n-a*k),abs(n-b*k),n)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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


if(N > K):
    if(N % K == 0):
        print(0)
        sys.exit()
    else:
        tmp=N % K
        print(min(tmp,abs(K - tmp)))
        sys.exit()
else:
    tmp = abs(N - K)
    if(tmp <= N):
        print(tmp)
    else:
        print(N)

Submission status: Accepted

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

c=abs(n-k)
d=abs(c-k)
if(c+d==k):
    if(c>d):
        print(d)
    else:
        print(c)
    exit()
if(n>=k):
    if(n%k==0):
        print(0)
        exit()
    else:
        a=n-k*(n//k)
        b=k*(n//k+1)-n
        if(a>b):
            print(b)
        else:
            print(a)
        exit()
else:
    
    while(1):
        a=abs(n-k)
        if(n>a):
            n=a
        else:
            print(n)
            exit()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
if N <= K:
    while True:
        if N > abs(N - K):
            N = abs(N - K)
        else:
            break
else:
    N = N % K + K
    while True:
        if N > abs(N - K):
            N = abs(N - K)
        else:
            break
print(N)

Submission status: Accepted

Solution code:
n, k = [int(x) for x in input().split()]
m = n % k
mt = abs(m - k)
if (mt < m):
    m = mt
print(m)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import math
from decimal import *
getcontext().prec = 70

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


if N<= K:
    print(min(N,K-N))
else:

    N = N - math.floor(Decimal(N)/Decimal(K))*K 
    print(min(N, K-N))

Submission status: Accepted

Solution code:
#!/usr/bin/python3
import copy

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

x = n

if x >= k:
    x %= k

print(min(x, k - x))

Submission status: Accepted

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

if n % k == 0:
    print(0)
elif n < k:
    if k/2 < n:
        rem = n % k
        if abs(rem-k) < k:
            print(abs(rem-k))
        else:
            print(rem)
    else:
        print(n)
else:
    rem = n % k
    if abs(rem-k) < k:
        print(abs(rem-k))
    else:
        print(rem)

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
tmpNum = n
subtraction = n
if n % k == 0:
  print(0)
else:
  tmp = int(n/k)
  subtraction = n - (tmp*k)
  while(1):
    subtraction = abs(subtraction-k)
    if subtraction < tmpNum:
      tmpNum = subtraction
    else:
      break
  print(tmpNum)

Submission status: Accepted

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

Submission status: Accepted

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

aaa = (n % k)

if aaa == 0:
  ret = 0
else:
  if n > k:
    ret = min(abs(aaa - k), aaa)
  else:
    ret = min(k - n, n)

print(ret)

Submission status: Accepted

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

if K==1 or N%K==0:
  print(0)
  exit()

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

Submission status: Accepted

Solution code:
N, K = list(map(int,input().split()))
d = N//K
S0 = N - d * K
S1 = S0 - K
Sa = abs(S1)
if S0 <= Sa:
  print(S0)
else:
  print(Sa)

Submission status: Accepted

Solution code:
N,K =  map(int, input().split())
i=1
X=N%K
A=[]
A.append(X)
if K!=1:
    while True:
        A.append(abs(A[i-1]-K))
        if A[i] in (A):
            break
        i+=1
    print(min(A))
else:
    print(0)

Submission status: Accepted

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

amari = N % K
absplus = abs(amari - K)


if N <= (K // 2):
    print(N)
elif amari >= absplus:
    print(absplus)
else:
    print(amari)

Submission status: Accepted

Solution code:
N, K = map( int, input().split() )
 
now_N = 10 ** 19
while True:
  N = min( abs( N % K ),abs( K - N ), now_N )
  if now_N == N:
    break
  now_N = N
print( now_N )

Submission status: Accepted

Solution code:
N,K=(int(x) for x in input().split())
x=N%K
if K!=1:
     while True:
        x = abs(x-K)
        y = abs(x-K)
        if y>x:
            break
     print(x)
else:
    print(0)

Submission status: Accepted

Solution code:
def RA():
    l = input().split()
    return [int(i) for i in l]

arr = RA()
r = arr[0] % arr[1]
rr = arr[1] - r
ans = r if r < rr else rr
print(ans)

Submission status: Accepted

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

print(min(n%k, abs(n%k-k)))

Submission status: Accepted

Solution code:
def calc(n, k):
    if n == 0:
        return 0
    if n > k:
        a = n%k
        return min(abs(a-k), a)
    # kのほうがnより大きいとき
    if n*2 < k:
        return n
    else:
        # k = k%n+n+n
        return min(abs(n-k), k-abs(n-k))

n, k = (int(each) for each in input().split())
print(calc(n, k))

Submission status: Accepted

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

if n==0:
    print(0)
else:
    while result > abs(result-k):
        result=abs(result-k)
    print(result)

Submission status: Accepted

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

if N % K == 0:
    print('0')
elif K % N == 0:
    print(N)
elif N <= K:
    print(min(N, abs(N-K)))
elif N > K:
    print(min(abs(N-K), abs(K-(N % K))))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
min_num = new_min = n % k
while True:
  new_min = new_min - k
  if new_min < 0:
    new_min *= -1
  if min_num > new_min:
    min_num = new_min
  else:
    break
print(min_num)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
i = 1
x = N
ans = min(x%K,K-x%K)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

if N > K:
    N = N - K*(N//K)

if N==K:
    print(0)
else:
    print(min(N,K-N))

Submission status: Accepted

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

N = N % K

print min(N, abs(K-N))

Submission status: Accepted

Solution code:
#python3
def main():
    n, k = map(int, input().split())
    if n%k == 0:
        print(0)
        return
    x = n % k
    ans = min(k - x, n)
    print(ans)
main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = input().split(" ")
n = int(n)
k = int(k)
min = 1000000000000000001
abs0 = 0
if n == 0:
    print(0)
elif k == 1:
    print(0)
elif n == 1:
    print(1)
else:
    if n > k:
        n = n % k
        print(abs(k-n))
    else:        
        for i in range(1000000000):
            abs0 = abs(k - n)
            n = abs0
            if abs0 < min:
                min = abs0
            elif min == abs0:
                print(abs0)
                break

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

a = n % k
b = abs(a-k)
if a > b:
  print(b)
else:
  print(a)

Submission status: Accepted

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

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

Submission status: Accepted

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

if N==0:
    print(N)
elif N>=K:
    #print("1")
    x1=N%K
    x2=abs(N%K-K)
    print(min(x1,x2))
else:
    #print("2")
    x1=N
    x2=abs(N-K)
    print(min(x1,x2))

Submission status: Accepted

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

if abs(N - K) >= N:
    print(N)
    exit()


dec = N - abs(N - K)
q, r = divmod(N, dec)

n = abs(r - K)
while n < r:
    r = n
    n = abs(r - K)

print(r)

Submission status: Accepted

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

Submission status: Accepted

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

if n >= k:
    n = n%k
    n_abs = abs(n-k)
    if n <= n_abs:
        print(n)
    else:
        print(n_abs)
elif n < k and k < 2*n:
    n_abs = abs(n-k)
    if n <= n_abs:
        print(n)
    else:
        print(n_abs)
else:
    print(n)

#nに対して操作を行う。
#操作とは、nとkの差の絶対値をとること。
#この操作を２回行えば起こりうる事象を全て観測した事になる。
#ただしnとkの値が大きいので、商とか使う。


#Plan 
# n >= k   割り算でnを小さくしてからalgorithm1
# n < k n < k < 2n　の時と2n <= kの時でさらに分岐？ 後者はアウト。前者はalgorithm2

#algorithm1 →　割れないほど小さくなってから、nで絶対値とったらさらに小さくなるかどうか。この時nは2k未満である。差を作って小さい方を選ぶ。
#algorithm2 → algorithm1と同じっぽい。

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8


def main():
    n, k = map(int, input().split())
    return min(n % k, k - n % k)


print(main())

Submission status: Accepted

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

n %= k

if abs(n-k) < n:
    n = abs(n-k)

print(n)

Submission status: Accepted

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

print(min(abs(max-k), max))

Submission status: Accepted

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

num = N % K
min_num = abs(num - K)

if num == 0:
  print(0)
elif N < K:
  if N < min_num:
  	print(N)
  else:
    print(min_num)
else:
  print(min_num)

Submission status: Accepted

Solution code:
N, K = map( int, input().split())
# if N%K == 0:
#     print(0)
#     return
print( min(N%K, K - N%K))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
mod = N % K
if mod > K / 2:
    mod = K - mod
print(mod)

Submission status: Accepted

Solution 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)

Submission status: Accepted

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

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

Submission status: Accepted

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

if n == 0:
  print(0)
elif n % k==0:
  print(0)
elif n >k:
  while n > abs(n - k):
    n = min(abs(n - k),n % k)
  print(n)
else:
  while n > abs(n - k):
    n = abs(n - k)
  print(n)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
n = int((N/K + 0.5)//1)
i = abs(n*K-N)
while i >= K/2:
    i = abs(K-i)

print(i)

Submission status: Accepted

Solution code:
import math

NK = input().split();
N,K = int(NK[0]),int(NK[1]);
min = N;

#桁が大きい時の処理
if(K == 1):
    while(int(len(str(N))) > 2):
        N = int(N - K * (N / 2));
else:
    border = math.floor(N / K);
    N = N - K * border;
        
flag = True;
while flag == True:
    N = abs(N - K);
    if(N <= min):
        min = N;
    else:
        flag = False;
    
print(min);

Submission status: Accepted

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

if k==1:
    print(0)
elif n>k:
    print(k-(n%k))
else:
    val=k-n
    if n<val:
        print(n)
    else:
        print(val)

Submission status: Accepted

Solution code:
N,K=(int(x) for x in input().split())
N=N%K
a = abs(K-N)
if a<N:
  print(a)
else:
  print(N)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N, K = map(int, input().split())
j = 0
i = 3
A = 1000000000000000001
B = 1000000000000000002
C = 1000000000000000003
if N<K:
    M = N-K
    M = abs(M)
    
    if M<N:
        print(M)
    else:
        print(N)
elif N==K:
    print(0)
elif N%K==0:
    print(0)
else:
    N = N%K
    while j==0:
        #print(N)
        #print("aaa")
        N = N-K
        N = abs(N)
        if i%3==0:
            A = N
        elif i%3==1:
            B = N
        elif i%3==2:
            C = N
        
        if A==C:
            if A<=B:
                print(A)
            else:
                print(B)
            j+=1
        elif B==A:
            if A<=C:
                print(B)
            else:
                print(C)
            j+=1
        elif B==C:
            if B<=A:
                print(C)
            else:
                print(A)
            j+=1
        i+=1

Submission status: Accepted

Solution code:
def q3():
    N, K = (int(i) for i in input().split())
    d,m = divmod(N,K)
    ans = min(m, abs(m - K))
    print(ans)


if __name__ == '__main__':
    q3()

Submission status: Accepted

Solution code:
import sys

n,k= map(int,input().split())
v={}

min = sys.maxsize

if n == 0 :print(0);exit()

if n > k :
    x = n%k
    if x == 0 or x > k: 
        print(x);exit()
    else:
        n=x
        v[x] = True

while True:
    diff = abs(n- k)

    if v.get(diff):
        break
    else:
        # print(diff)
        v[diff] = True
        n = diff

        if diff < min:
            min = diff



print(min)

Submission status: Accepted

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

print(min(l, abs(l - k)))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

if K == 1:
    N = 0

else:
    if N // K > 0:
        i = N//K
        N = N - (K * i)
    while abs(N-K)<N:
        N = abs(N-K)
print(N)

Submission status: Accepted

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

if n < k:
    tmp = abs(n-k)
    print(min(tmp, n))
else:
    tmp = n%k
    while True:
        a = tmp-k
        b = abs(a)
        if a < 0:
            print(min(tmp, b))
            break

Submission status: Accepted

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

Submission status: Accepted

Solution code:
nums = [int(e) for e in input().split()]
Min=nums[0]
y=nums[1]
ans = Min % y
t = True
if (ans <= Min):
    Min = ans
    while t:
        a = abs(Min - y)
        if(a<Min):
            Min = a
        elif(a>Min):
            break
    print(Min)
else:
    print(ans)

Submission status: Accepted

Solution code:
n, k = list(map(int, input().split(' ')))
result = 0
if k < n:
    result = min(n%k, k-(n%k))
elif n < k:
    result = min(n, k-n)
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a,b = map(int,input().split())
modn = a % b
dif = abs(modn - b)
if dif < modn:
    print(dif)
else:
    print(modn)

Submission status: Accepted

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

r = n%k
w = abs(r-k)
print(min(r,w))