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):
n,k = map(int,input().split())

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

print(n)

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

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

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

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):
nn, kk = input().split(" ")
n = int(nn)
k = int(kk)
 
remain = n % k
if( kk=="1" ):
  print(0)
elif( remain == 0 ):
  print(remain)
else:
  print( min(abs( remain - k), abs(abs(remain-k)-k) ))

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

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

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

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

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

amari = N % K

if amari < K / 2:
    print(amari)
else:
    print(K - amari)

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

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

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

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

a=n%k
for i in range(10):
    s=a
    a=abs(a-k)
    if s<a:
        print(s)
        break

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

m = n % k

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

Solution (Python):
li = list(map(int, input().split()))
n = li[0]
k = li[1]
a = n % k
b = abs(a - k)
if a >= b:
    print(b)
else:
    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())

ans = 0
if N ==K:
    ans = 0
else:
    if N > K and N%K==0:
        ans = 0
    else:
        if N > K:
            N = N%K
        ans = min(N, K-N)
print(ans)

Solution (Python):

import collections


class Solution:
    def solve(self, n, k):
        rem = n % k
        return min(rem, abs(rem-k))


sol = Solution()

[n, k] = list(map(int, input().strip().split()))
print(sol.solve(n, k))

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

ans = 0
if N!=K and K>1:
    if N < K:
        ans = min(N,abs(N-K))
    else:
        ans = min(N % K,abs((N % K)-K))
if N==K:
    ans = 0
print(ans)

Solution (Python):


N,K= map(int, input().split())
''' a1=abs(N)
a2=abs(N-K)
da=a1-a2
N=a2 '''
''' while da>=0:
    a1=abs(N)
    a2=abs(N-K)
    da=a1-a2
    N=a2 '''
ansN=N//K
ans=min(N-ansN*K,-N+(ansN+1)*K)

print(ans)

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

x = N

if x > K:
    x %= K

if K - x < x:
    x = K - x

print(x)

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

N = N % K
a = 0
while a == 0:
    b = N
    if N >= K:
        N = N - K
    else:
        N = K - N
    if N > b:
        a = 1

print(b)

Solution (Python):

import math
import fractions

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

if K==0:
    print("N")
elif N==0 or N%K==0:
    print("0")
else:
    N=N%K
    print(min(N, K-N))

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

if k == 1:
    print(0)
elif n < k:
    print(min(abs(n-k), abs(n)))
else:
    num = n // k
    n = n - num * k
    print(min(n, abs(n-k)))

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

Solution (Python):
n, k = list(map(int, input().split(" ")))
x = n % k
y = abs(x - k)

if x > y:
    print(y)
else:
    print(x)

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

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

start = 0
end = N

ans = float('inf')
while start <= end:
	mid = start + (end - start) //2
	ans = min(abs(mid*K-N),ans)
	if mid*K == N:
		break
	elif mid*K>N:
		end = mid-1
	else:
		start = mid+1
		
print(ans)

Solution (Python):
N, K = map(int, input().split())
q, mod = divmod(N, K)
if mod == 0:
    print(0)
else:
    if mod*2 > K:
        print(abs(mod-K))
    else:
        print(mod)

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

a = n//k
c = abs(n-a*k)
d = abs(n-(a+1)*k)

if c >= d:
    s = d
else:
    s = c
    
print(s)

Solution (Python):
NK = input().split()
N = int(NK[0])
K = int(NK[1])

for i in range(2):
  if N >= K:
    N = N % K
  
  else:
    if K < N*2:
      N = K % N
    
print(N)

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

if n > m:
    n %= m

print(min(n, abs(n - m)))

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

key = n % k


while True:
  d = key - k
  if d < 0:
    d *= -1
  if d >= key:
    break
  else:
    key = d
print(key)

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(min(n%k, abs(k-n%k)))
else:
    print(min(abs(n-k), n))

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

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

