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

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

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

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

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

N = N % K

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

print(ans)

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

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

Solution (Python):
a,b=map(int, input().split())
a=a%b
if a==0:
    print(0)
else:
    arr=[]
    arr.append(a)
    while True:
        a=abs(a-b)
        if sum(x == a for x in arr)==1:
            break
        else:
            arr.append(a)

    print(min(arr))

Solution (Python):
def C():
    nk=list(map(int,input().split()))
    
    abs_val=abs(nk[0]-nk[1])
    
    while True:
        if abs_val>nk[1]:
            abs_val=abs_val%nk[1]
        elif abs_val<nk[1]:
            abs_val=min(abs_val,abs(abs_val-nk[1]))
            break
        else :
            
            abs_val=abs_val-nk[1]
            break
    
    print(min(abs_val,nk[0]))
    
    
    
if __name__=='__main__':
    C()

Solution (Python):
def C():
    nk=list(map(int,input().split()))
    
    abs_val=abs(nk[0]-nk[1])
    
    while True:
        if abs_val>nk[1]:
            abs_val=abs_val%nk[1]
        elif abs_val<nk[1]:
            abs_val=min(abs_val,abs(abs_val-nk[1]))
            break
        else :
            
            abs_val=abs_val-nk[1]
            break
    
    print(min(abs_val,nk[0]))
    
    
    
if __name__=='__main__':
    C()

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

Nn = N%M
Nm = abs(Nn - M)

if(Nn >= Nm):
    print(Nm)
else:
    print(Nn)

Solution (Python):
N,K = (int(i) for i in input().split())
T = N//K
N = N - (K * T)

N_1 = abs(N-K)

while N_1 < N :
    N = N_1
    N_1 = abs(N-K)
print(N)

Solution (Python):
n,k=map(int,input().split())
inf=float('inf')
ans=n%k
while ans>=abs(ans-k):
      ans=abs(ans-k)
print(ans)

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

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

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

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

if amari <= amari_h:
        print(amari)
else:
        print(amari_h)

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

if N > K:
    amari  = N % K
    amari_h = abs(amari - K)

    if amari <= amari_h:
        print(amari)
    else:
        print(amari_h)
if N <= K:
    sub = abs(N - K)
    if N <= sub:
        print(N)
    else:
        print(sub)

Solution (Python):
N,K = list(map(int,input().split()))
divisor = N//K
if abs(N-(K*divisor)) < abs(N-(K*(divisor+1))):
	print(abs(N-(K*divisor)))
else:
	print(abs(N-(K*(divisor+1))))

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

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

Solution (Python):
def solve(n, k):
    return min(n % k, abs(n % k - k))

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

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


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

Solution (Python):
inp = input().split()
inp = [int(i) for i in inp]

print(min(inp[1] - (inp[0] % inp[1]), inp[0] % inp[1]))

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

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

if n >= k:
    result = min(n % k, k - (n % k))
elif 2 * n >= k > n:
    n = abs(k - n)
    result = n
else:
    result = n

print(result)

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

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):
import math
N,K = map(int, input().split())

ans1 = abs(N - (K * math.floor(N / K)))
ans2 = abs(N - (K * (math.floor(N / K)+1)))

ans1 = N % K
ans2 = K - N % K 
print(min(ans1,ans2))

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

N = N % K

if N > K/2:
    print(K-N)
else:
    print(N)

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

t = n % k

if t < (k - t):
    print(t)
else:
    print(k - t)

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

i = n % k 
l.append(abs(i-k))
l.append(abs(l[0]-k))
print(min(l))

Solution (Python):













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

val1 = n % k
val2 = abs(val1-k)
ans = min(val1,val2)

print(ans)

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

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

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

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

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

Solution (Python):
a = list(map(int,input().split()))
if a[0]<a[1]:
  print(str(min(a[0],a[1]-a[0])))
else:
  r=a[0]%a[1]
  print(str(min(r,a[1]-r)))

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

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

main()

Solution (Python):
n,k=map(int,input().split())
if (n==k or k==1 or n==0):
    print(0)
    exit()
if (n<k):
    if (n <= k/2):
        print(n)
        exit()
    print(k-n)
    exit()
n=n%k
if (n <= k/2):
    print(n)
    exit()
print(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())

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

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

Solution (Python):
s=input()
ss=s.split()
n=int(ss[0])
k=int(ss[1])
print(min(n%k,k-n%k))

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

print(_min)

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

Solution (Python):
arc = []

