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



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

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

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):
a = [int(a) for a in input().split(' ')]
N = a[0]
K = a[1]
    
def abs_met(N):
    N = N % K
    if N > abs(N - K):
        N = abs(N - K)
        abs_met(N)
    else:
        print(N)
        
if K == 1:
    print(0)
else:
    abs_met(N)

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

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

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

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

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())
n = int((N/K + 0.5)//1)
i = abs(n*K-N)
while i >= K/2:
    i = abs(K-i)

print(i)

Solution (Python):
import math

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


if(K == 1):
    while(int(len(str(N))) > 2):
        N = int(N - K * (N / 2));
else:
    border = math.floor(N / K);
    N = N - K * border;
        
flag = True;
while flag == True:
    N = abs(N - K);
    if(N <= min):
        min = N;
    else:
        flag = False;
    
print(min);

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

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

Solution (Python):
N,K=(int(x) for x in input().split())
N=N%K
a = abs(K-N)
if a<N:
  print(a)
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())
j = 0
i = 3
A = 1000000000000000001
B = 1000000000000000002
C = 1000000000000000003
if N<K:
    M = N-K
    M = abs(M)
    
    if M<N:
        print(M)
    else:
        print(N)
elif N==K:
    print(0)
elif N%K==0:
    print(0)
else:
    N = N%K
    while j==0:
        
        
        N = N-K
        N = abs(N)
        if i%3==0:
            A = N
        elif i%3==1:
            B = N
        elif i%3==2:
            C = N
        
        if A==C:
            if A<=B:
                print(A)
            else:
                print(B)
            j+=1
        elif B==A:
            if A<=C:
                print(B)
            else:
                print(C)
            j+=1
        elif B==C:
            if B<=A:
                print(C)
            else:
                print(A)
            j+=1
        i+=1

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


if __name__ == '__main__':
    q3()

Solution (Python):
import sys

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

min = sys.maxsize

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

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

while True:
    diff = abs(n- k)

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

        if diff < min:
            min = diff



print(min)

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

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

Solution (Python):


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

if K == 1:
    N = 0

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

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

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

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

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

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

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

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

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

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

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

if N > K:
    s = N // K
    N -= (K * s)
min = N
if K == 1:
    print(0)
else:
    while True:
        if K == 1:
            print(0)
            break
        N = abs(N - K)
        if  (N in num)  == True:
            print(min)
            break
        else:
            num.append(N)
            if N < min:
                min = N

Solution (Python):


import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,k = LI()
ans = min(abs(n % k - k), n % k)
print(ans)

Solution (Python):
import sys


def main(argv):
    n, k = [int(i) for i in sys.stdin.readline().rstrip("\n").split(" ")]
    r = n % k
    print(min(r, abs(r - k)))
    return 0
    

