CodeEff/ECCO huggingface dataset:
# Original solution
import math
N, K = list(map(int, input().split()))

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

# Optimized solution
import math
N, K = list(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)
# Original solution
N,K = list(map(int,input().split()))
print((min(N%K,abs(N%K-K))))

# Optimized solution
N,K = list(map(int,input().split()))
ans = min(N%K, abs(N%K-K))
print(ans)
# Original solution
import sys


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

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

n_list = [n]

while(True):
    n = abs(n - k)
    if n in n_list:
        break
    else:
        n_list.append(n)

n_list.sort()
print((n_list[0]))

# Optimized solution
n, k = list(map(int, input().split()))

m = n // k
n = n - m * k
print((min(n, abs(n-k))))
# Original solution
n, k = list(map(int, input().split()))

if n < k:
    ans = min(abs(n-k), n)

else:    
    if n%k == 0:
        ans = 0
    else:
        for i in range(-k,k+1):
            if (i%k == n%k and i < 0):
                nega = -i
            elif (i%k == n%k and i >= 0):
                posi = i
        ans = (min(nega, posi))
print(ans)

# Optimized solution
n, k = list(map(int, input().split()))

a = n%k
b = abs(a-k)
print((min(n,a,b)))
# Original solution
n, k = list(map(int, input().split()))

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

# Optimized solution
n, k = list(map(int, input().split()))

a = n%k
b = abs(a-k)
print((min(a,b)))
# Original solution
N, K = list(map(int, input().split()))
ans = 10**10
while True:
    temp = N % K
    if temp < ans:
        ans = temp
    elif temp == ans:
        break

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


# Optimized solution
# C
N, K = list(map(int, input().split()))
ans = 10**10
mod = N % K
ans = min(mod, abs(mod-K))
print(ans)
# Original solution
x = list(map(int, input().split()))

n = x[0]
k = x[1]
y = n

while True:
    temp_y = y

    if temp_y % k == 0:
        y = 0
        break

    y = abs(y - k)
    if temp_y < y:
        y = temp_y
        break
    else:
        continue

print(y)

# Optimized solution
x = list(map(int, input().split()))

n = x[0]
k = x[1]

y = n % k
if y < abs(y - k):
    print(y)
else:
    print((abs(y - k)))
# Original solution
N, K = list(map(int, input().split()))

flag = True

min = N

if K == 1:
    ans = 0
elif N % K == 0:
    ans = 0
elif abs(N - K) > N:
    ans = N
else:
    while(flag):
        if abs(min - K) < min:
            min = abs(min - K)
        else:
            flag = False
            ans = min

print(ans)


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

flag = True

min = N

if K == 1:
    ans = 0
elif N % K == 0:
    ans = 0
elif abs(N - K) > N:
    ans = N
else:
    if (N % K) > (K / 2):
        ans = abs((N % K) - K)
    else:
        ans = N % K

print(ans)
# Original solution
N,K = list(map(int,input().split()))
if K!=1:
    while True:
        N=abs(N-K)
        tmp=N
        if abs(N-K)>tmp:
            print(tmp)
            exit(0)
else:
    print((0))

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

div =N//K
N=N-div*K

if K!=1:
    while True:
        N=abs(N-K)
        tmp=N
        if abs(N-K)>tmp:
            print(tmp)
            exit(0)
else:
    print((0))
# Original solution

def main():
    N, K = list(map(int, input().split(' ')))
    
    # out = min(float('inf'), N)
    prev = N
    
    isFinish = False
    
    if N % K == 0:
        print((0))
        return
    
    while not isFinish:
        next = abs(prev - K)
        
        if next >= prev:
            isFinish = True
        else:
            prev = next
            
    print(prev)

main()

# Optimized solution

def main():
    N, K = list(map(int, input().split(' ')))
    
    # out = min(float('inf'), N)
    prev = N % K
    
    isFinish = False
    
    if prev == 0:
        print((0))
        return
    
    while not isFinish:
        next = abs(prev - K)
        
        if next >= prev:
            isFinish = True
        else:
            prev = next
            
    print(prev)