def tool(N, K):
    if N == K:
        arc.append(0)
        return True

    elif N < K:
        if K-N in arc:
            return True
        else:
            arc.append(K-N)
            return tool(K-N, K)

    else:
        if N % K in arc:
            return True
        else:
            arc.append(N % K)
            return tool(N % K, K)


def main():
    n, k = map(int, raw_input().split())
    tool(n, k)

    print min(arc)


if __name__ == '__main__':
    main()

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

if N == K:
  print(0)
elif N > K:
  if N%K == 0:
    print(0)
  else:
    print(abs(K-N%K))
else:
  if N < K/2:
  	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())
n=n%k
if n<=k//2:
    print(n)
else:
    print(abs(n-k))

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

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

Solution (Python):
def readInts():
    return list(map(int, input().split()))


n, m = readInts()
ans = min(n % m, abs(n % m - m))
print(ans)

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

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

n=n%k
while n>=k/2:
  n=abs(n-k)
  
print(int(n))

Solution (Python):
import numpy as np
N, K = list(map(int,input().split()))
tmp = np.zeros(2)
tmp[0] = N % K
tmp[1] = -N % K
print(int(tmp.min()))

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

Solution (Python):

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


t = N % K
ans = min(t, K - t)


print(ans)

Solution (Python):

import sys
input = sys.stdin.readline

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

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

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


'''
def DIF(N):
  global K
  return abs(N-K)

if N < DIF(N):
  print(N)
else:
  if N % K == 0:
    print(0)
  else:
    while N >= DIF(N):
      N = DIF(N)
    print(N)
'''

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

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

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

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

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

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

Solution (Python):

import sys


def main():

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

    if n < k:
        print(min(n,abs(n-k)))
        sys.exit(0)
    if n % k == 0:
        print(0)
        sys.exit(0)
    
    print(min(abs(n%k-k),n%k))


main()

Solution (Python):


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

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

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

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

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

Solution (Python):
import math
if __name__ == "__main__":
    N, K = map(int, input().split())
    if N == 0:
        ret = 0
        print(int(ret))
        exit(0)
    ret = N - N // K * K
    while ret > abs(ret-K):
        ret = abs(ret-K)
    print(int(ret))

Solution (Python):
import math
if __name__ == "__main__":
    N, K = map(int, input().split())
    if N == 0:
        ret = 0
        print(int(ret))
        exit(0)
    ret =  N % K
    while ret > abs(ret-K):
        ret = abs(ret-K)
    print(int(ret))

