peterverebics/CodeNet_Python_118 huggingface dataset:
n, k = map(int, input().split())

if n == 0 or k == 0:
    print(max(n, k))
    exit()
if n < k:
    ans = n
elif n % k == 0:
    ans = 0
else:
    ans = k % (n % k)

print(ans)
N, K = map(int, input().split())
c = 0
 
while 1:
    if (N%2==0)and(K%2==0)and(N>=K):
        a = 0
        break
    if K==1:
        a = 0
        break
    if N==0:
        a = K
        break
    a = N
    N = abs(N-K)
 
    if c==0:
        c += 1
        continue
    if a<=N:
        break
print(a)
# -*- coding: utf-8 -*-
import math

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

#A = max(N,K)
#B = min(N,K)

#sre = (1/(4*M))*sum(A)

def hoge(K):
    init_i = math.floor(N/K)

    p1 = abs(N - (init_i-1) * K)
    p = abs(N - init_i * K)
    m = abs((init_i+1) * K - N)

    return min(p1,p,m)

ks = []
tmp_K = K

while(1):
    tmp_K = hoge(tmp_K)
    ks.append(tmp_K)
    if tmp_K == 0 or tmp_K in ks:
        break

print(min(ks))
import sys
import math

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

if k == 1:
    print(0)
    sys.exit()

n_start = math.floor(n / k)
# print(n_start)
n = n - n_start*k

# print(n)


while True:
    min_n = n
    n = abs(n-k)
    # print("n, k =", n, k)
    if min_n > n:
        min_n = n
    else:
        break
print(min_n)
n, k = map(int, input().split())

ans = n % k
s = n // 2
if s >= ans:
    print(ans)
else:
    print(abs(ans - s))
N,K=map(int,input().split())
kari=int(N/K)
M=N-K*(kari)
L=abs(M-K)
B=abs(L-K)
print(min(N,abs(M-K),L,B,abs(M)))
# C
import math
def my_round(val, digit=0):
    p = 10 ** digit
    return (val * p * 2 + 1) // 2 / p

n, k = map(int, input().split())
s = my_round(n / k)
print(abs(n - int(s * k)))
N,K = map(int,input().split())

def replace(N,K):
    if(N<K):
        return N
    else:
        a = N%K
        if (abs(a-K)<K and abs(a-K)<a):
            a = abs(a-K)
    return a

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

a,b = n,k
while b>0:
    a,b = b,a%b

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

if n == 0:
    print(k)
elif n % k == 0:
    print(0)
elif n < k:
    print(n)
else:
    rem = n % k
    if abs(rem-k) < rem:
        print(abs(rem-k))
    else:
        print(rem)
n, k = map(int,input().split())
print(min(abs(n - k * int(n/k)), abs(n - k * (int(n/k) + 1))))
import itertools
import math
import fractions

n,k = map(int,input().split())
ans = n
if k == 1 or n == 0 or n==k :
    print("0")
    exit()

if n > k:
    ans = n%k



while(1):
    
    ans = abs(ans-k)
    if ans == n:
        break
    if ans == 1 or 0:
        break
print(ans)
N, K = list(map(int, input().split()))


def x(N, K):
    y = (N)
    f = 0
    while True:
        N = abs(N - K)

        if y > N:
            y = N

        if y == N:
            f += 1

        if f > 2:
            return y


# if K == 0:
#     print(N)
# elif K == 1:
#     print(0)
# else:
#     print(x(N, K))
print(x(N, K))
n, k = map(int, input().split())
if n==k or k==1:
  print(0)
else:
  if abs(n-k)<k:
    print(abs(abs(n-k)-(k)))
  else:
    print(abs(n-k)%k)
n, k = map(int, input().split())
a = n % k
b = a - (n + a) % k
if a < b:
    print(a)
else:
    print(b)
n, k = list(map(int, input().split()))
if k == 1:
  print(0)
else:
  print( abs( (n%k) -1 ) )