main()
# Original solution
n,k = list(map(int,input().split()))

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

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

# Optimized solution
n,k = list(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()
# Original solution
n, k = list(map(int, input().split()))
now = n

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

while True:
  n = abs(n - k)
  if n >= now:
    break
  now = n
print(now)

# Optimized solution
n, k = list(map(int, input().split()))

now = abs((n % k) - k)
if now < n%k:
  print(now)
else:
  print((n%k))
# Original solution
N, K = list(map(int, input().split()))

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

elif N<K and N<=K-N:
    print(N)

elif N<K and N>K-N:
    print((K-N))

elif N>K:

    for i in range(18):
        k = K*(10**(17-i))
        while N>=k:
            k=k+K*(10**(17-i))
        k = k-K*(10**(17-i))
    k = k+K
    if k-N<N-(k-K):
        print((k-N))
    else:
        print((N-(k-K)))


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

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

elif N<K and N<=K-N:
    print(N)

elif N<K and N>K-N:
    print((K-N))

elif N>K:
    if K-(N%K)>N%K:
        print((N%K))
    else:
        print((K-(N%K)))
# Original solution
import sys
N, K = list(map(int,input().split()))

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

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

print((min(N%K, K-N%K)))
# Original solution
import sys
n,k = [int(_) for _ in input().split()]
base = n

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

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

print(base)

# Optimized solution
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)
# Original solution
N, K = list(map(int, input().split()))

value = abs(N - K)

if K == 1:
    print((0))
    exit()

while True:
    if abs(value - K) > abs(value):
        break
    value = abs(value - K)

print(value)

# Optimized solution
N, K = list(map(int, input().split()))
a = N // K
value = abs(N - a * K)
if K == 1:
    print((0))
    exit()
while True:
    if abs(value - K) > abs(value):
        break
    value = abs(value - K)
print(value)
# Original solution
import sys
N, K = list(map(int, input().split()))

if K == 1:
    print((0))
    sys.exit()
while True:
    x = abs(N - K)
    if x > N:
        break
    N = min(x, N)
print(N)

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

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

print(N)
# Original solution
n, k = list(map(int, input().split()))
a = abs(n - k)
tmp = a
if k == 1:
    print((0))
elif a > n:
    print(n)
else:
    while n > tmp:        
        n = abs(n - k)
        tmp = abs(n - k)
    print(n)

# Optimized solution
n, k = list(map(int, input().split()))
a = n % k
b = k - a
print((min(a, b)))
# Original solution
n,k = list(map(int,input().split()))

judge = 0

if n%k==0:
    judge = 1
    print((0))
if judge == 0:
    while n>= 0:
        n -= k

    if abs(n+k)<abs(n):
        print((abs(n+k)))
    else:
        print((abs(n)))

# Optimized solution
n,k = list(map(int,input().split()))

n = n%k

if n<abs(n-k):
    print(n)
else:
    print((abs(n-k)))
# Original solution
import sys

n,k = list(map(int,input().split()))
x = k+(n%k)
x = n
min = sys.maxsize

while True:
    if x%k == 0:
        min = 0
        break

    x = abs(x-k)
    if x == min :break
    elif x < min : min = x

print(min)



# Optimized solution
import sys

n,k = list(map(int,input().split()))
x = k+(n%k)
min = sys.maxsize

while True:
    if x%k == 0:
        min = 0
        break

    x = abs(x-k)
    if x == min :break
    elif x < min : min = x

print(min)
# Original solution
N, K = list(map(int, input().split()))

tmp1 = abs(N - K)

while True:
    if K == 1:
        print((0))
        break
    tmp2 = abs(tmp1 - K)
    if tmp1 < tmp2:
        print(tmp1)
        break
    else:
        tmp1 = tmp2


# Optimized solution
N, K = list(map(int, input().split()))
print((min(N % K, K - (N % K))))
# Original solution
n,k = input().split(" ")
n = int(n)
k = int(k)
min = 1000000000000000001
abs0 = 0
if n == 0:
    print((0))
elif k == 1:
    print((0))
elif n == 1:
    print((1))