else:
    sho = n // k
    n -= k * sho
        
    for i in range(1000000000000000000000):
        n = abs(n - k)
        if n <= k // 2:
            break

    print(n)

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

num1 = N%K
num2 = abs(num1-K)

if num1>=num2:
    print(num2)
else:
    print(num1)

Solution (Python):
N,K=map(int, input().split())
x = abs(N-K)
x = N % K
mini = x
while(True):
    if abs(x-K) < mini:
        mini = abs(x-K)
    else:
        break
print(mini)

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

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

Solution (Python):
from fractions import gcd

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

if N > K:
    N %= K

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

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

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

Solution (Python):
N,K=[int(x) for x in input().split()]

R=N%K
S=abs(R-K)
print(min(R,S))

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

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

Solution (Python):
inputted = input().split()
x = int(inputted[0])
k = int(inputted[1])
buf = 0

p = x % k
q = k - p

if p > q:
    print(q)
else:
    print(p)

Solution (Python):
N,M = map(int, input().split())
if N>=N%M:
    if N%M>=M-N%M:
        ans=M-N%M
    else:
        ans=N%M
else:
    ans=N
print(ans)

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:
    while K<2*N:
        N=abs(N-K)
    print(N)
else:
    R=N%K
    print(min(R,K-R))

Solution (Python):
[n, k] = [int(i) for i in input().split()]
min = n%k
tmp = -1 * (min - k)
if min <= tmp:
    print(min)
else:
    print(tmp)

Solution (Python):

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


ans=0
import fractions
newval=n%k
if n>=k:
    if n%k==0:
        ans=0
    else:
        
        ans=n%k
        templ=[]
        ans=min(ans,abs(ans-k))
else:
    
    ans=n%k
    ans=min(ans,abs(ans-k))
print(ans)

