peterverebics/CodeNet_Python_118 huggingface dataset:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline

def main():
    n,k = map(int,input().split())
    dp=[n,k,abs(n-k)]
    if k==1:
        print(0)
        exit()
    elif n ==0:
        print(k)
        exit()
    while True:
        if min(dp) > (k-dp[-1]):
            dp.append(k-dp[-1])
        else:
            break
    print(min(dp))

if __name__=="__main__":
    main()
#abc 161c
import math
n,k = map(int, input().split())
i = int(n/k)
ilow = i
iup = i+1
print(min(n,abs(n-k*iup),n-k*ilow))
n,k = map(int,input().split())
a=n/k
if a >1:
    b=n%k
    if b==0:
        print(0)
    else:
        print(abs(b-k))
else:
    print(n)
    exit()
(a, b)=list(map(int, input().split()))
if a < b:
  print(a)
else:
  c = a % b
  if c == 0:
    print('0')  
  else:
    print(abs(b-c))
N,K = map(int,input().split())

def replace(N,K):
    if(K == 1 or N==K):
        return 0
    if(N<K):
        return N
    else:
        flag = 0
        while(abs(N-K)>=0):
            N = abs(N-K*(N//K))
            if(N == 0 ):
                return 0                
            elif (N-K<0):
                return abs(N-K)
print(replace(N,K))
def gcd(x, y): 
  
   while(y): 
       x, y = y, x % y 
  
   return x 
a,b=map(int,input().split())
if a==0:
    print(0)
else:
    if a%b==0:
        print(0)
    else:
        print(gcd(a,b))
n,k = map(int,input().split())
     
m = int(n/k)
n = n-m*k
     
print(min(n,abs(n-k)))
N,K = map(int,input().split())
if N > K:
    print(N % K)
elif N < K:
    ans = min(N,K-N)
    print(ans)
if N == K:
    print(0)
def MI(): return map(int, input().split())

if __name__ == "__main__":
    N, K = MI()
    if N < K:
        print(N)
    elif N%K > abs(K - N%K):
        print(abs(K - N%K))
    else:
        print(N%K)
n,k = map(int, raw_input().split())
from fractions import gcd

if n % k== 0:
  print 0
else:
  print gcd(n,k)
import math
n, k = map(int, input().split())
if n <= k: print(min(n, k - n))
else:
    t = math.ceil((n - k) / k - 10 ** -8)
    n = n - t * k
    print(min(n, k - n))
n, m = (int(x) for x in input().split())

a = n % m
b = n / m
if b < 1:
    print(n)
elif b == 1:
    print(0)
elif abs(a-m) > a:
    print(a)
else:
    print(abs(a-m))
N,K=map(int,input().split())
print(0 if N%K==0 else 1 if N>K else N if K//2>N else K-N)
N,K=map(int,input().split())
if K==1:
  if N==0:
    print("1")
  else:
    print("0")
if N > K :
  N-=(N//K)*K
if abs(K-N) < N:
  print(abs(K-N))
else:
  print(N)
n, k = map(int,input().split())
import math
a = n/k
b = math.ceil(a)
if a < 1 and abs(n-k) > n:
  print(n)
else:  
  x = n-(k*b)
  print(abs(x))
import math

data = input().split()

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

div = math.floor(n/k)
n2 = n - div*k
if div==0:
	n2 += k

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

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

if n<k:
    print(n)
    exit()
else:
    minV = (n%k) if (n%k) < (k-n%k) else (k-n%k)
    print(minV)
from sys import maxsize

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

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

if (n < k):
    if (n % k) <= abs(n - k):
        print(n % k)
    else:
        print(1)
elif (n >= k):
    if (l == 0):
        print(0)
    else:
        print(abs(l - k))
import math
num, k = map(int,input().split())

per = math.floor(num/k)
count = 0
least = num-k*per

for i in range(num):
    if math.fabs(num-(per+i) * k) > math.fabs(num - (per+(i+1))*k):
        count += 1
    else:
        break

print(math.floor(math.fabs(num - (per + count) * k)))
N,K = map(int, input().split())

result1 = N
result = abs(N - K)

if result1 < result:
  print(result1)
  
else:
  while result1 > result or result == 0:
    result1 = result
    result = abs(result - K)
    
  else:
    print(result)
N, M = map(int,input().split())
if N*2<M:
    print(N)
elif M == 1:
    print(0)
else:
    yo = N % M
    print(M-yo)
N, K = map(int, input().split())

sub = int(N / K)
val1 = N - K * sub
val2 = K * (sub+1) - N
val3 = N - K * (sub-1)

if val1 >= val3:
    print(val3)
elif val1 >= val2:
    print(val2)
else:
    print(val1)
N, K = map(int, input().split())

if(N%2==0):
    if(K%2==0):
        print(0)
else:
    print(1)
N, K = map(int, input().split())
s = int(N/K)

a1 = abs(N-K*s)
a2 = abs(N-K*(s+1))

if a1 < a2:
  print(a1)
else:
  print(a2)
#c Aoki-kun

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



y = abs(N - K)
N = y
y2 = abs(N - K)
c=1
dif = (y, y2)

if dif[c-1] <= dif[c]:
    print(dif[-1])
    
if dif[c-1] > K:

    dif += (dif[c-1]-(dif[c-1]-K),)

while dif[c-1] > dif[c]:

    yy = abs(dif[c] - K)
    dif += (yy,)
    c += 1


print(min(dif))
l = list(map(float,input().split()))
c = abs(l[0]-l[1])
x = l[0]
set_a = set()
if c>l[0]:
    print(int(l[0]))
else:
    while x>0:
        c = abs(c-l[1])
        set_a.add(c) 
        x -= 1
    print(int(min(list(set_a))))
N, K = 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:
    print(1)
    exit()
else:
    l = []
    ans = N
    for i in range(min(N,K)):
        ans = abs(ans - K)
        l.append(ans)
        if i >= 1:
            if ans in l:
                print(min(l))
                exit()
    print(min(l))
N, K = list(map(int, input().split()))
from math import ceil 
if N > K:
    p = ceil(N/K - 1)
    N_p = N - p*K
    N_q  = -(N_p - K)
    ans = min(N_p, N_q)
else:
    ans = min(N, K-N)
print(ans)
n,k=map(int,input().split())
if k >= 2*n:
	print(n)
elif k == n:
	print(0)
elif k ==1:
	print(0)
else:
	print(1)
n, k = [int(i) for i in input().split()]
l = []
ans = n
while True:
  if n <= k:
    l.append(ans)
    break
  elif ans > 0:
    ans = ans - k
    l.append(ans)
  else:
    break

print(abs(min(l)))
import math
n,k = map(int,input().split())
     
m = math.floor(n/k)
n = n-m*k
     
print(min(n,k-n))
N, K = map(int, input().split())
print(N%K, abs(N%K-K))
n,k = map(int,input().split())
t = n%k
if(n < abs(k-n)):
  print(n)
else:
  print(abs(k-n))
import math
n, k = map(int, input().split())
if n <= k: print(min(n, k - n))
else:
    t = math.ceil((n - k) / k - 10 ** -6)
    n = n - t * k
    print(min(n, k - n))
import math
n,k=map(int,input().split())
if n>k:
    num=math.floor(n/k)
    n=n-num*k
    ans=min(abs(k-n),n)
    print(ans)
else:
    print(min(k-n,n))
#c Aoki-kun

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



y = abs(N - K)
N = y
y2 = abs(N - K)
c=1
dif = (y, y2)

if dif[c-1] <= dif[c]:
    print(dif[-1])
    
if dif[c-1] > K:

    dif += (dif[c-1]-(dif[c-1]-K),)
    c += 1

while dif[c-1] > dif[c]:

    yy = abs(dif[c] - K)
    dif += (yy,)
    c += 1


print(min(dif))
N, K = map(int, input().split())

if N >= K:
  print(N % K)
else:
  print(N)
def op(n, k):
  return abs(n - k)


if __name__ == '__main__':
    n, k = map(int, input().split())
    print(abs(n % k))
#abc 161c
import math
n,k = map(int, input().split())
i= (n/k)
ilow = int(i)
iup = i+1
print(min(n,abs(n-k*iup),n-k*ilow))
LOOP_COUNT = 100
i = [int(x) for x in input().split()]
n = i[0]
k = i[1]

for i in range(LOOP_COUNT):
    if n >= k:
        n = n % k
    elif n == 0:
        break
    else:
        n = k - n
print(n)
n, k = map(int, input().split())

if k == 1 or n == 1:
    print(0)
else:
    n = n%k
    print(min(n,k-n))
#https://atcoder.jp/contests/abc161/tasks/abc161_c
# -*- coding: utf-8 -*-
N,K = map(int,input().split())
syou = N // K
A = N - syou*K
B = N - (syou+1)*K
if abs(A) >= abs(B):
  print(B)
else:
  print(A)
n, k = list(map(int, input().split()))
x = n % k
print(max(x, x-k))
n,k=map(int,input().split())
flag=True
tmp=n-int(n/k)*k
cnt=k

while(flag):
    if tmp > abs(tmp-k) :
        tmp=abs(tmp-k)
        cnt+=1
    else:
        flag=False
        print(tmp)
n,k = map(int,input().split())

if n % k == 0:
  print(0)
elif n / k != 0:
  min = abs(n - (k * int(n / k)))
  if min > abs(min - k):
    min = abs(min - k)
  print(min)
elif n > abs(n - k):
  print(abs(n - k))
else:
  print(n)
import math
num, k = map(int,input().split())

per = math.floor(num/k)
count = 0

for i in range(num):
    if math.fabs(num-(per+i)*k) > math.fabs(num-(per+(i+1))*k):
        count += 1
    else:
        break

print(math.floor(math.fabs(num-(per+count)*k)))
n, k = map(int, input().split())

a = n // k

if n - k * a <= -(n - k * (a + 1)):
    print(n - k * a)

else:
    print(-n)
n,k=map(int,input().split())
if(n>k):
    if(n%k==0):
        print(0)
    else:
        print(min((n%k),(n//k+1)*k-n))
elif(n==k):
    print(0)
elif(n==0):
    print(k)
else:
    if(k%n==0):
        print(n)
    else:
        if(k-n)<=n:
            print(k-n)
        else:
            print(n)
N,K=map(int,input().split())
for i in range(10**6):
    N=min(N,abs(N-K))
print(N)
N,K = map(int,input().split())

x = abs(N-K)

if K==1:
    print(0)
else:
    for i in range(1000000):
        x = min(x,abs(x-K))
    print(x)
N,K = map(int,input().split())

def replace(N,K):
    if(K == 1):
        return 0
    if (N>=K):
        return abs(N-K*round(N/K))
    else:
        return N

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


if N//2<K:
    ans = abs(N%K-K)
else:
    ans = N % abs(N-K)
if N<K:
    ans = N
if N%K==0:
    ans = 0
print(ans)
n, k = map(int, input().split())
if abs(n-k) == 0 or k==1:
  print(0)
elif n <= abs(n-k):
  print(n)
else:
  print(n%abs(n-k))
aa=list(map(int,input().split()))
n=aa[0]
k=aa[1]
minn=n%k
print(abs(min(n%k,(n%k)-k)))
n,k=map(int,input().split())
x=abs(n-k)

if n>k:
    print(x%k)
else:
    print(n)
import sys

input = sys.stdin.readline

N, K = map(int, input().split())
diff = abs(K - N)

if N < K:
    print(N)
    exit()

if N % K == 0:
    print(0)
else:
    print(min(K - N%K, N%K))
i = [int(i) for i in input().split()]
n = i[0]
k = i[1]
n2 = n % k
print(0 if n2 == 0 else n2 if n == n2 else k % n2)
N, K = map(int, input().split())
store = N
x = N % K
if N == 0:
    print(0)
else:
    while x < store:
        store = x
        x = abs(x - K)

    print(store)
n, k = map(int, input().split())
list = [n]
list.append(abs(n-k))
while(abs(list[-1]) >= abs(list[-2])):
	n = abs(n-k)
	list.append(n)

print(min(list))
N, K = map(int,input().split())
if K == 1 or N == 0 or N == K:
    print(0)
    exit()
elif K == 0 or K > N:
    print(N)
    exit()
elif N == 1 or N == K-1 or K == N-1:
    print(1)
    exit()
elif N > K:
    ans = (N//K) + 1
    res = abs(N - (K*ans))
    print(res)
    exit()
N, K = map(int, input().split())
store = N
x = N % K
if N == 0:
    print(0)
elif N == store:
    print(abs(N - K))
else:
    while x < store:
        store = x
        x = abs(x - K)

    print(store)
N, K = map(int, input().split())
if N % K == 0:
    print(0)
else:
    print(min(abs(N-K), abs(abs(N-K)-K)))
import fractions
N, K = map(int,input().split())
N = fractions.gcd(N,K)
min = N
for i in range(10*18):
    N = abs(N-K)
    if min > N:
        min = N
    if N == 0:
        min = 0
        break
print(min)
n, k = map(int, input().split())

max_i = max(n, k)


if n>=k:
    n = n%k
if n==0 or n==1 or k==1:
    print(0)
else:
    for _ in range(max_i):
        tmp = abs(n-k)
        if tmp >= n:
            break
        else :
            n = tmp
    
    print(n)
N, K = 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:
    ans = (N//K) + 1
    res = abs(N - (K*ans))
    print(res)
n, k = map(int, input().strip().split())
case = 0
while (n-k)>0:
    n = abs(n-k)
    case = case+1
if case>=1:
    n = abs(n-k)
elif case==0:
    n = n
print(n)
N,K=map(int,input().split())
while(1):
	A=N%K
	if N<K:
		print(min(N,abs(N-K)))
		exit()
	elif A==0:
		print(0)
		exit()
	else:
		print(A)
		exit()
N,K=map(int,input().split())
if N==0 or N==1 or K==1:
  print(0)
  exit()
while True:
  diff=abs(K-N)
  if diff>=N:
    print(N)
    break
  else:
    N=diff
N, K = list(map(int, input().split()))
if N < K:
    print(N)
else:
    N = N % K
    if N <= 0:
        print(N)
    else:
        print(abs(N - K))
from sys import stdin
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y) #再帰定義
N,K=list(map(int,(stdin.readline().strip().split())))
print(gcd(N,K))
n,k=map(int,input().split())
x=abs(n-k)

if x==0:
    print(0)
elif x>k:
    print(x%k)
else:
    print(k%x)
N,K=list(map(int,input().split()))

if N==0:
    print(0)
cand0=abs(N-K)
cand4=abs(cand0-K)
N=N%K
cand1=abs(N-K)
cand2=abs(cand1-K)
cand3=abs(cand2-K)
print(min(N,cand0,cand4,cand1,cand2,cand3))
from math import ceil
N,K=map(int,input().split())
#print(int(N/K))
kari=N-(int(N/K)*K)
test=abs(N-(ceil(N/K)*K))
print(min(kari,test))
N, K = [int(i) for i in input().split(' ')]

def res():
  if N == K:
    return 0
  elif N > K:
    return N % K if K - (N % K) > (N % K) else K - (N % K)
  else :
    return N - K if N - K > (N-K) - K else (N-K) - K

print(res())
N, M = map(int,input().split())
if N == 0 or M == 1:
    print(0)
elif N*2 <= M:
    print(N)
else:
    a = N//M
    p = N - a
    print(min(p,abs(p-N)))
# coding:utf-8
import math
import fractions

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

if K==0:
    print("N")
elif N==0 or N%K==0:
    print("0")
else:
    a=fractions.gcd(N,K)
    N=N%K
    N=int(N/a)
    K=int(K/a)
    mi=N
    t=N
    for i in range(2):
        t=abs(t-K)
        mi=min(mi,t)
    print(mi*a)
N, K = map(int, input().split())
if N >= K:
    while N>0:
        N -= K
N = abs(N)
print(N)
N,K=map(int,input().split())

cnt = 0
ans = N

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

ans2 = N
ans1 = K % N
if K > N:
    ans2 = N - ( K % N )
    ans1 = N
else:
    ans2 = K - ( N % K )
    ans1 = N % K

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

if N%K==0:
    print(0)
else:
    for i in range(100000000):
        x = min(x,abs(x-K))
    print(x)
# C
n, k = map(int, input().split())

import math
x = math.floor(n/k)
ans = min( abs(n-(x-1)*k), abs(n-x*k), abs(n-(x+1)*k) )
print(ans)
n,k=map(int,input().split())
l=(n//k)*k
if l==0:
  l=k
while abs(n-l) < n:
  n = abs(n-l)
print(n)
data = input().split()
n = int(data[0])
k = int(data[1])

if n > k :
  res = k - n % k
else:
  res = min(abs(n - k), n)

print(res)
x = list(map(int,input().split(" ")))
n = x[0]
k = x[1]
import math
l = abs(n % k)
print(l)

if l > k//2:
    print(k-l)
else:
    print(l)
n,k = map(int,input().split())
if(k==1):
  print(0)
else:
  while(len(str(n))>1):
    n = n//10
  t = n//k
  for i in range(t):
    n = abs(n-k)
  if(n < abs(k-n)):
    print(n)
  else:
    print(abs(k-n))
n, k = map(int, input().split())

if n >= k:
    print(n - k*(n // k))
else :
    print(n)
import fractions
N, K = map(int,input().split())
N = fractions.gcd(N,K)
for i in range(10*6):
    N = abs(N-K)
    if N == 0:
        break
print(N)
N,K=map(int,input().split())
l=abs(N//K)
p=K*l
for i in range(1,10000):
	if abs(N-(K*(l+i)))<abs(N-p):
		p=p+1
print(abs(N-p))
n,k  = map(int,input().split())

if n>=k:
    if n%k != 1 and n%k != 2 and n%k != 0:
        print(abs(k-n%k))
    else:
        print(n%k)
elif n<k:
    if n == 0:
        print(0)
    else:
        if n%k != 1 and n%k != 2 and n%k != 0:
            print(abs(n-k%n))
        else:
            print(n)
n , k = map(int,input().split())

if(n%k == 0):
        
    g = n//k
        
    print(n - (g*k))
else:
    g = n//k
    ans = n - (g*k)
    print(abs(ans - k))
N, K = map(int, input().split())



y = abs(N - K)
N = y
y2 = abs(N - K)
c=1
dif = (y, y2)

if dif[c-1] <= dif[c]:
    print(dif[-1])
    
if dif[c-1] > K:

    dif += (dif[c-1]%K,)
    c += 1

while dif[c-1] > dif[c]:

    yy = abs(dif[c] - K)
    dif += (yy,)
    c += 1


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

if n % k ==  0:
    print(0)
    exit()
if n < k:
    ans = n
else:
    ans = k - (n % k)

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

if n < k:
	print(n if n else k)
else:
	if n % k == 0:
		print(0)
	else:
		print((k - n) % k)
import sys
import math

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

if N < K:
    print(N)
elif N == 0:
    print(0)
elif N == K:
    print(0)
else:
    a = math.floor(N / K)

    ans1 = abs(N - (K * a))
    ans2 = abs(N - (K * (a + 1)))
    ans3 = abs(N - K)
    ans4 = abs(N - (K * (a - 1)))

    print(min(ans1, ans2, ans3, ans4))
N, K = map(int, input().split())

a = int(N / K)

b = abs(int(N - K * a))
c = abs(int(b - K))

print(min(b,c))
import math

inputted = list(map(int, input().split()))
N = inputted[0]
K = inputted[1]

candidate1 = N - K * math.floor(N / K)
candidate2 = K - candidate1
answer = min(candidate1, candidate2)

print(answer)
X = list(map(int, input().split()))
N = X[0]
K = X[1]
if N % K == 0:
  print(0)
else:
  while True:
    A = abs(N - K)
    if N > A:
      N = A
    else:
      break
print(N)
def main():
    n, k = map(int, input().split())
    print(n % k)


if __name__ == '__main__':
    main()
n,k=map(int,input().split())
t=n%k
n=min(t,k)
print(n)
import math
N, K = map(int, input().split())
if N == K:
    ans = 0
elif N < K:
    ans = N
else:
    ansmin = N-K*math.ceil(N/K)
    ansmax = K-ansmin
    ans = min(abs(ansmin), abs(ansmax))
print(ans)
N,K=map(int,input().split())
import fractions
a=fractions.gcd(N,K)
if K==1:
  print(0)
else:
  print(a)
n,k = map(int,input().split())
N = n
if n % k == 0:
    print(0)
elif n > 100000000000:
    n -= 1000 *k
    while n > 0:
        n -= k
    if abs(n) > N:
        print(N)
    else:
        print(abs(n))
import math

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

x = N

if x < K:
  ans1 = abs(x - K)
  ans2 = abs(ans1 - K)
  ans3 = abs(ans2 - K)
  ans4 = abs(ans3 - K)
  print(min(ans1, ans2, ans3, ans4))
else:
  if x % K == 0:
    print(0)
  else:
    x = x - (K * math.floor(x / K))
    ans1 = abs(x - K)
    ans2 = abs(ans1 - K)
    ans3 = abs(ans2 - K)
    ans4 = abs(ans3 - K)
    print(min(ans1, ans2, ans3, ans4))
N, K=map(int, input().split())


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

else:
    nums=list()
    nums.append(N)
    while abs(N-K)<N:
        N=abs(N-K)
        nums.append(N)
    print(min(nums))
import fractions
N,K = list(map(int,input().split()))
if N%K == 0 or K%N == 0:
  ans = 0
  print(ans)
else: 
  ans = fractions.gcd(N,K)
  if N == 1 or K == 1:
    ans = 0
  print(ans)
N, K = map(int, input().split())

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

mod = N % K

print(min(mod, abs(mod + K)))
n, k = map(int, input().split())
modulo = n % k
ans = min(k - modulo, n)
print(ans)
N,K=list(map(int,input().split()))
if N>K:
    ans_1=N-K*int(N/K)
    ans_2=abs(N-K*int(N/K+1))
    print(min(ans_1,ans_2))

else:
    ans_1=N
    ans_2=abs(N-K)
    print(min(ans_1,ans_2))
n, k = map(int, input().split())

if k == 1:
  print(0) 
elif (n - k) % 2 == 1:
  print(1)
else:
  print(2)
n,x=map(int, input().split())
l=[]

if(x!=1 and n!=0):
  n = n - x * int(n/x)
  l.append(abs(n-x))
  l.append(abs(l[0]-x))
  print(min(l))
else:
  print(0)
n,m=map(int,input().split())
ans=(n % m)
ans=min(ans,abs(n-m))
print(ans)
nk = list(map(int, input().split()))
n = nk[0]
if nk[1] == 1:
  print(0)
elif n == 1:
  print(1)
elif nk[1] == 0:
  print(n)
elif n == 0:
  print(0)
else:
  for _ in nk:
    diff = abs(n - nk[1])
    if diff < n:
      n = diff
    else:
      break
  print(n)
a=list(map(int, input().split()))
n=a[0]
k=a[1]
def gcd(a,b):
    while b!=0:
        a,b = b,a%b
    return a
if n==1 or k==1:
    print(0)
    exit()
else:
    print(gcd(n,k))
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)


def main():
    n, k = map(int, input().split())
    if n < k:
        print(n)
        return

    a = n // k
    b = n - a*k
    c = abs(b - k)
    print(min(b, c))


main()
import fractions
import sys
N,K = map(int,input().split())
L = []
L.append(N)
i = 0
if N == 0:
    print(0)
    sys.exit(0)
if K == 1:
    print(0)
    sys.exit(0)
Q = 0
while True:
  L.append(abs(N-K))
  N = abs(N-K)
  i += 1
  if i > 10**5:
    Q = min(L)
    break
if N%2 == 0 and K % 2 == 0:
    print(min(2,N,Q))
else:
    print(min(abs(N-K),N,Q))
n, k = [int(i) for i in input().split()]

a = abs(n - k*int(n/k))
b = abs(n - k*(int(n/k)+1))
print(min(n, a, b))
nums = list(map(int, input().split()))
n = nums[0]
k = nums[1]

x  = n % k

print(min(n, k - n))
import sys
a,b=map(int,input().split())
i=1
z=[0]*2
saisyo=abs(a-b)
if a==0:
  print(a)
  sys.exit()
if a<b:
  if a<saisyo:
    saisyo=a
  print(saisyo)
  sys.exit()


  
u=a%b
w=(a-u)/b
z[0]=a-(w*b)
z[1]=abs(a-(w+1)*b)
r=int(min(z))


c=[saisyo,r]
print(min(c))
N, K = map(int,input().split())
if K == 1 or N == 0:
    print(0)
    exit()
elif K == 0:
    print(N)
    exit()
elif N == 1:
    print(1)
    exit()
else:
    l = []
    ans = N
    for i in range(min(N,K)):
        ans = abs(ans - K)
        l.append(ans)
    print(min(l))
import sys
input = sys.stdin.readline


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

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

if __name__ == "__main__":
    main()
import sys

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

tmp = N

if N >= K:
    if N % K == 0:
        print(0)
        sys.exit()
    else:
        K = N % K

while True:
    print(N)
    if abs(N - K) < tmp:
        N = abs(N - K)
    else:
        print(tmp)
        sys.exit()
    tmp = N
N,K=map(int, input().split())
if N>=1000000000000000000 or K>=1000000000000000000:
  print(0)
  exit(0)
while True:
  n=abs(N-K)
  if N <= n:
    break
  else:
    N = n
print(N)
import math
N,K= list(map(int,input().split()))
Num = 10*18
for i in range(N):
    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)
n, k = map(int, input().split())

if n < k and k - n > n :
  print(n)
elif n > k and (n- k) % 2 == 0:
  print(0)
elif n < k and (k - n ) % 2 == 0:
  print(0)
elif n > k and (n - k ) % 2 == 1:
  print(1)
else:
  print(1)
n, k = [int(i) for i in input().split()]

if n >= k:
    result = min(n % k, k - (n % k))
if n < k:
    result = n

print(result)
n,k=map(int,input().split())
if n==0:
    print(0)
else:
    if k%2==0:
        print(min(k//2-1,n))
    else:
        print(min(k//2,n))
# C
n, k = map(int, input().split())

import math
x = math.floor(n/k)

ans = 10**9
for i in range(-100, 101):
    if ans > abs(n-(x-i)*k):
        ans = abs(n-(x-i)*k)

print(ans)
N_s,M_s=input().split()
K=int(M_s)
N=int(N_s)

if N==0:
    print(N)
elif N>=K:
    #print("1")
    x1=N%K
    x2=abs(N%K-K)
    print(min(x1,x2))
else:
    #print("2")
    print(N)
n,x=map(int, input().split())
l=[]


i = n - x * int(n/x)
if(n != x):
  l.append(abs(i-x))
  l.append(abs(l[0]-x))
  print(min(l))
else:
  print(n)
n, k = map(int, input().split())
count = n // k
abs_val = abs(n-k)
ans = n - k * count

if abs(ans - abs_val) < ans:
    print(abs(ans - k))
else:
    print(ans)
n, k = map(int, input().split())
 
if n // k == 0:
    print(n)
else:
    print(min(n // k, n % k))
[n, k] = map(int, input().split())
 
n -= n//k
if n > k//2:
  n = k -n
print(n)
from fractions import gcd

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

g = gcd(N, K)
ans = 0 if g == K else g
print(ans)
N,K=(int(x) for x in input().split())
x=N

if(x > K):
    t=N%K
    x=min(t,K-t)

print(x)
def minValue():
    n, k = list(map(int, input().split()))

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

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


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

sub = int(N / K)
val1 = N - K * sub
val2 = K * (sub+1) - N

if val1 >= val2:
    print(val2)
else:
    print(val1)
n,k= map(int,input().split())
if n % k == 0:
    print("0")
elif n < k:
    print(n % k)
else:
    print(abs(n % k - k))
import fractions
N, K = map(int,input().split())
N = fractions.gcd(N,K)
min = N
for i in range(10**7):
    N = abs(N-K)
    if min > N:
        min = N
    if N == 0:
        min = 0
        break
print(min)
n,k=map(int,input().split())
if k >= 2*n:
	print(n)
elif k == n:
	print(0)
elif k == 1:
	print(0)
elif n == 0:
	print(0)
else:
	print(1)
N, K = map(int,input().split())

m = N//K

a = N
b = abs(a-K*m)
while b < a:
       a = min(a,b)
       b = abs(a-K)
        
print(a)
import math
n, k = [int(i) for i in input().split()]

m = math.floor(n/k)

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