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

Solution (Python):
import sys
input = sys.stdin.readline
 
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))


def main():
    
    N,K=MI()
    a=N%K     
    b=K-N%K   
    print(min(a,b))
 
main()

Solution (Python):
def c_replacing_integer():
    N, K = [int(i) for i in input().split()]
    return min(N % K, K - (N % K))

print(c_replacing_integer())

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

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

Solution (Python):
n, k = map(int, input().split()) 
t = n % k
if (k-t > t):
    print(t)
else:
    print(k-t)

Solution (Python):
N, K = [int(n) for n in input().split()]
print(min(N%K, K-(N%K)))

Solution (Python):
import math

if __name__ == '__main__':
    s = input().rstrip().split(' ')
    N,K = int(s[0]), int(s[1])
    if(N>=K):
        
        
        cand1 = int(N%K)
        cand2 = int(K-(N%K))
        print(min(cand1, cand2))
    else:
        print(min(int(abs(N-K)),N))

Solution (Python):
import sys
import math

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

pre=n-n//k*k

while 1:
    if pre <= abs(pre-k):
        print(pre)
        sys.exit()
    pre = abs(pre - k)

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

Solution (Python):
import math

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

be=n%k
af=abs(k-be)

print(be if be<=af else af)

Solution (Python):
import sys

N,K = [int(i) for i in input().split(" ")]

tmp = N

if N >= K:
    if N % K == 0:
        print(0)
        sys.exit()
    else:
        N = N % K

while True:
    
    if abs(N - K) < tmp:
        N = abs(N - K)
    else:
        print(tmp)
        sys.exit()
    tmp = N

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

a = 0
b = 0
c = 0

a = n//k
b = abs(n-k*(a+1))
c = abs(n-k*a)
if b < c:
	print(b)
elif c < b:
	print(c)

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

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

if n >= k:
    n %= k

print(min(n, k-n))

Solution (Python):

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

while True:
  if abs(min-k)>=min:
    break
  else:
    min=abs(min-k)

print(min)

Solution (Python):
a = list(map(int, input().split()))
if a[0] == 0 or a[1] ==1:
  print(0)
else:
  if a[0]%a[1] > a[1]/2:
    print(a[1] - a[0]%a[1])
  else:
    print(a[0]%a[1])

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

Solution (Python):
N, K = map(int, input().split())
t = N % K

if t > (K -t):
    print(K-t)
else:
    print(t)

Solution (Python):
import sys
sys.setrecursionlimit(300000)

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


N, K = MI()

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

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

n = n % k
if n < k - n:
    print(n)
else:
    print(k - n)

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

a = N % K 

print(min(a, K - a))

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

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

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

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

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

b = n % k
c = abs(b - k)
if b < c:
    print(b)
else:
    print(c)

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

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

Solution (Python):
n, m = (int(x) for x in input().split())

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

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

Solution (Python):
a,b=map(int,input().split())
ans=[a,a%b]
for i in range(10000):
    ans.append(abs(ans[i]-b))
print(min(ans))

Solution (Python):
N,K = map(int, input().split(' '))
x = N % K
x1 = K - x
if x > x1:
  print(x1)
else:
  print(x)

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

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

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

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

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

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

Solution (Python):


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

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

Solution (Python):
def main():
    n, k = map(int, input().split())
    remainder = n % k
    print(min(remainder, k - remainder))


if __name__ == '__main__':
    main()

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

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

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

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

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

Solution (Python):
import math
N,K= list(map(int,input().split()))
a = int(N/K)
a1 = a*K
a2 = (a+1)*K
if N % K ==0:
    Num = 0
else:
    if abs(N%K) < abs((N%K)-K) :
        Num = abs(N%K)
    else:
        Num =  abs((N%K)-K)
    
print(Num)

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

ans = N%K

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

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
import numpy as np

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

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













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

print(solve)

Solution (Python):
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)

Solution (Python):
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()

Solution (Python):
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)

Solution (Python):
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))

Solution (Python):
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))

Solution (Python):
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))

Solution (Python):
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))

Solution (Python):
"""
青木君は任意の整数 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)

Solution (Python):
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))

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

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

Solution (Python):

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)

Solution (Python):
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))

Solution (Python):
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))

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

Solution (Python):
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')

Solution (Python):
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)

Solution (Python):
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)

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

Solution (Python):
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))

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


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)

Solution (Python):
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)))

Solution (Python):
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))

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

a=n%k

b=abs(a-k)

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

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

rest = N % K

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

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

Solution (Python):
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))

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

Solution (Python):



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


def main():
    N, K = map(int, input().split())
    ans = solve(N, K)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
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:
        
        min = x
        x = absdiff(x, K)
        if min == 1:
            break

    print(min)
    return


culc(N, K)

Solution (Python):
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))

Solution (Python):

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

Solution (Python):
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))

Solution (Python):
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

Solution (Python):
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))

Solution (Python):
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))

Solution (Python):

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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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))

Solution (Python):
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)

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

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

Solution (Python):
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)

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

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

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

n = a % b

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

Solution (Python):
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)

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

Solution (Python):
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)
   
    if s.count(ans):
        ans=abs(ans-k)
        break

print(ans)

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











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

Solution (Python):
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))

Solution (Python):
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))

Solution (Python):
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))

Solution (Python):
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)

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

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

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

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

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

Solution (Python):
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)

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

Solution (Python):
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)

Solution (Python):
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()

Solution (Python):
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)

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

Solution (Python):


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

Solution (Python):

import copy

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

x = n

if x >= k:
    x %= k

print(min(x, k - x))

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

Solution (Python):
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)

Solution (Python):
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)

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

Solution (Python):
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)

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

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

tmp=N%K

ans=abs(tmp-K)
print(min(tmp,ans))

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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 )

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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))

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

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

Solution (Python):
def calc(n, k):
    if n == 0:
        return 0
    if n > k:
        a = n%k
        return min(abs(a-k), a)
    
    if n*2 < k:
        return n
    else:
        
        return min(abs(n-k), k-abs(n-k))

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

Solution (Python):
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)

Solution (Python):
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))))

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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))

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

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

Solution (Python):
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))

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

N = N % K

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

Solution (Python):

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

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

Solution (Python):
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)

Solution (Python):
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

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

Solution (Python):
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)

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

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

Solution (Python):
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)))

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

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

Solution (Python):
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)

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

Solution (Python):
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)

Solution (Python):
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))

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

Solution (Python):



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


print(main())

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

n %= k

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

print(n)

Solution (Python):
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))

Solution (Python):
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))

Solution (Python):
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)