if __name__ == "__main__":
    sys.exit(main(sys.argv))

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())
a=-(-N//K)
print(min(N,abs(N-(a*K))))

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

if num_list[0] < num_list[1]:
    if (num_list[1] - num_list[0]) < num_list[0]:
        print(num_list[1] - num_list[0])
    else:
        print(num_list[0])
elif num_list[0] == num_list[1]:
    print(0)
elif num_list[1] % num_list[0] == 0:
    print(0)
else:
    kake_num = num_list[0] // num_list[1]
    check_num = num_list[0] - (num_list[1]*kake_num)
    min_list.append(check_num)
    min_list.append((num_list[1] - check_num))
    print(min(min_list))

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

N = int(NK[0])
K = int(NK[1])

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

elif N > K:
    A = N % K
    if A > K-A:
        print(K-A)

    else:
        print(A)

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

Solution (Python):
a=list(map(int, input().split()))
def d(a,b):
    _1=a%b
    _2=b-(a%b)
    if _1<_2:
        print(_1)
    else:
        print(_2)
d(a[0],a[1])

Solution (Python):


n, k = map(int, input().split())
if n == k:
    min = 0
else:
    x=n%k
    y=k-x
    min = min(x,y)

print(min)

Solution (Python):
import sys

N,K=map(int,input().split())
ZET=N
b=(10**18)
c=N
b1=N
for i in range(int(b)):
  if K==1:
    b1=0
    break
  if N>10**17 and K<10**9:
    b1=N%K
    c=abs(b1-K)
    if b1>c:
      b1=c
    break
  c=abs(b1-K)
  if int(b)<=c:
    break
  b1=c
  b=c
print(b1)

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




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

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

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

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

        else:
            N = abs(N - K * (N//K))

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

n = int(nk[0])
k = int(nk[1])

kf = n % k
ki = abs(kf-k)

print(min(kf, ki))

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

if N % K ==0:
  print(0)
  exit()
  
if N < K:
  print(min(N,K-N))
  exit()

if N > K:
  print(min(N%K,K-(N%K)))

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

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

Solution (Python):
import sys
n,k=map(int,input().split())
if n%k==0:
    print(0)
    sys.exit()
if n%k<abs(k-n%k):
    print(n%k)
else:
    print(abs(k-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())
r=n%k
if -(r-k)>=r:
    print(r)
else:
    print(-r+k)

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

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

Solution (Python):
import math
first_input = list(map(lambda x:int(x), input().split(" ")))


N = first_input[0]
K = first_input[1]



if N>K:
    first_min_N = N - K*math.floor(N // K)
else:
    first_min_N = N

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

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

Solution (Python):




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

    if 2 * N < K:
        return N

    R = N % K
    
    if R < abs(R - K):
        return R
    else:
        return abs(R - K)


if __name__ == '__main__':
    print(main())

Solution (Python):
n, k = map(int, input().split())
q, mod = divmod(n, k)
if mod > (k//2):
        print(abs(k - mod))
else:
        print(mod)

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

ans = N

if N < K == 0:
    ans = N%K
else:
    ans = ans % K
    while True:
        if ans > abs(ans - K):
            ans = abs(ans - K)
        else:
            break

print(ans)

Solution (Python):
N, K = map(int, input().split(' '))
if 2 * N < K:
    print(N)
else:
    a = N // K
    mm = min(abs(N - a * K), abs(N - (a + 1) * K))
    print(mm)

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

Solution (Python):
[n, k] = map(int, input().split())
 
n -= (n//k) * k
if n > k//2:
  n = k -n
print(n)

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

x = N - (N // K) * K
ans1 = abs(x - K)
ans2 = abs(ans1 - K)
print(min(ans1, ans2))

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

q = n // k 
mod = n % k 

if q == 0:

    while True:
        nc = n
        n = n - k
        if n <= 0:
            if abs(n) < nc:
                print(abs(n))
                break
            else:
                print(nc)
                break
else:
    n1 = abs(n - (q * k))
    n2 = abs(n - ((q + 1) * k))
    if n1 < n2:
        print(n1)
    else:
        print(n2)

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

Solution (Python):
N,K=map(int,input().split())
if N==0 or K==1:
    print(0)
else:
    a=N//K
    N=N-a*K
    for i in range(10**18):
        if N>abs(N-K):
            N=abs(N-K)
        else:
            print(N)  
            break

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

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

a=N//K


if a>=1:
    s=N-K*a
    if s>=abs(s-K):
        print(int(abs(s-K)))
    else:
        print(int(s))

elif a==0 and N>=(abs(N-K)):
    print(int(abs(N-K)))

else:
    print(int(N))

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

if a == b:
    print("0")
    exit()

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

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

Solution (Python):
import math

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

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

print(N)

Solution (Python):

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

if (N%K) <=(K/2):
	x=N%K
else:
	x=K-N%K
	
print(x)

Solution (Python):
nums = list(map(int, input().split()))
n = nums[0]
k = nums[1]

def absolute(n,k):
    return abs(n-k)

x = absolute(n, k)
if n % k == 0:
    print(0)
elif n < x:
    print(n)
else:
    while absolute(x, k) < x:
        x = x % k
        x = absolute(x, k)

    print(x)

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

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

def diff(n,k):
  diff = 0
  if n > k:
    diff = n-k*(n//k)
  elif n < k:
    diff = k-n
  else:
    diff = 0
  return diff

ans = [n]

for i in range(100):
  ans.append(diff(ans[i],k))

print(min(ans))

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

Solution (Python):
NK = list(map(int, input().split()))
N = NK[0]
K = NK[1]
posiVal = N - N//K * K
negaVal = N - (N//K + 1) * K
if posiVal < -negaVal:
    print(posiVal)
else:
    print(-negaVal)

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

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

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

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

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

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

var = N % K
if var == 0:
        print(0)
elif var == K-1 or var == 1:
        print(1)
else:
        while N >= math.ceil(K / 2):
                N = abs(N-K*(N//K+1))
        print(N)

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

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

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

        else:
            
            N = abs(N - K * (N//K))

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

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

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

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


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

        if k%n<n:
            n=k%n

    print(n)

Solution (Python):
def solve(n, k):
    occurence = set()

    diff = n % k
    n = diff
    
    if diff == 0 or diff == 1:
        print(diff)
        return
    else:
        while n not in occurence:
            if k == 1:
                print(0)
                return

            occurence.add(n)

            n = abs(n - k)

            if n == 0:
                print(n)
                return

    min_value = min(occurence)
    print(min_value)


if __name__ == '__main__':
    nk = input().split()

    n = int(nk[0])
    k = int(nk[1])

    solve(n, k)

Solution (Python):

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

def gcd2(a, b):
  while True:
    if a > b:
      if a % b == 0:
        return 0
      else:
        
        a = a % b
    elif a == b:
      return 0
    else:
      return min(a, abs(a-b))

ans = gcd2(N, K)
if ans == K:
  print(0)
else:
  print(ans)

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

Solution (Python):

N,K = (int(x) for x in input().split(" "))


zeroika = 0
if K <N:
    zeroika = N//K
else:
    zeroika = 1
N1 =abs(N-K*zeroika) 
while N1<N:
    N =N1
    N1 =abs(N-K)
    
print(N)

Solution (Python):
n, k = map(int, input().split())
tmp = n
n = n%k
while True:
    tmp = n
    n = abs(n-k)
    if tmp<n:
        n = tmp
        break
print(n)

Solution (Python):
def solve():
    N, M = map(int, input().split())
    
    if N % M == 0:
        return 0
    
    a = N % M

    return min(a, abs(a-M))

print(solve())

Solution (Python):


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

if n==k:
    print(0)
    

if n < k:
    for _ in range(2):
        abs_val = abs(n-k)
        
        if abs_val<=n:
            n=abs_val
            
    print(n)


if n > k:
    z = n//k
    n = n - k*z
    
    for _ in range(4):
        abs_val = abs(n-k)
        
        if abs_val<=n:
            n=abs_val
            
    print(n)

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

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

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


if a <= b:
  while min > abs(a-b):
    min = abs(a-b)
    a   =abs(a-b)
else :
  a = a%b
  while min > abs(a-b):
    min = abs(a-b)
    a   = abs(a-b)
  
print(min)

Solution (Python):
n = list(map(int,input().split()))
a = n[0]
b = n[1]
c = a%b
d = b - c
if d<c:
    print(d)
else:
    print(c)

Solution (Python):


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

N = input_d3[0]
K = input_d3[1]

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

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

l=[]
c=abs(n-m)
if n%m==0:
    c=0
    print(c)
else:
    c=n%m
    d=abs(m-c)
    l.append(c)
    l.append(d)
    l=sorted(l)
    print(l[0])

Solution (Python):
n,k=map(int,input().split())
count=10**18 
if abs(n)>abs(k):
  a=n//k
  n=abs(n)-a*k
  
  
for i in range(10*18):
  n=abs(n-k)
  if count>n:
    count=n
  if count==0:
    print(0)
    exit()
    
print(count)

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


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

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


if __name__ == '__main__':
  main()

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

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

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

if n < k:
  if n > abs(n-k):
    print(abs(n-k))
    exit()
  else:
    print(n)
    exit()

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

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

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

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

        else:
            print(abs(N % K))

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

syou = n//k

zet = abs(n-k*syou)
if zet == 0:
  print(zet)
else:
  zet1 = abs(n-k*(syou+1))
  if zet > zet1:
    print(zet1)
  else: 
    print(zet)

Solution (Python):

N, K = map(int,input().split())
N  %= K
min = N
for i in range(10**6):
    N = abs(N-K)
    if min > N:
        min = N
    if N == 0:
        min = 0
        break
print(min)

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

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

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):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))



N,K = LI()

count = N//K

N = N-count*K
print(min(N,abs(N-K)))

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

Solution (Python):
def func(x,k):
    x = x%k
    if abs(x-k)>x:
        return x
    return func(abs(x-k),k)

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

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


x = [n]

if n > k:
    n = n%k


while True:
    n = abs(n - k)
    if n >= x[-1]:
        break
    x.append(n)




print(min(x))

Solution (Python):
n, k = map(int, input().split())
L = []
if n > k:
  n = n - k*(n//k)
 
if n*2 <= k:
  print(n)
else:
  print(k-n)

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

elif n%k==0:
    print(0)
else:

    a = n // k
    b= (a + 1) * k - n
    c = n - a * k
    t=n-k
    p=k//t
    r=k-p*t
    print(min(b,c,r))

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

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

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


a=n%m

if a>abs(a-m):
    a=abs(a-m)


print(a)

Solution (Python):
import math

data = input().split()

n = int(data[0])
k = int(data[1])

div = n//k
n2 = n - div*k
if div==0:
	n2 += k

ans = []
for i in range(2):
	ans.append(abs(n2-k))
	n2 = abs(n2-k)

print(min(ans))

Solution (Python):
"""
N K
操作: x を x と K の差の絶対値で置き換える
整数 N の初期値が与えられます。この整数に上記の操作を 0 回以上
好きな回数行った時にとりうる N の最小値を求めてください。
"""
import math
n,k=map(int,input().split())
x0=n
xi=n

xi1=abs(xi-k)

leap=math.floor(xi1/k)-3
xi1,xi=abs(xi1-k*leap),xi1
i=0
while ((i==0)|(xi1 < xi)) :
  
  """
  if(math.floor(i/100000)==0):
    print(xi1,xi)
  """
  xi1,xi=abs(xi1-k),xi1
  i=i+1

print(xi)
"""
"""  

"""


if b[m-1]>= (sum(b) /(4*m)) :
  print("Yes")
else:
  print("No")

x,y,z=map(int,input().split())
a,b,c=x,y,z
a,b=b,a
a,c=c,a
print(a,b,c)
"""

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

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

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

else:
    
    x = (n+k-1)//k
    y = (n-k-1)//k
    if y < 0:
        y += 1
    if x < 0:
        x+= 1
    a =abs(x*k-n)
    b = abs(y*k-n)
    if a < b:
        print(a)
        
    else:
        print(b)

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

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

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

if N ==0 or K==1 or N == K:
  print(0)
else:
  while N>abs(N-K):
    N = abs(N%K-K)
  print(N)

Solution (Python):
N,K=(int(i) for i in input().split())
N=N-N//K*K
M=N-K
print(min(N,abs(M)))

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

r = []
n = n % k
if n == 0:
    print(0)
    exit()
while True:
    n = abs(n - k)
    r.append(n)
    if r.count(n) >= 2:
        break

print(min(r))

Solution (Python):
N,K = map(int, input().split())
N = N%K
valuee = abs(K-N)
print(valuee if valuee < N else 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())

a=N % K

if a > K/2:
  a = K - a
  
print(a)

Solution (Python):
import math
n, k = input().strip().split(' ')
n, k = [int(n), int(k)]

def fx(m):
    a = m - k 
    if(m - k) < 0:
        a  = - (m - k)
    if a >= m:
        return m
    return fx(a)

l = n
if n>= k:
    i = n//k
    l = n - k * i

ans = fx(l)
print(ans)

Solution (Python):
N,K = list(map(int, input().split()))
 
ans = []
a = N
 
ans2 = N % K
ans1 =  (N % K ) - K
if abs(ans2) > abs(ans1):
    print(abs(ans1))
else:
    print(abs(ans2))

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

nums = []

def calc_num(n, k):
    if n <= k:
        nums.append(min(n, k-n))
        return
    
    n = n % k
    if n in nums:
        return
    elif n == 0:
        nums.append(0)
        return
    else:
        nums.append(n)
        calc_num(n, k)
        
calc_num(N, K)
print(min(nums))

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

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

print(min(ans, ans2))

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

if N % K == 0:
    print(0)
    sys.exit()
else:
    X = N // K
    N = N - (K * (X+1))

second = N + K

N = abs(N)

list_a.append(N)
list_a.append(second)
list_a.append(first)

list_a = sorted(list_a)

print(list_a[0])

Solution (Python):
from fractions import gcd 
n,k = map(int,input().split())
a=k*(n//k)
if n >= k and n%k == 0:
    print(0)
elif n < k:
    print(min(abs(n-k),n))
else:
    print(min(abs(abs(n-a)-k),abs(n-a)))

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

r = []
n = n % k
if n == 0:
    print(0)
    exit()
while True:
    n = abs(n - k)
    r.append(n)
    if r.count(n) >= 2:
        break

print(min(r))

Solution (Python):
N,K = map(int, input().split())
if N > K and N//K>2:
	a = N//K
	N= N-K*(a-2)

while N>0:
	T = N
	N = abs(N-K)
	if N >T:
		break

print(N if N==0 else min(N,T) )

Solution (Python):
N, K = map(int, input().split())
Nm = N % K
ans = Nm
A = [Nm]
while 1:
    if ans == 0:
        break
    elif abs(Nm-K) in A:
        break
    else:
        if abs(Nm-K) < ans:
            ans = abs(Nm-K)
        A.append(abs(Nm-K))
        Nm = abs(Nm-K)
    
print(ans)

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

    if N == 0 or N == K :
        print("0")
    elif N > K :
        ans = N % K
        
        N = ans
        for i in range(0 , N + 1) :
            value_diff_abs = N
            N = abs(N - K)
            
            if value_diff_abs < N :
                print(str(value_diff_abs))
                break
            elif value_diff_abs == N :
                print(str(value_diff_abs))
                break
    else :
        for i in range(0 , N + 1) :
            value_diff_abs = N
            N = abs(N - K)
            if value_diff_abs < N :
                print(str(value_diff_abs))
                break
            elif value_diff_abs == N :
                print(str(value_diff_abs))
                break


if __name__ == '__main__':
    main()

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

a = n % k
if a == 0:
    print(0)
    exit()
k_1 = k / 2
if a > k_1:
    ans = k - a
elif a < k_1:
    ans = a
print(ans)

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

if K == 0:
  print(N)
else:
  if N >= K:
    kotae1 = abs(N - ((N // K) * K))
    kotae2 = abs(N -((N // K) + 1) * K)
    if kotae1 > kotae2:
      print(kotae2)
    else:
      print(kotae1)

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

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

n = n % k
if k - n < n:
    n = k - n

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):
N, K = map(int, input().split())
d = N // K
print(min(abs(N - (d * K)), abs(N - ((d + 1) * K))))

Solution (Python):
import math

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

ansa = n % k
ansb = abs(ansa - k)

ans = min(ansa, ansb)

print(ans)

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

x = abs(N -(N//K)*K)
y = abs(N - (N//K + 1)*K)

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

Solution (Python):
import sys

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

an = int(n%k)
if  an > (abs(an-k)):
    print(abs(an-k))
    sys.exit()

print(an)

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):
import sys


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

n = n - (k * div)
ans = min(n, abs(n-k))
print(ans)