Solution (Python):
N, K = map(int, input().split())
n_1 = N - (K * (N // K))
n_2 = abs(n_1 - K)
print(min(n_1, n_2))

Solution (Python):
def main(n,k):
    val, mod = divmod(n,k)
    x = n - (k*val)
    y = abs(x - k)
    return min(x,y)

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

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

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



if __name__ == "__main__":
    c()

Solution (Python):

n, k = map(int, input().split())
if k==1:
    print(0)
    exit()
n = n%k
if n <=k/2:
    print(n)
else:
    print(-1*(n-k))

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

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

d = n // k
ans = min(abs(n - d * k), abs(n - (d + 1)* k))
print(ans)

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

if N == K:
    print(0)
elif N > K:
    b = N // K
    print(min(abs(N - b * K),abs(N - (b+1) * K)))
else:
    print(min(abs(N - K),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 = tuple(map(int, input().split()))
if n > k:
    n = n%k
while(True):
    n = abs(n-k)
    if(n < abs(n-k)):
        break
print(n)

Solution (Python):
import sys

N,K = map(int, input().split())
tmp = N
while(1):
    if(N%K<N):
        N = N%K
    elif(N%K == N):
        if(abs(N-K) <= N):
            N = abs(N-K)
        else:
            print(N)
            sys.exit()

Solution (Python):
import math

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

amari = n % k
n = amari

ab = abs(n - k)

while n > ab:
  n = ab
  ab = abs(n - k)

print(n)

Solution (Python):
n,k =map(int,input(). split())
a = n//k
b = abs(n-k*a)
c = abs(n-k*(a+1))
d = min(b,c)
print(d)

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

Solution (Python):

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

N_min=N%K
if N_min>K/2:
    N_min=K-N_min
print(N_min)

Solution (Python):
nk = list(map(int, input().split()))

amari=nk[0]%nk[1]

ab=abs(nk[1]-amari)

if ab<amari:
    print(ab)
else:
    print(amari)

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


v1 = N % K
v2 = v1 - K

ans = min([v1, -v2])
print(ans)

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

ans = N % K
ans2 = (ans - K) * -1

if ans2 < ans:
    print(ans2)
else:   
    print(ans)

Solution (Python):

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

seen = set()

while N not in seen:
    seen.add(N)

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

print(min(seen))

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

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

Solution (Python):
n,k = map(int,input().split())
n = n%k
while n > abs(n-k):
  n = abs(n-k)
print(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 = map(int, input().split())

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

print(min(m, n))

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

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

Solution (Python):
n,k=map(int,input().split())
if n>=k:
    if abs(n-(n//k)*k)<=abs(n-(n//k+1)*k):
        print(abs(n-(n//k)*k))
    else:
        print(abs(n-(n//k+1)*k))
else:
    if k-n<=n:
        print(k-n)
    else:
        print(n)

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


if N == K or K == 1:
    ans = 0
else:
    a = N % K
    ans = abs(a - K)
    if a <= ans:
        ans = a

print(str(ans))

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

r=0

if(N<K):
  if(abs(N-K)<N):
    r=abs(N-K)
  else:
    r=N

elif(K==1):
   r=0

elif(K<N):
  a=N//K
  N=N-K*a
  while(0<N):
    r=abs(N-K)
    N=N-K

print(r)

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

if n < abs(n-k):
    pass

elif n > k:
    a = n // k
    n = n - a*k
    while abs(n-k) <= n:
        n = abs(n-k)

else:
    while abs(n-k) <= n:
        n = abs(n-k)


print(n)

Solution (Python):
n, k = map(int, input().split())
x = n % k
while x ** 2 >= (x - k) ** 2:
    x -= k
print(x if x >= 0 else x * (-1))

Solution (Python):
n, k = [int(i) for i in input().split()]
if n % k  == 0:
    print(0)
elif n < k:
    print(min(n, k-n))
else:
    n = n - int(n//k) * k
    n = min(abs(n-k), n)
    n = min(abs(n-k), n)
    print(n)

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

min_num = N % K if N % K < abs(N % K - K) else abs(N % K - K)

print(str(min_num))

Solution (Python):
N,K = map(int, input().split())
b = N // K
min = N - (K*b)
while True:
  if abs(min - K) > min:
    print(min)
    break
  min = abs(min-K)

Solution (Python):
import sys
input = sys.stdin.readline

def solve(init: int, K: int, acc: int):
    while abs(init - K) < init:
        init = abs(init - K)
    return init

    
    
    
    
    



def main():
    N, K = map(int, input().rstrip().split())
    N -= K * (N  // K)
    res = solve(N, K, N)
    print(res)


if __name__ == "__main__":
    main()

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

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

a = n
if n<=k:
    a = min([n,k-n])
else:
    a = min([n%k,k-n%k])
print(a)

Solution (Python):

(n, k) = [int(s) for s in input().split()]

r = n % k
b = abs(r - k)

ans = 0

if r == 0:
    ans = 0
elif r >= b:
    ans = b
else:
    ans = r

print(ans)

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

Solution (Python):
def main():
    n, m = map(int, input().split())
    n = abs(n - (n // m) * m)
    n = min(n, abs(n - m))
    print(n)

if __name__ == '__main__':
    main()

Solution (Python):
n,k=map(int, input().split())
d=abs(n-k)
t=n//k
if n%k==0:
    print(0)
elif n>d:
    a=n-t*k
    if a>abs(a-k):
        print(abs(a-k))
    else:
        print(a)
else:
    print(n)

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

Solution (Python):
import math

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

s = N % K

t=abs(K-s)

a=[s,t,N]
ans = min(a)

if t>N:
  ans=N
else:
  ans=t



if s==0 or t==0 or N==0:
  ans=0

print(ans)

Solution (Python):
n,k=map(int,input().split())
if n>k:
    x=n-(n//k)*k
else:
    x=n
while x>k/2:
    x=abs(x-k)
print(x)

Solution (Python):
N, K = map(int, input().split())
import math
if N > K:
    s = N // K
    print(min(abs(N - s * K), abs(N - (s + 1) * K)))
elif N == K:
    print(0)
else:
    print(min(N, abs(N - K)))

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

n = n%k
while n >= 0:
  if abs(n - k) <= n:
    n = abs(n - k)
  else:
    break
    
print(n)

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

min_num = N - (N//K)*K

for i in range(N//K+1):
    if min_num >= abs(min_num - K):
        min_num = min_num - K
    else:
        break

print(abs(min_num))

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

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

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

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

if k >= 2 * n:
    print(n)
    quit()

if k >= n and k < 2*n:
    print(abs(n-k))
    quit()

if n > k:
    x_b = n%k
    x = abs(x_b - k)
    print(min(x_b,x))

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

r1= n%k;

r2=k-r1;

print(min(r1,r2))

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

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

Solution (Python):
def main():
    n,k = map(int,input().split(' '))
    n = n % k
    key = 0
    data = []
    while(key == 0):
        check = abs(n - k)
        for per in data:
            if(check == per):
                key = 1
        if(key == 0):
            data.append(check)
            n = check
        else:
            key = 1
    print(min(data))

if __name__ == '__main__':
    main()

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

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

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

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

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

x = (n // k) - 1
ans = [abs(n - ((x + i) * k)) for i in range(3)]

print(min(ans))

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

if N%K == 0:
    print(0)
elif K%N==0:
    print(N)
else:
    a = N%K
    b = K*(N//K+1)-N
    print(min(a,b))

Solution (Python):



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

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

Solution (Python):
n, k = map(int, input().split())
absn = 0
checker = True
while checker:
    if n > k:
        n = n % k
    else:
        absn = abs(n - k)
        if n > absn:
            n = absn
        else:
            checker = False
print(n)

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

Solution (Python):
def func(a,b):
    if a <= b:
        return a
    else:
        return func(b,abs(b-K))

N,K=map(int, input().split())
N=N%K
print(func(N,abs(N-K)))

Solution (Python):
n,k=map(int,input().split())
p=n//k
if n==0:
  print(0)
elif abs(n-2*k)>=abs(n):
  print(min(abs(n-k),abs(n)))
else:
  print(min(abs(n-p*k),abs(n-(p+1)*k)))

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

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

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

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

Solution (Python):
n,k=map(int,input().split())
ans=0
if n==k:
    print(0)
    exit()
elif n>k:
    if n%k==0:
        print(0)
        exit()
    else:
        ans+=n%k
else:
    ans+=n


while True:
    a=abs(ans-k)
    if a>=ans:
        print(ans)
        exit()
    else:
        ans=a

Solution (Python):
n, k = map(int, open(0).read().split())

amari = n % k

diff = abs(amari - k)

print(min(amari, diff))

Solution (Python):
from math import ceil,floor
N,K = map(int,input().split())
print(min(N%K,K-N%K))

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

div_p = N//K
div_m = N//K+1


print(min(abs(N-K*div_p), abs(N-K*div_m)))

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

while True:
    n %= k
    dif = abs(n-k)
    old_n = n
    if dif >= n:
        break
    n = dif
    if n==old_n:
        break

print(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 = map(int, input().split())

t = []
n = n%k

while n != 0:
    n = abs(n-k)
    if n in t or n == 0:
        print(min(t))
        exit()
    t.append(abs(n))
print(0)

Solution (Python):
def main():
    n,k = map(int,input().split())
    div = n//k
    A = abs(n-k*div)
    B = abs(n-k*(div+1))
    if A < B:
        x = A
    else:
        x = B
    print(x)
if __name__ == '__main__':
    main()

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

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

if K == 1:
    print(0)
    exit()
elif N == 0:
    print(0)
    exit()    
else:
    a = N-K*(N//K)
    b =abs(N-K*(N//K +1))
    print(min(a,b))

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

s = {x}
if 0 in s:
    ans = 0
else:
    while True:
        x = abs(x - K)
        if x == 0:
            ans = 0
            break
        if x in s:
            ans = min(s)
            break
        s.add(x)

print(ans)

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

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

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

md = n % k
ans = min ( md , k - md )
print(ans)

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

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

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

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

n = n % k
print(min([n, k-n]))

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

q, r = n // k, n % k

print(min(abs(n - (q + 1) * k), r))

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

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

Solution (Python):
if __name__ == "__main__":
    N, K = list(map(int, input().split()))
    r = N%K
    if r < abs(r-K):
        print(r)  
    else:
        print(abs(r-K))

Solution (Python):

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

flag = "No"
if k == 1:
  n = 0
else:
  n = n % k

for i in range(1, 1000000000000000000):
  if abs(n - k) > n:
    print(n)
    flag = "Yes"
    break
  else:
    n = abs(n - k)
if flag == "No":
  print(n)

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

t=n%k

if n<k:
    print(min(n,abs(n-k)))
elif k==1:
    print("0")
else:
    print(min(t,abs(t-k)))

Solution (Python):
import sys,math,copy,queue,itertools,bisect
from fractions import gcd
sys.setrecursionlimit(10**7)
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
_LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
INF = 10**18
MOD = 10**9 + 7
DD = ((1,0),(0,1),(-1,0),(0,-1))
N,K = LI()
if K == 1:
    print (0)
    exit (0)
if N == 0:
    print (0)
    exit(0)

if N == 1:
    print (1)
    exit(0)
N = N % K
while True:
    NN = abs(N - K)
    if NN >= N:
        print (N)
        exit (0)
    N = NN

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

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

Solution (Python):
n, k = map(int, input().split())
num = abs(n - k)
if num == 0:
    print(0)
    exit()
tmp = n % k
print(min(tmp, abs(tmp - k)))

Solution (Python):
nk = input().split(" ")
N, K = int(nk[0]), int(nk[1])

n = N % K


r = K - n


if n <= r:
  print(n)
else:
  print(r)

Solution (Python):
def fun(N, K):
    if N%K > K:
        return K
    return fun(K, N%K)

N, K= list(map(int, input().split()))
if N%K == 0:
    print(0)
    exit()
N = N%K
while N>abs(N-K):
    N = abs(N-K)
print(N)

Solution (Python):

import sys

def solve(N: int, K: int):
    if K<N:
        ans=min(N%K, abs(N%K-K))
    elif K<2*N:
        ans=K-N
    else:
        ans=N
    print(ans)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    solve(N, K)

if __name__ == '__main__':
    main()

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

if n < k:
    while(True):
        prev_n = n
        n = abs(n - k)
        if prev_n <= n:
            print(prev_n)
            break
else:
    tmp = n % k
    while(True):
        prev_n = tmp
        tmp = abs(tmp - k)
        if prev_n <= tmp:
            print(prev_n)
            break

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

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

Solution (Python):
N, K = map(int, input().split())
if N == K:
    print(0)
    exit()

tmp1 = 0
if N > K:
    tmp, m = divmod(N, K)
    tmp1 = m
    tmp1 = min(tmp1, abs(m - K))
else:
    tmp = abs(N - K)
    tmp1 = min(N % K, tmp)

print(tmp1)

Solution (Python):
i = list(map(int, input().split()))

j = i[0] % i[1]
k = i[1] -j

print(min(j,k))

Solution (Python):
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') 
def gcd(a,b):return fractions.gcd(a,b) 
def lcm(a,b):return (a*b) // fractions.gcd(a,b) 
def iin(): return int(sys.stdin.readline()) 
def ifn(): return float(sys.stdin.readline()) 
def isn(): return sys.stdin.readline().split() 
def imn(): return map(int, sys.stdin.readline().split()) 
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) 
def fmn(): return map(float, sys.stdin.readline().split()) 
def iln(): return list(map(int, sys.stdin.readline().split())) 
def iln_s(): return sorted(iln()) 
def iln_r(): return sorted(iln(), reverse=True) 
def fln(): return list(map(float, sys.stdin.readline().split())) 
def join(l, s=''): return s.join(l) 
def perm(l, n): return itertools.permutations(l, n) 
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) 
def comb(l, n): return itertools.combinations(l, n) 
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) 
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 
def m_add(a,b): return (a+b) % MOD
def print_list(l): print(*l, sep='\n')
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def No(): print('No')

def sieves_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime

N,K = imn()


if N == 0 or N % K == 0:
    print(0)
else:
    ans = 0
    if N > K:
        ans = N % K
        ans = min(ans, abs(K-ans))
    else:
        ans = min(N, abs(N-K))
    print(ans)

Solution (Python):
x,y=map(int,input().split())

if x>=y:
    x=x%y
a=y-x
b=x

print(min(a,b))

Solution (Python):

N, K = map(int, input().split())
flag = 1
X = N%K
if abs(X-K)<X:
    print(abs(X-K))
else:
    print(X)

Solution (Python):
x,K=map(int,input().split())
temp=x+1
if K>=x:
    print(min(x,K-x))
else:
    while temp>x:
        temp=x
        if x>=K:
            x%=K
        else:
            x=K-x
    print(temp)

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

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

if nmin > abs(nmin-k):
    nmin = abs(nmin-k)

print(nmin)

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

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

r=n%k

R=(r-k)*(-1)
  
print(min(r,R))

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

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

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


n %= k

min_ = n

def r(min_, k):
  if min_ > abs(min_ - k):
    min_ = abs(min_ - k)
    return r(min_, k)
  else:
    return min_

print(r(min_, k))

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

counter = 0
while True:
    if n * 2 < k:
        print(n)
        exit()
    if counter == 0:
        n = abs(n - (k * (n//k)))
        counter += 1
    else:
        n = abs(n - k)

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

if(n%k==0):
    print("0")
elif(n>k):
    ans1 = n % k
    ans2 = abs(ans1-k)

    if(abs(ans1)<abs(ans2)):
        print(ans1)
    else:
        print(ans2)
else:
    ans = abs(n-k)
    if(ans<n):
        print(ans)
    else:
        print(n)

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

if n >= k:
    n = n%k
    
while n > abs(n-k):
    n = abs(n-k)
    
print(n)

Solution (Python):


import sys
import math


debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        
        
        lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]
    
    (n, k) = [int(e) for e in lines[0].split(" ")]

    return (n, k)


def solve(n, k):


    r = n % k

    r2 = abs(r - k)

    result = min([r, r2])
    
     
    return  result


def main():
    
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r, sep='')
    else:
        print("%s" % result, sep='')

if __name__ == '__main__':

    main()

Solution (Python):



import sys


def input():
    return sys.stdin.readline()


def resolve():
    n, k = map(int, input().split())

    min_num = 9999999999999999

    amari = n % k

    ans_list = []
    ans_list.append(amari)

    ans1 = abs(amari - k)
    ans_list.append(ans1)
    ans2 = abs(ans1 - k)
    ans_list.append(ans2)

    answer = min(ans_list)

    print(answer)




if __name__ == "__main__":
    resolve()

Solution (Python):
N,K = map(int,input().split())
s = N // K
n1 = N - K*s
n2 = n1 - K
print(min(abs(n1),abs(n2)))

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

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

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

n = n % k

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

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

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

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

if __name__ == "__main__":
    main()

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

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

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

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

temp=n

temp=min(n,n%k)

for i in range(10^6):
    temp=min(temp,abs(temp-k))

print(temp)

Solution (Python):
N , K = [int(i) for i in input().split()]

X1 = N % K

X2 = abs(X1 -K)

if X1 < X2:
  print(X1)
else:
  print(X2)

Solution (Python):
N,K=map(int, input().split())
m=(N//K)
if m>=1:
    tmp=abs(N-K*m)
    res=min(tmp,abs(tmp-K))
else:
    res=abs(N-K)
print(min(N,res))