n, k = list(map(int, input().split(' ')))
result = 0
if k < n:
    result = n % k
elif n < k:
    result = min(n, k-n)
print(result)
N, K = map(int, input().split())

if N % K == 0:
  print(0)
elif N % 2 != 0 or K % 2 != 0:
  print(1)
else:
  print(2)
n, k = map(int, input().split())
buff = n

while True:
  n -= k
  if n <= 0:
    n = abs(n)
    break

print(n)
N,K = map(int,input().split())
n = abs(N-K)
if(N == K):
    print(0)
if(K>N):
    if(n<N):
        print(n)
    else:
        print(N)
if(K<N):
    mod = N % K
    print(mod)
n, k = map(int,input().split())
ans = 0
if n < k:
    ans = n
else:
    ans = min(n%k,k-n%k)

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

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

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

if k == 1:
    print(0)
    sys.exit()

pre=n-math.floor(n/k)*k

while 1:
    if pre <= abs(pre-k):
        print(pre)
        sys.exit()
    pre = abs(pre - k)
n,k=map(int,input().split())
a=b=abs(n-k)
ans=n
if n%k==0:
 print("0")
 exit()
while a!=abs(b-k):
 b=abs(b-k)
 if b<ans:
  ans=b
print(ans)
n, k = map(int,input().split())
array = []
if k == 1:
    array.append(0)
else:
    for i in range(0,3):
        n = abs(n-k)
        array.append(n)
min_n = min(array)
print(min_n)
N, K = map(int, input().split())

cnt = int(N / K) + 1

print(min(abs(N - (K * cnt)), abs(N - (K * (cnt-1))), N))
n,k=map(int,input().split())

if k==1:
    print(0)
elif n>k:
    print(k-(n%k))
else:
    print(n)
n, k = list(map(int, input().split()))
x = n % k
print(min(x, x-k))
n,k = list(map(int,(input().split())))

if n<k :
  print(n)
elif n>=k:
  x = n//k
  ans = n-(x*k)
  ans_x = abs(n-(k*(x+1)))
  if ans >= ans_x:
    print(ans_x)
  else:
    print(ans)

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

while True:
	buff = n-k
	if buff <= 0:
		if buff < n:
			print(n)
		else:
			print(abs(buff))
		break
	n = buff
N, K = [int(i) for i in input().split()]

n = N
c = True
while(c):
    if n > K:
        # n - abs(n - K) => n - (n - K) => K > 0
        n = n - int(n / K) * K
    elif n < K:
        # n - abs(n - K) => n - (K - n) => 2n - K
        if 2 * n - K > 0:
            n = K - n
        else:
            c = False
    else:
        # n == K で n - abs(n - K) => n（変更後も同じ）
        c = False
print(n)
n,k=map(int, input().split())
val=n%k
num=abs(n-k)
if num==0:
    print(0)
else:
    val1=min(num%k,k%num)
    print(min(val,val1))