else:
    for i in range(1000000000):
        abs0 = abs(k - n)
        n = abs0
        if abs0 < min:
            min = abs0
        elif min == abs0:
            print(abs0)
            break

# Optimized solution
n,k = input().split(" ")
n = int(n)
k = int(k)
min = 1000000000000000001
abs0 = 0
if n == 0:
    print((0))
elif k == 1:
    print((0))
elif n == 1:
    print((1))
else:
    if n > k:
        n = n % k
        print((abs(k-n)))
    else:        
        for i in range(1000000000):
            abs0 = abs(k - n)
            n = abs0
            if abs0 < min:
                min = abs0
            elif min == abs0:
                print(abs0)
                break
# Original solution
import sys
n,k=list(map(int,input().split()))
if n==0 or n%k==0:
    print((0))
    sys.exit()
tmp=abs(n-k)
while tmp<n:
    n=tmp
    tmp=abs(n-k)
print(n)

# Optimized solution
import sys
n,k=list(map(int,input().split()))
if n==0 or n%k==0:
    print((0))
    sys.exit()
n=n%k
tmp=abs(n-k)
while tmp<n:
    n=tmp
    tmp=abs(n-k)
print(n)
# Original solution
N, K = list(map(int, input().split()))

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

elif N == K:
    print((0))

elif N == 0:
    print((0))

elif N % 2 == 0 and K % 2 == 0 and N > K and K % N == 0:
    print((0))
elif N % 2 == 0 and K % 2 == 0 and N > K and K % N != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10 ** 18):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break


elif N % 2 == 0 and K % 2 == 0 and N < K and K % N == 0:
    print(N)
elif N % 2 == 0 and K % 2 == 0 and N < K and K % N != 0:
    if abs(N - K) > N:
        print(N)
    else:
        print((abs(N - K)))


elif N % 2 == 1 and K % 2 == 1 and N > K and K % N == 0:
    print((0))
elif N % 2 == 1 and K % 2 == 1 and N > K and K % N != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10**18):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break


elif N % 2 == 0 and K % 2 == 0 and N < K and K % N == 0:
    print(N)
elif N % 2 == 0 and K % 2 == 0 and N < K and K % N != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10 ** 18):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break


elif N % 2 == 0 and K % 2 == 1 and N > K and N % K != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10 ** 18):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break
elif N % 2 == 0 and K % 2 == 1 and N > K and N % K == 0:
    print((0))


elif N % 2 == 0 and K % 2 == 1 and N < K and N % K != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10 ** 18):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break
elif N % 2 == 0 and K % 2 == 1 and N < K and N % K == 0:
    print((0))


elif N % 2 == 1 and K % 2 == 0 and N > K and N % K != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10 ** 18):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break
elif N % 2 == 1 and K % 2 == 0 and N > K and N % K == 0:
    print((0))


elif N % 2 == 1 and K % 2 == 0 and N < K and N % K != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10 ** 18):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break
elif N % 2 == 1 and K % 2 == 0 and N < K and N % K == 0:
    print(N)

else:
    print("NG")



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

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

# Optimized solution
N,K = list(map(int,input().split()))
#N-K -> -K -> -K... gives N%K
#-> (N%K - K) -> (N%K)
answ = N%K
print((min(abs(answ),abs(answ-K))))
# Original solution
N,K=input().split()
N=int(N)
K=int(K)

list=[]


if N%K==0:
    print((0))
else:
    while N not in list:
        list.append(N)
        N=abs(N-K)
    print((min(list)))


# Optimized solution
N,K=input().split()
N=int(N)
K=int(K)

print((min(N%K,K-N%K)))
# Original solution
n,k = list(map(int,input().split()))
answer = 0
if k == 1:
  answer = 0
elif k >= n:
  gap = k-n
  if gap > n:
    answer = n
  else:
    answer = gap
else:
  while n > k:
    n = n - k
  if k >= n:
    gap = k-n
    if gap > n:
      answer = n
    else:
      answer = gap
print(answer)

# Optimized solution
n,k = list(map(int,input().split()))
answer = 0
if k >= n:
  gap = k-n
  if gap > n:
    answer = n
  else:
    answer = gap