Solution (Python):
N,K=map(int,input().split())
x=N%K
y=((N//K+1)*K - N)
if(x==0):
    print(x)
else:
    if(x>y):
        print(y)
    else:
        print(x)

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

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

a = n % k
b = (a - k) * -1

if a == 0:
    print(a)
else:
    if a > b:
        print(b)
    else:
        print(a)

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

if n > k :
    A = n % k
    B = abs(A - k)
    ans = min(A,B)
else :
    ans = min(n,abs(n-k))
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())
print(min(N%K,-N%K))

Solution (Python):

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


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

Solution (Python):

import sys
N, M = map(int, sys.stdin.readline().split())
tmp = N%M
print(min(tmp, -(tmp-M)))

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

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

Solution (Python):

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


r = N//K
print(min(abs(N - K*(r+1)), abs(N-K*r)))

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

l = n // k

print(min(n - l * k, abs((n - l * 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())
n %= k
print(min(n, (k - n)))

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

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

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

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

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

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

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

if n > k:
     n = n % k

ans = k - n
n = ans

while k - n != ans:
    ans = min(ans, k - n)
    n = k - n

print(ans)

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

Solution (Python):










"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

def readInts():
  return list(map(int,input().split()))

def main():
  """
  例えば x = 100で、k=7みたいなとき
  最初はずっと x >= kの場合になる
  x = 2のときに、 2 - 7より、7 - 2となる
  最終値になり得るものは、最終的に
  2 - 5 - 2 - 5 - 2のどっちかの数になる
  
  2 というのは　N % K
  そうすると、 N % K = a と置くと、
  a, k - aをminすればよい
  
  """
  n,k = readInts()
  a = n % k
  ans = min(a, k - a)
  print(ans)
if __name__ == '__main__':
  main()

Solution (Python):

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

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

Solution (Python):
from sys import setrecursionlimit, exit
setrecursionlimit(1000000000)

from heapq import heapify, heappush, heappop, heappushpop, heapreplace
from bisect import bisect_left, bisect_right

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

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

m = n // k
n = n - m * k
print(min(n, abs(n-k)))

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

cnt = n // k;

n -= cnt * k;
best = n;
for i in range(5):
    best = min(best, abs(n - k));
    n = abs(n - k);

print(best);

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

print(N%K if N%K < abs(N%K-K) else abs(N%K-K))

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

Solution (Python):


from sys import stdin, stderr

def main():
   N, K = map(int, stdin.readline().split())

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

   return 0

if __name__ == '__main__': main()

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

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

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

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

n = n % k

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

print(n)

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

if n % k == 0:
    print(0)
    exit()
if n * 2 < k:
    print(n)
    exit()

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

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

if N >= K:
    q, r = divmod(N, K)
    v = min(r, abs(K - r))
    print(v)
else:
    print(min(N, abs(N - K)))

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
b = n%k - k
print(min(a,abs(b)))

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

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

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

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

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

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

y2 = abs(y1-K)

print(min(y1,y2))

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

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

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

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

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

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

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

temp = n//k
print(min(abs(n-temp*k),abs(n-temp*k-k)))

Solution (Python):
V=0;
o=input().rstrip().split(' ')
n=int(o[0])
k=int(o[1])
if(n%k==0):
    print(0);
elif(k%n==0):
    print(n)
elif(n>k):
    a=(n//k)*k;
    b=n-a;
    print(k-b);
else:
    print(min(n,k-n))

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

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

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

p = N % K

if p <= K-p:
  print(p)
else:
  print(K-p)

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

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

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

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

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

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

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

Solution (Python):



def main():
    import sys
    input = sys.stdin.readline

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

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


if __name__ == '__main__':
    main()

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

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

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

cand1 = abs(int(n-k*(n//k)))
cand2 = abs(int(n-k*(1+n//k)))
cand3 = n

ans = min(cand1, cand2, cand3)
print(ans)

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

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

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

q = n // k
mod = n % k
ans_mod = k-mod

if mod<ans_mod:
  print(mod)
else:
  print(ans_mod)

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

n = n % k

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

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

if N >= K:
    N %= K

if K <= 2*N:
    N = K - N

print(N)

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

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

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

def main():
    N, K = map(int, input().split())
    tmp = N%K
    print(min([N, tmp, K-tmp]))

if __name__ == "__main__":
    main()

Solution (Python):

n,m=input().split()
n,m=[int(n),int(m)]
k=n%m


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

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

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

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

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

x = N % K

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

Solution (Python):
n, k = list(map(int, input().split()))
reminder = n % k
s = abs(reminder - k)

res = min(reminder, s)
print(res)

Solution (Python):
from sys import stdin, stdout
rl = lambda: stdin.readline()
rll = lambda: stdin.readline().split()

def main():
	n, k = (int(x) for x in rll())
	ans = float('inf')
	c1 = n % k
	c2 = abs(c1 - k)
	stdout.write(str(min(c1, c2)));stdout.write("\n")
	stdout.close()

if __name__ == "__main__":
	main()

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

a = n%k
while True:
    tmp = abs(a-k)

    if a > tmp:
        a = tmp
    else:
        break
print(a)

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

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

Solution (Python):

def submit():
    n, k = map(int, input().split())
    
    coef = n // k
    cand1 = abs(coef * k - n)
    cand2 = abs((coef + 1) * k - n)

    print(min(cand1, cand2))


submit()

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

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

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

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

print(min(mod, K - mod))

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

N2=N-K
if N2<0:
  N2=-N2

if N<N2:
  print(N)
else :
  print(N2)

Solution (Python):
n,k=[int(x) for x in input().split()]
mn1=n%k
mn2=k-mn1
print(min(mn1,mn2))

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

t = N % K
t_dush = abs(t-K)

print(min(t, t_dush))

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

def get_min(n, k):
    s = set()
    min_num = n
    while n not in s:
        s.add(n)
        n = k - n
        min_num = min(min_num,n)
    return min_num

print(get_min(n%k,k))

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

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

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


if __name__ == '__main__':
    main()

Solution (Python):
import unittest


class TestC(unittest.TestCase):
    def test_1(self):
        self.assertEqual(think(7, 4), 1)

    def test_2(self):
        self.assertEqual(think(2, 6), 2)

    def test_3(self):
        self.assertEqual(think(1000000000000000000, 1), 0)


def solve():
    n, k = read()
    result = think(n, k)
    write(result)


def read():
    return read_int(2)


def read_int(n):
    return read_type(int, n, sep=' ')


def read_float(n):
    return read_type(float, n, sep=' ')


def read_type(t, n, sep):
    return list(map(lambda x: t(x), read_line().split(sep)))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(n, k):
    if n % k == 0:
        return 0
    m = n % k
    return min(m, abs(k - m))


def write(result):
    print(result)


if __name__ == '__main__':
    
    solve()

Solution (Python):
N, K = list(map(int, input().split()))
N = N % K
while True:
  last = abs(N - K)
  if (last < N):
    N = last
  else:
    print(N)
    exit()

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

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

n = n % k
a = n
while True:
    b = abs(a - k)
    if b <= k / 2:
        ret = b
        break
    else:
        a = b

print("{}".format(ret))

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

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

    remainder = n % k
    
    diff = abs(remainder - k)
    print(min(diff, remainder))


minValue()

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

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

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

a = N%K

print(min(a,K-a))

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

Solution (Python):
import sys
n,k = [int(_) for _ in input().split()]

amari = n % k
if amari == 0 :
  print("0")
  sys.exit()

base = amari
while base > abs(base - k):
  base = abs(base - k)

print(base)

Solution (Python):
n,k=map(int,input().split())
n%=k
if(k==1):
  print(0)
  exit()
s={n}
while True:
  n=abs(n-k)
  if(n in s):
    break
  else:
    s.add(n)
print(min(s))

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, abs((N%K)-K)))

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

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

shou = N // K

print(min(N-K*(N//K), K*(N//K+1)-N))

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

Solution (Python):
import numpy as np

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

list = np.array([], dtype=int)
N = N % K

while(True):
    new_N =np.abs(N-K)
    list = np.append(list, new_N)
    N = new_N
    
    if(len(list) != len(set(list))):
        break;

N_min = np.min(list)
print(N_min)

Solution (Python):
list = input().split(" ")

startNum, standardNum = int(list[0]), int(list[1])

mod = startNum % standardNum
minusMod = abs(mod - standardNum)

result = mod if mod <= minusMod else minusMod

print(result)

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

first = int(N % K)
second = int(K - first)

if second < first:
  print(second)
else:
  print(first)

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

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)

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

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

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

Solution (Python):


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

t=N//K


ans = min(N - K * t, -(N - K * (t + 1)))
print(ans)

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

Solution (Python):
n, k = map(int, input().split())
t = n // k
ans = min(n - k * t, -(n - k * (t + 1)))
print(ans)

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

quotinent = a // b

res = min(a - b * quotinent,  abs(a- b * (quotinent + 1)))
print(res)

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

mini = N % K

if mini < abs(mini - K):
    mini = mini
else:
    mini = abs(mini - K)

print(mini)

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

Solution (Python):



import sys
input = sys.stdin.readline

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

if __name__ == '__main__':
    main()

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

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

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

A = N % K

if (A <= K):
    if A <= abs(A-K):
        print(A)
    else:
        A = A - K
        print(abs(A))
else:
    print(A)

Solution (Python):
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n,k = nm()

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

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

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

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

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

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

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

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

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

a = N%K
ans = min(a, K-a)

print(ans)

Solution (Python):
import sys
def fastio():
    from io import StringIO
    from atexit import register
    global input
    sys.stdin = StringIO(sys.stdin.read())
    input = lambda : sys.stdin.readline().rstrip('\r\n')
    sys.stdout = StringIO()
    register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

INF = 10**20
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))

from math import ceil
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br

n, k = I()
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):
def main():
    N, K = map(int, input().split())
    ans = N % K
    print(min(ans, abs(ans-K)))


if __name__ == "__main__":
    main()

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

x = nk[0]%nk[1]
p = abs(x-nk[1])
print(x if x < p else p)

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

resolve()

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

Solution (Python):
str = input().split()
n = int(str[0])
k = int(str[1])
t = n%k
other = abs(k-t)
print(min(t,other))

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

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

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

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

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

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

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

rot = n//k
x = n

if k == 1 or n == k or n%k == 0:
  x = 0
elif rot >= 1:
  x = n - k*rot
  
t = x%k

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

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

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

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

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

x = N
d = N-K
if d > 0:
    x = K + d%K
ans = min(x, abs(x-K), abs(x-K-K))
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())
m=N%K
ans=min(m,K-m)
print(ans)

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

Solution (Python):


import sys
sys.setrecursionlimit(300000)


def solve(N: int, K: int):
    def gcd(a, b):
        return a if b == 0 else gcd(b, a % b)
    ret = 0
    if N <= K:
        ret = min(K - N, N)
    else:
        tmp = N % K
        ret = min(K - tmp, tmp)
    print(ret)
    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())
a=-(-N//K)
print(min(N,abs(N-K*a)))