CodeEff/ECCO huggingface dataset:
# Original solution
n, k = [int(i) for i in input().split()]

def a(x):
    return abs(x - k)

if n > k:
    tmp = k 
    while not (n - tmp) % k == 0:
        tmp -= 1
    n = tmp

while True:
    tmp = n
    n = a(n)
    if tmp < n:
        print(tmp)
        break


# Optimized solution
n, k = [int(i) for i in input().split()]
print((min(n % k, k - n % k)))
# Original solution
n, k = list(map(int,input().split()))
while(True):
  nk_abs = abs(n - k)

  if n <= k and n <= nk_abs:
    break
  if k == 1:
    n = 0
    break
  n = nk_abs

print(n)

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

answer = 10**18+1
formar = 10*18+1
c_v = N

if N % K == 0:
    print((0))
else:
    N = N//K
    while True:
        if answer > c_v:
            answer = c_v
        n_v = abs(c_v - K)
        if formar == n_v:
            break
        formar = c_v
        c_v = n_v
    print(answer)


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

if N % K == 0:
    print((0))
else:
    N = N % K
    print((min([N, abs(N-K)])))
# Original solution
n,k=list(map(int,input().split()))
a=[]
if k==1:
    print((0))
else:
    while True:
        n=abs(n-k)
        a.append(n)
        if a[0]==abs(n-k):
            break
        elif abs(n-k)==0:
            a.append(0)
            break
    print((min(a)))

# Optimized solution
n,k=list(map(int,input().split()))
if k==1:
    print((0))
else:
    print((min(n%k,abs(n%k-k))))
# Original solution
N, K = list(map(int, input().split()))
res = [N]
if N % K == 0:
  print((0))
else:
  while True:
    M = abs(N - K)
    if M  in res:
      break
    elif M not in res:
      N = M
      res.append(N)
  print((min(res)))

# Optimized solution
N, K = list(map(int, input().split()))
cand1 = N % K
cand2 = K - (N % K)
print((min(cand1, cand2)))
# Original solution
N, K = list(map(int,input().split()))

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)