else:
  sho = n // k
  if n % k == 0:
    dec = sho - 1
  else:
    dec = sho
  n = n - k*dec
  if k >= n:
    gap = k-n
    if gap > n:
      answer = n
    else:
      answer = gap
print(answer)
# Original solution
n,k = list(map(int,input().split()))
if n>=k:
  t = n%k
else:
  t = n

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


# Optimized solution
#x>Kの時、x => x-k =>x-2k => x-3k ... : xをkで割った余りになる
#x<Kの時、x　=> k-x => x : max(x,k-x)
n,k = list(map(int,input().split()))
if n >= k:
    n = n%k
print((n if n%k <= k-n else k-n))
# Original solution
N, K = list(map(int, input().split()))
Nj = N
while True:
    Ni = Nj
    Nj = abs(Ni - K)
    if Ni > N:
        print(N)
        break
    elif Nj > Ni:
        print(Ni)
        break
    elif K == 1 or Nj == K:
        print((0))
        break
    else:
        continue


# Optimized solution
n, k = list(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)))
# Original solution
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
from bisect import bisect_left 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

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

# Optimized solution
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,k = inpl()
print((min(n%k, k-n%k)))
# Original solution
n,k=list(map(int,input().split()))
n%=k
N=abs(n-k)
while N<n:
    n=N
    N=abs(n-k)
print(n)


# Optimized solution
n,k=list(map(int,input().split()))
n%=k
N=abs(n-k)
if N<n:
  n=N
print(n)
# Original solution
import sys
sys.setrecursionlimit(1000000000)

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

if k == 1:
    print((0))
elif n < k / 2:
    print(n)
else:
    while n >= k / 2:
        n = abs(k - n)
        
    print(n)
    


# Optimized solution
n, k = list(map(int, input().split()))

if k > n:
    tmp = n
else:
    tmp = n % k

if tmp > k / 2:
    tmp = k - tmp

print(tmp)
# Original solution
n, k = list(map(int, input().split()))
L = []
if n > k:
  n = n - k*(n//k)
 
for i in range(k+1):
  L.append(abs(n-k))
  n = abs(n-k)
  
print((min(L)))  

# Optimized solution
n, k = list(map(int, input().split()))
L = []
if n > k:
  n = n - k*(n//k)
 
if n*2 <= k:
  print(n)
else:
  print((k-n))
# Original solution
n,k=list(map(int,input().split()))

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

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

print(n)

# Optimized solution
n,k=list(map(int,input().split()))

print((min(n%k,abs(n%k-k))))
# Original solution
if __name__ == '__main__':
    n, k = [int(i) for i in input().split(" ")]
    while abs(n - k) <= n:
        if n%(k*10)==0:
            n = int(n/(k*10))
        else:
            n = abs(n - k)
        
    print(n)

# Optimized solution
if __name__ == '__main__':
    n, k = [int(i) for i in input().split(" ")]
    while abs(n - k) < n and n != 0:
        n = n%k
        if n != 0:
            n = abs(n - k)
    print(n)
# Original solution
n = list(map(int, input().split()))
re = True
num = n[0]
if n[1] == 1:
  print((0))
elif n[1] == 2:
  if n[0]%2 == 0:
    print((0))
  else:
    print((1))
else:
  while re:
    k = num - n[1]
    if num > abs(k):
      num = abs(k)
    else:
      re = False
  print(num)

# Optimized solution
n = list(map(int, input().split()))
k = n[0] % n[1]

if abs(k - n[1]) > k:
  print(k)
else:
  print((abs(k - n[1])))
# Original solution
n, k = list(map(int, input().split()))

ans = abs(n - k)

i = False

if n % k == 0:
  ans = 0
  i = True

while not i:
  if ans > abs(ans - k):
    ans = abs(ans - k)
  else:
    i = True

print(ans)

# Optimized solution
n, k = list(map(int, input().split()))

ans = n % k

i = False

if ans == 0:
  i = True

while not i:
  if ans > abs(ans - k):
    ans = abs(ans - k)
  else:
    i = True

print(ans)
# Original solution
def replaceInt(N, M):
    N = abs(N-M)
    return N
    
N, M = [int(i) for i in input().split()]

if N == M :
    output = 0
elif N < M :
    if N <= M / 2:
        output = N
    else:
        output = abs(M-N)
else :
    if M == 1:
        output = 0
    else :
        output_candidates = [N]
        while True:
            N = replaceInt(N,M)
            if N in output_candidates:
                break
            output_candidates.append(N)
        output = min(output_candidates)
        
print(output)


# Optimized solution
def replaceInt(N, M):
    N = abs(N-M)
    return N
    
N, M = [int(i) for i in input().split()]

if N == M :
    output = 0
elif N < M :
    if N <= M / 2:
        output = N
    else:
        output = abs(M-N)
else :
    if M == 1:
        output = 0
    else :
        N = N % M
        output_candidates = []
        while True:
            N = replaceInt(N,M)
            if N in output_candidates:
                break
            output_candidates.append(N)
        output = min(output_candidates)
        
print(output)
# Original solution
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:
#     while(int(len(str(N))) > 2):
#         N = int(N / K);
        
flag = True;
while flag == True:
    N = abs(N - K);
    if(N <= min):
        min = N;
    else:
        flag = False;
    
print(min);

# Optimized solution
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);
# Original solution
n,k = list(map(int, input().split()))

t = n // k

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

# Optimized solution
n, k = list(map(int, input().split()))

l = n // k

print((min(n - l * k, abs((n - l * k) - k))))
# Original solution
def solve(n, k):
    occurence = set()

    diff = n % k

    if diff == 0:
        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)