N,K = map(int,input().split())
if (N//K)==0:
  print(N)
else:
  x=N-K*(N//K)
  print(min(x,abs(x-K)))
n,k = map(int,input().split())

def gcd(a,b):
    b,a = sorted([a,b])
    while(b!=0):
        save = b
        b = a%b
        a = save
    return a


if(n%k==0):
    print(0)
else:
    print(gcd(n,k))
n,k=map(int,input().split())
t = int(n/k)
tt = t + 1
ans=min([n - t*k,tt*k - n])
print(str(ans))
import sys
n, k = list(map(int, input().split()))

if (k == 1):
    print(0)
    sys.exit()

s, a = divmod(n, k)

ans = 0
if (a == 0):
    ans = s
else:
    ans = a

print(ans)
a,b = map(int, input().split())
if abs(a-b) > a:
    print(a)
else:
    if a%b == 0:
        print(0)
    else:
        print(a//b)
l = input().split()
l_int = [int(a) for a in l]
N = l_int[0]
K = l_int[1]

if K > N:
    print(N)
else:
    print(N % K)
N,K = map(int, input().split())
ans = 0
# if N > K and K == 1:
#     print(0)
#     exit(0)
if N < abs(N - K):
    ans = 0
elif N % K == 0:
    ans = 0
else:
    ans = abs(N % K - K)
# while N > abs(N - K):
#     N = abs(N - K)


print(ans)
N,K = map(int,input().split())
i = 0
if K == 1:
    print(0)
else:
    results = [N]
    while i < K:
        N = N - K
        if N < 0:
            results.append(-N)
            N = -N
        else:
            results.append(N)
        i += 1
    print(min(results))
n, k = map(int, input().split())

if n < abs(n - k):
    print(n)
elif n % k == 0:
    print(0)
elif n % k != 0:
    print(1)
n,k = map(int,input().split())
if n == n%k:
    print(n)
else:
    n = n%k
    print(min(n,abs(n-k)))
N,K = map(int, raw_input().split())

d = int(N/K)
if d == 0:
    print N
else:
    p = [abs(N - K*(d-1)), abs(N - K*d), abs(N - K*(d+1))]
    print min(p)
n, k = map(int, input().split())

if k == 1:
    print(0)
    exit()
    
def gcd(a,b):
  while b!=0:
    a,b=b,a%b
  return a

tmp = gcd(n, k)
low = 10**19

while True:
    tmp = abs(tmp-k)
    if tmp < low:
        low = tmp
        n = tmp
    else: break

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

if n % k == 0:
    print(0)
else:
    while n > abs(n-k):
        n = abs(n-k)
print(n)
n, k = [int(i) for i in input().split()]
import fractions

if k == 1:
  print(0)
else:
  print(fractions.gcd(n, k))
n,k = map(int,input().split())
l = n%k

if k !=1 and l == 0:
    if l >= abs(l-k):
        print(abs(l-k))
    else:
        print(l)
else:
    print(0)
str = input().split()
a1 = int(str[0])
a2 = int(str[1])
if a1 >= a2:
    a1 = a2%a1
if a1 < a2-a1:
    print(a1)
else:
    print(a2-a1)
N, K = map(int, input().split())

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

elif N % K != 0:
    print(1)

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

else:
    print(2)
N,K=map(int,input().split())
i = 1
lis = []
if K==1:
    print(0)
while True:
    N = abs(N-K)
    lis.append(N)
    if N < 10:
        break
print(min(lis))
n, k = map(int, input().split())
result = 0

while True:
    m = abs(n - k)
    if k == 1:
        break
    elif n == 0:
        result = m
        break
    elif n <= k:
        result = min(n, m)
        break
    else:
        n = n % k

print(result)
import math
N,K = map(int, input().split())

print(min(abs(N-K*int(N/K)),abs(N-K*math.ceil(N/K)),min(abs(N-(1+K*int(N/K))),abs(N-K*(1+math.ceil(N/K))),N)))
n, k = list(map(int, input().split()))

while(True):
    x = abs(n-k)
    print(n,k,x)
    if n < x:
        print(n)
        break
    n = x
N, K = map(int, input().split())
a = abs(N-K)
print(min(a, abs(a-K)))
n,k=map(int, input().split())
l=[]

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

r = n%k
w = r%k
print(min(r,w))
n,k = map(int,input().split())
left = 0
right = 10**19

while right - left >1:
  middle = (right + left)//2
  if middle < abs(middle - k) :
    left = middle
  else:
    right = middle
if n == 0:
  print(0)
elif k//n==2:
  print(n)
elif n % k == 0:
  print(0)
else:
  print(min(left,abs(n-k),n))
N, K = map(int, input().split())
if N >= K:
    c = N - (int(N/K)-1)*K
    print(min(c, abs(c-K)))
else:
    print(N)
N,K = map(int,input().split())
import fractions

if N==0:
    print(K)
elif K == 1:
    print(0)
elif N*2 == K or N == K*2:
    print(0)
else:
    print(fractions.gcd(N,K))
num_list = [int(i) for i in input().split()]
min_list = []

if num_list[0] < num_list[1]:
    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))
a,b=input().split()
a=int(a)
b=int(b)
x=b-a
if x==0:
    f=0
    print(0)
elif x<0:
    a=x*(-1)
    y=b-a
    if y==0:
        f=0
    elif y<x:
        f=y
    else:
        f=x
    print(f)
else:
    a=x
    y=b-a
    if y==0:
        f=0
    elif y<x:
        f=y
    else:
        f=x
    print(f)
N, K = map(int, input().split())

if K == 0:
  print(N)
else:
  if N >= K:
    print(N % K)
  else:
    keisan = abs(N - K)
    if keisan > N:
      print(N)
    else:
      print(keisan)
#C
N, K = map(int, input().split())
ans = N

if N <= abs(N-K):
    pass
else:
    for i in range(N+1):
        try:
            if ((N-i) / K).is_integer():
                ans = i
                break
            elif ((N+i) / K).is_integer():
                ans = i
                break
        except:
            break

print(ans)
n,k=map(int,input().split())
a=n%k
if n<k:
    print(n)
    exit()

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



while True:
    if n < k:
        print(n)
        break
    n = n % k
    if n == 0:
        print(0)
        break

    ans = n - k
    next_ans = abs(ans) - k
    if abs(next_ans) > abs(ans):
        print(abs(ans))
        break
    else:
        print(abs(next_ans))
        break
n,k=map(int,input().split())
while n>=0:
  n=abs(n-k)
  if n-k<=0:
    print(abs(n-1))
    break
# 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(100):
        t=abs(t-K)
        mi=min(mi,t)
    print(mi*a)
N, K = map( int, input().split() )

# ユークリッドの互除法
# a = b * q + r 
# b = b'* r + r' ...

a = N
b = K
if a < b:
  r = b - a
else:
  r = a % b
ret = 0
while r != 0:
  a = b
  b = r
  r = a % b
  if r == 0:
    break
  ret = r
print( ret )
n,k=map(int,input().split())
if n<=k:
    print(n)
elif k==0:
    print(n)
elif n%k==0:
    print(n)
else:
    t=n//k
    s=(t+1)*k-n
    r=n-t*k
    print(min(r,s))
n,k = map(int,input().split())
divflg = True
flg = True

while divflg:
    if n % 10 == 0:
        n = int(n/10)
    else:
        divflg = False

while flg:
    div = n // k
    mod = n % k
    if div == 0:
        flg = False
        diff = abs(mod-k)
    else:
        n = mod

print(min(n,diff))
from fractions import gcd

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

if n == 1 or k == 1:
    print(0)
else:
    print(gcd(n,k))
N,K = map(int,input().split())

def replace(N,K):
    if(K == 1 or N==K):
        return 0
    if(N<K):
        return N
    else:
        N = N-(K*(N//K))   
        # while(N-K>0):
        if (N-K<=0 and abs(N-K)<K):
            N = abs(N-K)
                       
            
        return N
print(replace(N,K))
n,k=map(int,input().split())
t = int(n/k)
tt = t + 1
a = n - t*k
b = tt*k - n
ans=min([a,b])
print(str(ans))
def abstract(a, b):
    return abs(a - b)

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

A = N
min_temp = A

if N == 1 or K == 1:
    print(0)
elif N == 0:
    print(K)
elif N == 2 or K == 2:
    print(2)
else:
    while True:
        min_temp = abstract(A, K)
        if A < min_temp:
            break
        elif A == min_temp:
            break
        A = min_temp
    print(A)
N,K=list(map(int,input().split()))

if N==0:
    print(0)

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

print(N)
n,k=map(int,input().split())
print(min(n%k,k-n))
# -*- coding: utf-8 -*-

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

bef=-1
x =0
count = 0
Bool=True
if N%K < K:
	bef=N%K
	Bool=False
while Bool:
	if count>0:
		N=bef
	x=abs(N-K)
	count+=1
	if bef>=0 and bef<=x:
		break
	bef=x

print(bef)
N,K=map(int,input().split())
if N<=K:
    print(N)
else:
    A=N%K
    ans=min(A,K-A)
    print(ans)
n,k = map(int,input().split())
a = 10**19
if n >= k:
    a=n//k
b=n%k
c=abs(b-k)
l=[n,a,b,c]
print(min(l))
n, k = map(int, input().split())
t = n % k
if t < k:
    print(t)
else:
    print(k)
n,k=map(int,input().split())

if k==1:
    print(0)
elif n>k:
    print(k-(n%k))
else:
    print(k-(n%k))
import math
N, K = map(int, input().split())

var = N % K
if var == 0 or N == 0:
        print(0)
elif var == K-1 or var == 1:
        print(1)
else:
        ans = N
        while ans != N:
                ans = N
                N = N // K
        print(ans)
n,k=map(int,input().split())
while n>0:
  n=n-k
print(abs(n))
n,k = list(map(int,input().split()))

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

if N % K == 0:
    result = 0
elif N % K != 0 and N // K == 0:
    result = N
else:
    while N < 0:
        N = N - K
    result = abs(N)

print(abs(result))
import math
n,k = map(int,input().split())
     
m = int(n/k)
n = n-m*k
     
print(min(n,k-n))
N, K = map(int, input().split())

if N == K:
    print(0)
elif N < K:
    print(N)
elif N > K:
    if N % K == 0:
        cnt = (N // K)
    else:
        cnt = (N // K) + 1
    print(cnt*K - N)
N, K = list(map(int, input().strip().split()))

def gcd(a,b):
  a, b = max(a, b), min(a, b)
  while b!=0:
    a,b=b,a%b
  return a

if N < K:
  print(N)
elif N == K:
  print(0)
else:
  ans = gcd(N, K)
  if ans == K:
    print(0)
  else:
    print(ans)
from fractions import gcd


def main():
    n, k = map(int, input().split())
    if k == 1 or n == 0 or n == k:
        print(0)
    elif n <= k:
        print(n)
    elif n % k == 0:
        print(0)
    else:
        print(gcd(n, k))


if __name__ == '__main__':
    main()
N, K = map(int, input().split())
a = abs(N-K)
b = abs(a-K)
print(b)
N,K = map(int,input().split())
ans = 100000
for i in range(1000000):
    k = abs(N-K)
    if ans > k:
        N = k
        ans = k
if N != 1 and K != 1:
    print(ans)
else:
    print(0)
N, K = map(int, input().split())

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

b = math.floor(n/k)

min = abs(n-(b*k))
for i in range(10**5):
    n = abs(n-k)
    if min > n:
        min = n

print(min)
N,K=map(int,input().split())
A=[]
for i in range(1000):
    N=abs(N-K)
    A.append(N)
    if N==0 or N==1 or N==2:
        break
print(min(A))
#import math
N, K = list(map(int, input().strip().split()))

def gcd(a,b):
  a, b = max(a, b), min(a, b)
  while b!=0:
    a,b=b,a%b
  return a

if N < K:
  print(min(N, abs(N-K)))
elif N == K:
  print(0)
else:
  ans = gcd(N, K)
  if ans == K:
    print(0)
  else:
    print(ans)
def main():
  n, k = map(int, input().split())
  n %= k
  if abs(n - k) < n:
    print(abs(n-k))
  else:
    print(n)
n, k = map(int, input().split())
if n%k == 0:
    print(0)
elif n%2 == 0 and k%2 == 0:
    print(2)
else:
    tmp = n % k
    print(min(tmp, k-tmp))
n, k = map(int, input().split())

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

print(ans)
N, K = map(int, input().split())
max1 = max(N, K)
min1 = min(N, K)

if min1 == 0:
    print(max1)
else:
    a = int(max1/min1)
    max1 -= a*min1
    print(max1)
N,K=map(int,input().split())
kari=int(N/K)
N=N-K*kari
print(min(N,abs(N-K)))
N,K = map(int, input().split())

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

x = N % K
x_before = x
sa = x
for i in range(sa):
  x = abs(x-K)
  if x >= x_before:
    print(x_before)
    exit()
  x_before = x
n,k = map(int,input().split())

if abs(n - abs(n-k)) == 1:
    print(0)
elif abs(n - abs(n-k)) == 2:
    if n % 2 == 0:
        print(0)
    else:
        print(1)

else:
    while n > abs(n-k):
        if n > abs(n-k):
            n = abs(n-k)
    print(n)
n, k = map(int, input().split())

if n > k:
  n = n - k*int(n/k)
  n_ = abs(n-k)
  print(min([n, n_]))
elif n < k:
  n_ = abs(n-k)
  print(min([n, n_]))
else:
  print(0)
import math


def main():
    n, k = map(int, input().split())
    print(math.gcd(n, k))


if __name__ == '__main__':
    main()
N, K = map(int, input().split())

if N % K == 0:
    print(N)
elif abs(N - K) >= N:
    print(0)
else:
    print(1)
if __name__ == '__main__': 
    n, k = [int(i) for i in input().split(" ")]
    while abs(n - k) < n:
        if k == 1:
            n = 0
        if n%k==0:
            n = int(n/k)
        else:
            n = abs(n - k)
    print(n)
from fractions import gcd
n,k=map(int,input().split())
if k==1:
    print(0)
else:
    print(gcd(n,k))
n,k = map(int, input().split())
if n//k>=10000:
    n=0
dif = abs(n-k)
while dif<n:
  n = dif
  dif = abs(n-k)
print(n)
I=lambda:list(map(int,input().split())) 
n,k=I()
print(min(n,n%k,abs(n-k)))
N, K = map(int, input().split())

if N == K:
    print(0)
elif N < K:
    print(N)
elif N > K:
    cnt = (N // K)
    print(cnt*K - N)
N, K = map(int, input().split())

def act(int1, int2):
    return abs(int1-int2)

if N < K:
    pass
elif N == K:
    N = 0
elif N%K == 0:
    N = 0
else:
    while N >= K:
        N = act(N, K)

print(N)
N,K = [int(i) for i in input().split()]
print(N%K)
nums = [int(e) for e in input().split()]
Min=nums[0]
y=nums[1]
t=True
while t:
  if(abs(Min -y)<Min):
    Min = Min-y
  elif(abs(Min - y) > Min):
    break
print(Min)
N,K = map(int,input().split())
if(N >K):
  print(N)
elif(N % K == 0):
  print(0)
else:
  print(abs(N % K - K))
N, K = [int(element) for element in input().split(" ")]

if K == 0:
    print(N)

elif N < K:
    print(N)
    
else:
    if (N % K) == 0:
        print(abs(N % K))
    else:
        print(abs((N % K) - K))
N,K = map(int, input().split())
if N < K:
    print(N)
else:
    mod = N%K
    print(min(mod, K - mod))
n,k = map(int, input().split())
if k==1:
    print(1)
else:
    d = [n]
    x = abs(n-k)
    while(x not in d):
        d.append(x)
        x = abs(x-k)
    print(min(d))
n,k=map(int,input().split())
a=n%k
if n<k:
    print(n)
    exit()

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


while True:
    if n < k:
        print(n)
        break

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

    n = n % k
    print(n)
    break
n, k = list(map(int, input().split()))
# print(n, k)


min_result = n
flag = 0
dic = {}

if n > k:
    z = n // k
    zz = n //z
    result = zz
else:
    result = n

while flag==0:
    result = abs(result - k) 
    # print('result',result)
    # print('min_result',min_result)
    if result <= min_result:
        min_result = result
        dic[result] = dic.get(result, 0) + 1

        if dic[result] > 1:
            flag=1
print(min_result)
n, k = map(int, input().split())

def gcd(a, b):
    if a % b == 0:
        return b
    return gcd(b, a % b)

cnt = gcd(n, k)

if n == 1 or k == 1:
    print(0)
else:
    print(cnt)
a,b=input().split()
a=int(a)
b=int(b)
y=[10**18]
f=0
for i in range (10*18):
    x=a-b
    if x<0:
        y.append(x*(-1))
        a=x*(-1)
    else :
        y.append(x)
        a=x
    if y[i]<=y[i+1]:
        f=y[i]
        break

print(f)
N, K = map(int, input().split())
Nj = N
while (Nj == K) is False:
    Ni = Nj
    Nj = abs(Ni - K)
    if Nj > Ni:
        print(Ni)
        break
    else:
        continue
if (Nj == 0) or (Nj == K) or (K == 1):
    print(0)
#スペース区切りの整数の入力
N, K = map(int, input().split())

aa = int(N/K)
ans = N - K*aa
ans_2 = ans - K

if(N<K):
  print(N)
elif(N%K == 0):
  print("0")
elif(ans<ans_2):
  print(ans)
else:
  print(abs(ans_2))
import sys
import math

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

pre=n-math.floor(n/k)*k

while 1:
    if pre <= abs(pre-k):
        print(pre)
        sys.exit()
    pre = abs(pre - k)
N,K = map(int,input().split())
n = int((N/K + 0.5)//1)

print(min(abs(n*K-N),abs((n+1)*K-N),abs((n-1)*K-N),abs((n-2)*K-N),abs((n+2)*K-N)))
min1=0
min2=0
def amb(n,k):
    min1=abs(n%k)
    min2=abs(min1-k)
    if min1==0 or min2==0:
        print(0)
    elif min1==min2:
        print(min1)
    elif min1>min2:
        print(min2)
    elif min1<min2:
        print(min1)

n,k=list(map(int,input().split()))
if n<k:
    print(n)
elif n==k:
    print(0)
else:
    amb(n,k)
def main():
    n, k = map(int, input().split())

    if n % k == 0:
        ans = 0
    else:
        ans = min(abs(n-k), abs(abs(n-k)-k))

    print(ans)


if __name__ == "__main__":
    main()
def solve():
    N, M = map(int, input().split())
    a = abs(N-M)
    
    if a == 0:
        return 0
    
    if M == 1:
        return 0
        
    n = N
    while n > abs(n-M) and n != 0:
        n = min(n, abs(n-M))
    
    return min(n, abs(n-a))

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

initial = N - (K * int(N / K))
if abs(initial - K) < initial:
    print(abs(initial - K))
else:
    print(initial)
import sys
n, k = map(int, input().split())

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

if n >= k:
  n = n % k

for i in range(10**18):
  if abs(k-n) < n:
    n = abs(k-n)
  else:
    break

print(n)
def min(a, b):
    if a >= b:
        res = a - b
    elif a < b:
        res = b - a
    return res

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

min_num = float('inf')


print(min_num)
import sys

input = sys.stdin.readline

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

if N < K:
    print(N)
else:
    if N % K == 0:
        print(0)
    else:
        print(K - N%K)
N,K=map(int,input().split())
B=int(N/K)
A=N-B*K
A=min(abs(A-K),abs(A+K),A)
print(A)
N,K=map(int,input().split())
if N%K==0:
    print(0)
    exit()
if N<K:
    print(N)
    exit()
ans=N%K
if ans>abs(ans-K):
    print(abs(ans-K))
else:
    print(ans)
n,k = map(int,input().split())
divflg = True
flg = True

while divflg:
    if n % 10 == 0:
        n = int(n/10)
    else:
        divflg = False

while flg:
    tmp = abs(n-k)
    n = tmp
    tmp2 = abs(n-k)
    if tmp2 > tmp:
        flg = False
    
print(tmp)
n, k = map(int, input().split())

n = str(n)

if(len(n) > 1):
    n = int(n[0]+n[1])
else:
    n = int(n)
    
if k == 1:
    print(0)
    exit()

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

print(n)