# Optimized solution
N, K = list(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)
'''
# Original solution
n,k=list(map(int,input().split()))
ans=n
a=[]
if k==1:
  print((0))
elif abs(n-k) > n:
  print(n)
else:
  while len(a)==len(set(a)):
    n=abs(n-k)
    a.append(n)
    ans=min(ans,n)
  print(ans)

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

a=n%k
ans=min(a,k-a)
print(ans)
# Original solution
import math
N,K= list(map(int,input().split()))
Num = 10*18
while Num > 0:
    if N % K ==0:
        Num = 0
        break
    if Num > abs((N%K)-K):
        Num = abs((N%K)-K)
        N  = Num
        if N < abs((N%K)-K):
            break
print(Num)


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

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

if n%k==0:
  print((0))
  
else:
  while True:
    if abs(n-k)<=be:
      be=abs(n-k)
    else:
      print(be)
      sys.exit()
    n=abs(n-k)

# Optimized solution
import math

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

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

print((be if be<=af else af))
# Original solution
def solve():
    N, M = list(map(int, input().split()))
    
    if M == 1:
        return 0
    
    n = N
    c = 0
    while True:
        n = n-M
        c = (abs(n) - M)
        
        if c <= 0:
            break

    return min(abs(n), abs(c))

print((solve()))

# Optimized solution
def solve():
    N, M = list(map(int, input().split()))
    
    if N % M == 0:
        return 0
    
    a = N % M

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

print((solve()))
# Original solution
def MI():return list(map(int,input().split()))
def LI():return list(MI())

N,K=MI()

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

# Optimized solution
n,k=list(map(int,input().split()))
print((min(n%k,(-n)%k)))
# Original solution
N, K = list(map(int, input().split()))
x = N
x_b = 0

if K == 1:
    x = 0
else:
    while True:
        x_b = x
        x = abs(x-K)
        if abs(x-K) == x_b:
            break

print((min(x_b,x)))


# Optimized solution
N, K = list(map(int, input().split()))
a = (N // K) + 1

ans1 = -(N-K*a)
ans2 = N - (K*(N//K))
print((min(ans1,ans2)))
# Original solution
n,k=list(map(int,input().split()))
n=int(n)
k=int(k)
n = n % k
minimam = k
for i in range(k):
  if n < minimam:
    minimam = n
  n = abs(n-k)
print(minimam)

# Optimized solution
n,k=list(map(int,input().split()))
n=int(n)
k=int(k)
n = n % k
minimam = n
record=[]
for i in range(k):
  n=abs(n-k)
  if n < minimam:
    minimam = n
  record.append(n)
  if n in record:
    break
print(minimam)
# Original solution
def min(a, b):
    if a >= b:
        res = a - b
    elif a < b:
        res = b - a
    return res

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

min_num = float('inf')
res_list = []
while(1):
    res = min(n, k)
    if res < min_num:
        min_num = res
    if res == 0:
        break
    if res in res_list:
        break
    n = res
    res_list.append(res)

print(min_num)


# Optimized solution
def min(a, b):
    if a >= b:
        res = a - b
    elif a < b:
        res = b - a
    return res

n,k = list(map(int, input().split()))
if n%k == 0:
    print((0))
    quit()
else:
    n = n%k

min_num = float('inf')
res_list = []
while(1):
    res = min(n, k)
    if res < min_num:
        min_num = res
    if res == 0:
        break
    if res in res_list:
        break
    n = res
    res_list.append(res)

print(min_num)
# Original solution
N, K = list(map(int,input().split()))
while True:
    if N%K == 0:
        print((0))
        break
    if abs(N-K) < N:
        N = abs(N-K)
    else:
        print(N)
        break

# Optimized solution
N, K = list(map(int,input().split()))
while True:
    if N%K == 0:
        print((0))
        break
    else:
        N=N%K
        N = min(N,abs(N-K))
        print(N)
        break
# Original solution
n, k = list(map(int, input().split()))

def solve():
    global n, k
    n = n - (k * (n // k))
    pre = n
    while True:
        n = min(n, abs(n-k))
        if n == pre:
            break
        pre = n
    return n

print((solve()))

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

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

# Optimized solution
N,K=list(map(int,input().split()))
N=N%K
print((min(N,abs(N-K))))
# Original solution
n,k = list(map(int, input().split()))
a = [n]
i = 0
if n%k == 0:
    print((0))
else:
    while i >= 0:
        n = abs(n-k)
        a.append(n)
        i += 1
        if a[i] > a[i-1]:
            print((min(a)))
            break

# Optimized solution
n,k = list(map(int, input().split()))
a,b = divmod(n,k)
print((min(b,k-b)))
# Original solution
n,k = list(map(int,input().split()))
if k==1 or n%k==0:
    print((0))
    exit()
while n > abs(n-k):
    n = abs(n-k)
print(n)

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

# if N == K:
#     print(0)
#     exit(0)
# elif N < K:
#     print(N)
#     exit(0)

ans = []
before = abs(N - K)
N = N % K
for i in range(10000):
    ze = abs(N - K)
    ans.append(ze)
    before = ze
    N = ze
print((min(ans)))


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


N = N % K
ans1 = abs(N - K)
ans2 = abs(ans1-K)
print((min(ans1, ans2)))
# Original solution
N, K = list(map(int, input().split()))

"""
while N > K:
  N = abs(N - K)
  #print(N)

print(min(N, abs(N-K)))
"""
"""
m = -(-N//K)
print(abs(N - m*K))
"""

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

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

"""
while N > K:
  N = abs(N - K)
  #print(N)

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

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

"""
print(min(N%K, abs(N%K - K)))
"""
# Original solution
N,K = list(map(int,input().split()))
if N%K==0:
    print((0))
elif K%N==0:
    print(N)
else:
    while abs(N-K) < N:
        N = abs(N-K)
    print(N)

# Optimized solution
N,K = list(map(int,input().split()))
if N>=K:
    N = N%K
    while abs(N-K) < N:
        N = abs(N-K)
    print(N)
else:
    if K >= 2*N:
        print(N)
    else:
        N = K%N
        print(N)
# Original solution
def getInput():
    return [int(i) for i in input().rstrip().split(' ')]

def swap(x, K):
    return abs(x-K)

N, K = getInput()
x = N
mins = x

while(mins > 0):
    if K == 1:
        mins = 0
    x = swap(x, K)
    if  x < mins:
        mins = x
    else:
        break

print(mins)

# Optimized solution
def getInput():
    return [int(i) for i in input().rstrip().split(' ')]

N, K = getInput()
t = N % K 
print((min(t, K -t)))
# Original solution
#n=int(input())
n,k=list(map(int,input().split()))
#l=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]
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)

# Optimized solution
#k=int(input())
n,k=list(map(int,input().split()))
#hl=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]
t=n%k
print((min(t,k-t)))
# Original solution
import sys

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

pre = N
while True:
    if N % K == 0 and N // K > 1:
        print((0))
        sys.exit()
        
    N = abs(N-K)
    if pre <= N:
        print(pre)
        sys.exit()
    else:
        pre = N

# Optimized solution
import sys

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

pre = N
while True:
    if N % K == 0 and N // K >= 1:
        print((0))
        sys.exit()
        
    if N > K:
        N = N % K
    else:
        N = abs(N-K)
    
    if pre <= N:
        print(pre)
        sys.exit()
    else:
        pre = N
# Original solution
n, k = list(map(int, input().split()))

tmp = n % k
if n >= k:
    ans = min(tmp, k - tmp)
else:
    ans = min(n, k - n)

print(ans)

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

tmp = n % k
ans = min(tmp, k - tmp)

print(ans)
# Original solution
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 = absolute(x, k)

    print(x)



# Optimized solution
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)
# Original solution
n,k=list(map(int,input().split()))
if n>k:
    n=n-n//k*k
ans=10**18+10
for i in range(10000):
    ans=min(ans,n)
    n=abs(n-k)
print(ans)

# Optimized solution
n,k=list(map(int,input().split()))
print((min(n%k,k-n%k)))
# Original solution
n,k =list(map(int,input().split()))
x1,x2,cnt =n,0,0
if n%k==0:
  print((0))
else:  
  while True:
    x2 = abs(x1-k)
    if min(x1,x2) ==x1:
      print(x1)
      break
    x1=x2

# Optimized solution
n,k =list(map(int,input().split()))
if n%k==0:
  print((0))
else:
  print((min(n%k,abs((n%k)-k))))
# Original solution
n,k=list(map(int,input().split()))
ans=float('inf')
if k == 1:
    print((0))
    exit()
elif n < k :
    print((min(n,k-n)))
else:
    for i in range(n):
        n=abs(n-k)
        ans=min(ans,n)
    
    print(ans)

# Optimized solution
n,k=list(map(int,input().split()))
print((min(n%k,k-n%k)))
# Original solution
N,K=list(map(int,input().split()))
while N>100000000000000000*K:
  N=N-100000000000000000*K
while N>100000000000000*K:
  N=N-100000000000000*K
while N>1000000000000*K:
  N=N-1000000000000*K
while N>10000000000*K:
  N=N-10000000000*K
while N>100000000*K:
  N=N-100000000*K
while N>1000000*K:
  N=N-100000*K
while N>1000*K:
  N=N-1000*K
while N>100*K:
  N=N-100*K
while N>10*K:
  N=N-10*K
while N>K:
  N=N-K

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

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


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

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

if N<N2:
  print(N)
else :
  print(N2)
# Original solution
def main():
    n, k = list(map(int, input().split()))
    if n - k > k:
        n_k = (n-k) // k
        minv = n - k * n_k
    else:
        minv = n

    def min_x(x):
        minv = x
        for _ in range(2):
            if abs(minv - k) < abs(minv):
                minv = abs(minv - k)
            else:
                print(minv)
                exit()
        print(minv)

    min_x(minv)


if __name__ == '__main__':
    main()

# Optimized solution
n, k = list(map(int, input().split()))
mod = abs(n-k) % k
print((min(mod, k-mod)))
# Original solution
n,k=list(map(int, input().split()))
a=n//k
print((min(abs(n-k*a), abs(n-k*(a+1)))))

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

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

print((min(r)))


# Optimized solution
n, k = list(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)))
# Original solution
n,k = list(map(int,input().split()))
 
if k==1 :
  ln=0
elif k==0 :
  ln=n
else:
  while 1:
    ln = n
    n = abs(n-k)
    if ln<=n:
      break
print(ln)

# Optimized solution
n,k = list(map(int,input().split()))
 
if k==1 :
  ln=0
elif k==0 :
  ln=n
elif n>=k:
  a = n%k
  n=a
  while 1:
    ln = n
    n = abs(a-k)
    if ln<=n:
      break
else:
  while 1:
    ln = n
    n = abs(n-k)
    if ln<=n:
      break
print(ln)
# Original solution
import sys
n ,k = list(map(int, input().split()))
 
 
if n == k or k == 1:
    print((0))
    sys.exit()
    
while True:
    b = abs(n-k)
    if b == 0:
        print((0))
        sys.exit()
    elif b == 1:
        print((1))
        sys.exit()
    elif n < b:
        print(n)
        sys.exit()
    n = b

# Optimized solution
import sys
n ,k = list(map(int, input().split()))
if n == k or k == 1:
    print((0))
    sys.exit()
else:
    n %= k
print((min(n, abs(n-k))))
# Original solution
n,k = list(map(int, input().split()))

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

min = 10**18

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

print(min)






# Optimized solution
n,k = list(map(int, input().split()))
n = n % k
a = abs(n-k)
if n <= a:
  print(n)
else:
  print(a)
# Original solution
N,K=list(map(int,input().split()))
if N==0 or K==1:
    print((0))
else:
    for i in range(10**18):
        if N>abs(N-K):
            N=abs(N-K)
        else:
            print(N)  
            break

# Optimized solution
N,K=list(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
# Original solution
n, k = list(map(int, input().split()))
Min = n
while True:
    if k == 1:
        print((0))
        exit()
    n = abs(n-k)
    if Min == n:
        break
    if Min > n:
        Min = n
print(Min)


# Optimized solution
n, k = list(map(int, input().split()))
r = n%k
print((min(r, k-r)))
# Original solution
def abs_result(x,y):
    tmp = abs(x-y)
    return tmp

def main():
    line = list(map(int,input().split()))
    n,k = line[0],line[1]
    
    # nを、nとkの差の絶対値で置き換える
    if n == 0 or k == 1:
        print((0))
        return
    
    com = 2
    for i in range(0,10**18):
        ans = abs_result(n,k)
        if ans == 0:
            print((0))
            return
        elif ans == 1:
            print((1))
            return
        if ans <= com:
            print(ans)
            return
        n = ans

main()

# Optimized solution
def main():
    n,k = list(map(int,input().split()))
    t = n % k
    x = k - t
    if t < x:
        print(t)
    else:
        print(x)
main()
# Original solution
def main():
  N, K = list(map(int,input().split()))
  sett = set()
  ans = float('inf')
  if N%K==0:
    print((0))
    return
  while N not in sett:
    sett.add(N)
    N = abs(N-K)
    ans = min(ans,N)
  print(ans)
  return 
main()

# Optimized solution
def main():
  N, K = list(map(int,input().split()))
  print((min(N%K, K-N%K)))
  return 
main()
# Original solution
n, k = list(map(int, input().split()))

while True:
    if not n%k:
        n = 0
        break
    num = n
    n = n-k
    if n < 0:
        n = min(abs(n), num)
        break

print(n)

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

a = n%k

num = min(a, abs(a-k))

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

while True:
  if K == 1:
    answer = 0
    break
  if N <= abs(N - K):
    answer = N
    break
  else:
    N = abs(N - K)

print(answer)
    

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

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

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

def absdiff(x, K):
    return abs(x - K)

def culc(N, K):
    if K == 1:
        print((0))
        return

    if N*2 == K:
        print(N)
        return

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

    x = N
    min = x
    while min >= x:
        #print(min, x)
        min = x
        x = absdiff(x, K)
        if min == 1:
            break

    print(min)
    return


culc(N, K)


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

def absdiff(x, K):
    return abs(x - K)

def culc(N, K):
    if K == 1:
        print((0))
        return

    if N*2 == K:
        print(N)
        return

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

    x = N % K
    min = x
    while min >= x:
        #print(min, x)
        min = x
        x = absdiff(x, K)
        if min == 1:
            break

    print(min)
    return


culc(N, K)
# Original solution
n,k=list(map(int,input().split()))
if k==1 or n%k==0:
    ans=0
else:
    if n/2<k:
        x=abs(n-k)
        y=abs(x-k)
        ans=min(n,min(x,y))
    else:
        while 1:
            x=abs(n-k)
            y=abs(x-k)
            n=min(n,min(x,y))
            if n<k:
                x=abs(n-k)
                y=abs(x-k)
                ans=min(n,min(x,y))
                break
print(ans)


# Optimized solution
n,k=list(map(int,input().split()))
if k==1 or n%k==0:
    ans=0
else:
    if n/2<k:
        x=abs(n-k)
        y=abs(x-k)
        ans=min(n,min(x,y))
    else:
        x=n%k
        y=abs(x-k)
        ans=min(x,y)
print(ans)
# Original solution
N, K = list(map(int, input().split()))

N = N % K

if (N == 0):
    print(N)
elif (N == K):
    print((0))
elif (K == 1):
    print((0))
else:
    while (N >= 0):
        if (N > abs(N - K)):
            N = abs(N - K)
        else:
            print(N)
            break

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

N = N % K

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

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

result = N
if N > K:
    result = min(result, N % K)
for i in range(1000):
    result = min(result, abs(result - K))
print(result)


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

t = N % K
print((min(t, K - t)))
# Original solution
def resolve():
    N, K = list(map(int, input().split()))
    if (K == 1):
        print((0))
        return
    if N % K == 0:
        print((0))
        return
    x = N
    memo = [N]
    while True:
        x = min(abs(x - K), x)
        if x in memo:
            print((min(memo)))
            return
        memo.append(x)
    return


resolve()


# Optimized solution
def resolve():
    N, K = list(map(int, input().split()))
    print((min(N % K, K - (N % K))))
    return


resolve()
# Original solution
import sys
input = sys.stdin.readline

def binary_search_int(ok, ng, solve):
    """
    :param ok: solve(x) = True を必ず満たす点
    :param ng: solve(x) = False を必ず満たす点
    """
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if solve(mid):
            ok = mid
        else:
            ng = mid
    return ok, ng

##############################################################

def solve(x):
    """ ある点 x を境にして True と False が二分されるような関数"""
    return N - K*x >= 0

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

a, b = binary_search_int(0, 10**18, solve)
print((min(abs(N-a*K), abs(N-b*K))))

# Optimized solution
import sys
input = sys.stdin.readline

def binary_search_int(ok, ng):
    """
    :param ok: solve(x) = True を必ず満たす点
    :param ng: solve(x) = False を必ず満たす点
    """
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if solve(mid):
            ok = mid
        else:
            ng = mid
    return ok, ng

##############################################################

def solve(x):
    """ ある点 x を境にして True と False が二分されるような関数"""
    return N - K*x >= 0

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

a, b = binary_search_int(0, 10**18)
print((min(abs(N-a*K), abs(N-b*K))))
# Original solution
# Problem C - Replacing Integer

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

# initialization
compare_1 = N % K
compare_2 = abs(compare_1 - K)
ans = min(compare_1, compare_2)

# output
print(ans)


# Optimized solution
# Problem C - Replacing Integer

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

# initialization
tmp_1 = N % K
tmp_2 = abs(tmp_1 - K)
ans = min(tmp_1, tmp_2)

# output
print(ans)
# Original solution
N, K = list(map(int, input().split()))
ans = N
x = abs(N - K)
if N % K == 0:
    print((0))
else:
    while ans > x:
        ans = x
        x = abs(x - K)
    else:
        print(ans)

# Optimized solution
N, K = list(map(int, input().split()))
ans = N
a = ans // K
b = ans % K

if a > 0:
    ans = N - K * a
    a = ans // K
    b = ans % K

if a  == 0:
    if ans > abs(ans - K):
        print((abs(ans - K)))
    else:
        print(ans)
# Original solution
import sys

n,k = list(map(int, input().split()))
if n == 0 or k == 1:
  print((0))
  sys.exit()
while True:
  ans = abs(n-k)
  if ans >= n:
    break
  elif n > ans:
    n = ans
print(n)

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

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

else:
    while True:
        if N // K == 0:
            # print('N % K = ' + str(N % K) + ', abs(N - K) = ' + str(abs(N - K)))
            print(N % K) if N % K < abs(N - K) else print(abs(N - K))
            break;

        else:
            # print('N % K = ' + str(N % K) + ', abs(N - K) = ' + str(abs(N - K)))
            N = abs(N - K * (N//K))


# Optimized solution
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))
# 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(' ')))
print((min(N % K, abs((N % K) - K))))
# Original solution
# -*- coding: utf-8 -*-

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

if K == 1:
    N = 0

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



# Optimized solution
# -*- coding: utf-8 -*-

N, K = list(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)