# Optimized solution
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)
# Original solution
N, K = list(map(int, input().split()))

if K == 1:
  res = 0
elif K == 2:
  res = K % 2
else:
  diff = abs(N-K)
  while diff < N:
    N  = diff
    diff = abs(N-K)
 
  res = N
  
  
print(res)
  

# Optimized solution
N, K = list(map(int, input().split()))
rem = N %K
print((min(rem, abs(rem - K))))
# Original solution
n,k=list(map(int,input().split()))
if n>k:
    n=n%k
print((min(n,k-n)))

# Optimized solution
n,k=list(map(int,input().split()))
n=n%k
print((min(n,k-n)))
# Original solution
N, K = list(map(int,input().split()))
if K == 1 or N == 0 or N == K:
    print((0))
    exit()
elif K == 0:
    print(N)
    exit()
elif N == 1 or N == K-1 or K == N-1:
    print((1))
    exit()
else:
    res = []
    ans = N
    for i in range(max(N,K)):
        ans = abs(ans - K)
        res.append(ans)
        if i >= 2:
            if res[i] == res[i-2]:
                print((min(res)))
                exit()
    print((min(res)))

# Optimized solution
N, K = list(map(int,input().split()))
if K == 1 or N == 0 or N == K or N%K==0:
    print((0))
elif K == 0:
    print(N)
elif N == 1 or N == K-1 or K == N-1 or N%K==1:
    print((1))
elif N > K:
    res = abs(N - (K*(N//K)))
    if abs(res - K) < res:
        print((abs(res-K)))
    else:
        print(res)
elif K > N:
    if abs(K-N) < N:
        print((abs(K-N)))
    else:
        print(N)
# Original solution
n, k = list(map(int, input().split()))
loop = True
while loop:
    if n > abs(n - k):
        if n > k:
            n = min(n, n % k)
        elif k > n:
            n = min(n, k % n)
    else:
        loop = False

print(n)


# Optimized solution
n, k = list(map(int, input().split()))
if n == k:
    n = 0
else:
    while n > abs(n - k):
        if n > k:
            n = min(n, n % k)
        elif k > n:
            n = min(n, k % n)

print(n)
# Original solution
li = list(map(int, input().split()))

new_li = []
n = 0

while True:
  if li[1] == 1:
    print((0))
    break

  li[0] = abs(li[0]-li[1])
  new_li.append(li[0])
  
  if n>=2:
    if new_li[n] == new_li[n-2]:
      print((min(new_li)))
      break
  n += 1

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

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