peterverebics/CodeNet_Python_118 huggingface dataset:
N,K = map(int,input().split())
A = N % K;
if A > K/2:
    print(K-A)
else:
    print(A)
n,k=[int(_) for _ in input().split()]
d=n//k
print(min(abs(n-k*d), abs(n-k*(d+1))))
N,K=map(int,input().split())
a=N%K
b=abs(a-K)
print(min(a,b))
import sys
input = sys.stdin.readline


def inpl():
    return list(map(int, input().split()))


N, K = inpl()

if N == 0:
    print(0)
else:
    print(min(N % K, K - N % K))
n,k = map(int, input().split())
print(min(n%k, k-n%k))
inte, sub = map(int, input().split(" "))
inte = inte % sub
t = [inte]
count = 0
while True:
    ab = abs(inte - sub)
    t.append(ab)
    if t[count] <= t[count + 1]:
        print(t[count])
        break
    else:
        count += 1
#coding:utf-8
N,K=map(int,input().split())

if (N%K) <=(K/2):
	x=N%K
else:
	x=K-N%K
	
print(x)
import sys
# input = sys.stdin.buffer.readline
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getlist():
    return list(map(int, input().split()))
def getlist2():
    return [int(x) * 100000 for x in input().split()]
import math
import bisect
import heapq
from decimal import Decimal
# from collections import defaultdict, Counter, deque
MOD = 10**9 + 7
INF = 10**15
def main():
    n,k = getlist()
    mo = n % k
    print(min(mo, abs(mo - k)))

if __name__ == '__main__':
    main()

"""
9999
3

2916
"""
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))
N,K = map(int, input().split())
A = N % K
print(min(abs(A-K),A))
n, k = map(int, input().split())

if 2*n <= k:
    print(n)

elif n < k:
    print(k-n)

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

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

if N % K  == 0:
    print("0")
else:
    val = N % K
    if val < abs(val-K):
        print(val)
    else:
        print(abs(val-K))
N, K = map(int, input().split())

L = []



for i in range(10**5):
    M = N // K
    if i == 0:
        N = abs(N - K)
        L.append(N)
    else:
        if N > K:
            N = N - (K * M)
            L.append(N)
        else:
            N = abs(L[i-1] - K)
            L.append(N)


print(min(L))
n, k=[int(w) for w in input().strip().split()]

rem = n%k
res = min(rem, abs(k-rem))
print(res)
N, K = map(int, input().split())
min = N % K
ans = 0
if min < abs(min - K):
    ans = min
else:
    ans = abs(min - K)
print(ans)
N, K = [int(i) for i in input().split()]

ans = N - (N // K) * K

print(min(ans, abs(K - ans)))
[N, K]  = list(map(int, input().split(' ')))

N = N % K
nN = abs(N - K)
while(nN < N):
    N = nN
    nN = abs(N-K)

print(N)
N,K = map(int, input().split(sep=" "))
x = N%K
if x < K-x:
    print(x)
else:
    print(K-x)
n, k = map(int, input().split())
n %= k
print(min(n, abs(k - n)))
import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
N, K = [int(x) for x in input().strip().split()]
if N == 0 or K == 1:
    print(0)
    exit()
if N >= K and (N - K) >= K:
    N -= (N - K) // K * K
if N >= K:
    N = abs(N-K)

if N >= (K / 2):
    print(K - N)
    exit()
print(N)
N, K = map(int, input().split())
mod = N % K
print(min(mod, abs(mod - K)))
# coding: utf-8

n, k = map(int, input().split())
t = n % k
if t > k-t:
    print(k-t)
else:
    print(t)
N, K = map(int, input().split())
 
q = N // K
Na = N - q*K
Nb = abs(Na - K)
 
if Na >= Nb:
  	print(Nb)
else:
  	print(Na)
import sys
n,k = map(int,input().split())
if n%k < abs((n%k) - k):
    print(n%k)
    sys.exit()
print(abs((n%k) - k))
n, k = map(int, input().split())

r = n % k
d = abs(k - r)

ans = r
if r > d:
    ans = d
print(ans)
N,K = map(int,input().split())
H = 0
while True:
  if (N > K):
    H = N%K
  else:
    H = K-N
  if (H>N):
    break
  else:
    N = H
print(N)
N,K=map(int,input().split())
a=N%K
print(min(a,abs(a-K)))
#n=int(input())
n,k=map(int,input().split())
#al=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]

print(min(n%k,k-n%k))
import math

if __name__ == '__main__':
    s = input().rstrip().split(' ')
    N,K = int(s[0]), int(s[1])
    if(N>=K):
        #cand1 = int(abs(N - int(K*math.ceil(N/float(K)))))
        #cand2 = int(abs(N - int(K*math.floor(N/float(K)))))
        cand1 = int(N%K)
        cand2 = int(K-(N%K))
        print(min(cand1, cand2))
    else:
        print(min(int(abs(N-K)),N))
n,k = map(int,input().split())
print(min(n,n%k+k,n%k,abs(n%k-k),abs(n%k-k-k)))
n,k=map(int, input().split())
am = n % k
print(min(am,(k-am)))
N, K = map(int, input().split())
q, mod = divmod(N, K)
if mod*2 > K:
    print(abs(mod-K))
else:
    print(mod)
N,K=map(int,input().split())
print(min(N%K,K-N%K))
n,k=map(int,input().split())
count=10**18 
if abs(n)>abs(k):
  a=n//k
  n=abs(n)-a*k
  
  
for i in range(10*18):
  n=abs(n-k)
  if count>n:
    count=n
  if count==0:
    print(0)
    exit()
    
print(count)
n, k = map(int, input().split())
if n>k:
    n=n%k
if n > k/2:
    n=k-n
print(n)
n,m = map(int,input().split())
syo,amari = divmod(n,m)
ans = m-amari
if ans >= n:
  ans = n
if amari == 0:
  ans = 0
print(ans)
N, K = list(map(int, input().split()))

print(min(N, abs(N%K), abs(N%K-K)))
n, k = map(int, input().split())
tmp1 = n % k
tmp2 = max(k, tmp1) - min(k, tmp1)
ans = min(tmp2, k - tmp2)
print(ans)
def resolve():
    N,K=map(int,input().split())
    val=N%K
    print(min(val,abs(val-K)))


resolve()
############ ---- USER DEFINED INPUT FUNCTIONS ---- ############
def inp():
    return(int(input().rstrip()))
def inlt():
    return(list(map(int,input().rstrip().split())))
def insr():
    s = input().rstrip()
    return(s[:len(s) - 1])
def invr():
    return(map(int,input().rstrip().split()))
################################################################
n,k = invr()
if k >= 2*n:
    print(n)
elif k >= n:
    print(k - n)
else:
    need = n//k
    remain = n%k
    first = need *k
    print(min(n - first,abs(n - first - k)))
#print()
#input
N, K = map(int, input().split())

#output
r = N//K
print(min(abs(N - K*(r+1)), abs(N-K*r)))
N,K = map(int, input().split())
a = N%K
print(min(a,abs(a-K)))
n, k = map(int, input().split())
print(min(n, abs(n-(n//k)*k), abs(n-(n//k+1)*k)))
#import numpy as np


def solve():
    m = n
    if m > k:
        m = m % k
    ans = min(m, abs(k-m))
    print(ans)



n, k = [int(i) for i in input().split(" ")]
solve()
n, k = map(int, input().split())
if n <= k:
  print(min(k-n, n))
else:
  print(min(n%k, abs(k-n%k)))
# -*- coding: utf-8 -*-

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

remainder = N % K

if remainder > K / 2:
  print(abs(remainder - K))
else:
  print(remainder)
n,k=map(int,input().split())
a=n
b=(n//k)
c=n-k*b
d=c-k

  
if abs(c)>abs(d):
    print(str(abs(d)))
else:
    print(str(abs(c)))
N,M = list(map(int,input().split()))

x = min(N%M, M-N%M)
print(x)
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
if n>=k:
    x=n//k
    n-=x*k
ans=min(n,k-n)
print(ans)
n, k = map(int, input().split())
print(min(n%k, abs(n%k-k)))
n,k = 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))
n, k = map(int, input().split())

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

a = n % k
print(min(abs(a - k), a))
n,k = map(int,input().split())
print(min(n%k,abs(n%k-k)))
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


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

    x = N // K


    print(min(N % K, (x + 1) * K  - N))


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


if N>K:
    N=N % K

while True:
    if abs(N - K) < N:
        N = abs(N - K)
    else:
        print(N)
        exit()
N, K = list(map(int, input().split()))
a = N % K
print(min(a, abs(a-K)))
def main():
    n,k = map(int,input().split())
    if n >= k:
        answer = min(n%k,k-(n%k))
    else:
        answer = min(k-n,n)

    print(answer)

if __name__ == '__main__':
    main()
import math

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

while True:
  if abs(tmp - K) < tmp:
    tmp = abs(tmp - K)
  else:
    break
 	
print(tmp)
n, k = map(int, input().split())
print(min(n % k, k - n % k))
## C
N,K =map(int,input().split())

N_min=N%K
if N_min>K/2:
    N_min=K-N_min
print(N_min)
n,m=map(int,input().split())
n%=m
print(min([n,m-n]))
n,k=map(int,input().split())
a=[]
a.append(n%k)
a.append(k-n%k)
print(min(a))
N,K = list(map(int,input().split()))
if N%K == 0:
    print(0)
    exit()
print(min(N%K,abs(N%K-K)))
N,K=map(int,input().split())
k=N//K+1
print (min(K*k-N,N%K))
n, k = map(int, input().split())

a = n % k
print(min(a, abs(a-k)))
N, K = map(int, input().split())

N = N % K

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

print(N)
n,k = map(int, input().split())		# 5 7 2

m = n % k

ans = min(abs(m), abs(m-k))

print(ans)
N,K = map(int,input().split())
S = N // K
Answer = abs(N - K*S)
if abs(Answer - K) <= Answer:
    Answer = abs(Answer - K)
print(Answer)
n, k=map(int, input().split())
n=n%k
if(n>k-n):
  n=k-n
print(n)
def main():
    n, k = map(int, input().split())
    print(min(n % k, k-n % k))


main()
#!/usr/bin/env python3
n, k = map(int, input().split())
a = n % k
b = abs(k - a)
print(a) if a <= b else print(b)
n, k = map(int, input().split())

if k <= n:
    if n%k == 0:
        ans = 0
    else:
        tmp = n//k
        ans = min(abs((n-k*tmp)), abs((n-(k*(tmp+1)))))
else:
    ans = min(abs(n), abs(n-k))

print(ans)
N,K=map(int, input().split())
N%=K
while N > K/2:
    N = abs(N-K)
print(N)
n, k = map(int, input().split())

x = n % k

print(min(x, k - x))
n,k=(int(i) for i in input().split())
print(min(n%k,(((n//k)+1)*k)-n))
import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
INF = 1 << 60
sys.setrecursionlimit(10 ** 6)

#ここから書き始める
n, k = map(int, input().split())
ans = min(n % k, abs(n % k - k))
print(ans)
n, k = map(int, input().split())


x = n % k

y = abs(x - k)

if x >= y:
    print(y)
else:
    print(x)
n, k = map(int, input().split())

rem = n % k
rem_minus = (rem - k) * - 1

if rem >= rem_minus:
  print(rem_minus)
else:
  print(rem)
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input(): return sys.stdin.readline().rstrip()

def main():
    N,K=map(int,input().split())
    print(min(abs(N//K*K-N),abs((N//K+1)*K-N)))

if __name__ == '__main__':
    main()
n, k = map(int, input().split())

x = n % k
print(min(x, abs(k-x)))
# ABC161c


def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    # 再帰関数を使わない限りPypyで出すこと

    def dump(*args):
        sys.stderr.write(str(args))

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


if __name__ == '__main__':
    main()
[N,K]=list(map(int,input().split()))
def hoge(X):
	if X<0:X*=-1
	return X
print(sorted(map(hoge,[(N//K)*K-N,(N//K)*K+K-N]))[0])
N,K=map(int,input().split())
rest=N%K
print(min(rest,K-rest))
# coding: utf-8
# Your code here!

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

if K-a<=a:
    print(K-a)
else:
    print(a)
import sys
input = sys.stdin.readline

n, k = map(int, input().split())
print(min(n % k, k - (n % k)))
N, K = map(int, input().split())
n = N % K
print(min(n, K - n))
# abc161 c
n, k = [int(i) for i in input().split()]
temp1 = n % k
temp2 = abs(temp1 - k)
print(min(temp1, temp2))
l=input("").split(" ")
n=int(l[0])
k=int(l[1])
s=n%k
ss=k-s
if(s<ss):
    print(s)
else:
    print(ss)
def main():
    n, k = map(int, input().split())
    t = n % k
    print(min(t, k - t))


if __name__ == '__main__':
    main()
n, k = list(map(int, input().split()))
n = n % k
if n < abs(n-k):
  print(n)
else:
  print(abs(n-k))
n, k = map(int, input().split())
x = n % k
x = min(x, k - x)
n = x
print(n)
N, K = map(int, input().split())
if N % K == 0:
    print(0)
    exit()
if (N % K) >= abs((N % K) - K):
    print(K - (N % K))
else:
    print(N % K)
N,K=(map(int,input().split()))
if N==K:
    print(0)
elif N<K:
    if 2*N<=K:
        print(N)
    else:
        print(K-N)
else:
    if N%K==0:
        print(0)
    else:
        print(min(N%K, K-(N%K)))
n, k = map(int, input().split())

m = 10**18
n = n % k
while True:
    n = min(n, abs(n-k)) 
    if n < m:
        m = n
    else:
        break

print(m)
n,k = map(int,input().split())
m = n
m = m%k
if m > int(k/2):
  ans = abs(m-k)
else:
  ans = m
print(ans)
n,k=map(int,input().split())

ans=n%k

if ans<abs(ans-k):
    print(ans)
else:
    print(abs(ans-k))
n, k = list(map(int, input().split()))
mod = n % k
ans = min(mod, k - mod)
print(ans)
n, k = map(int, input().split())
m = n % k

print(min(m, abs(m-k)))
(n,k) = tuple(map(int,input().split()))

if n == k:
    print(0)
if n < k:
    if n <= k-n:
        print(n)
    if n > k-n:
        print(k-n)
if n > k:
    n2 = n%k
    print(min(n2,abs(n2-k)))
n, k = map(int, input().split())

print(min(n, n%k, k-n%k))
import sys


def input():
    return sys.stdin.readline().strip()

def main():
    n, k = map(int, input().split())
    a = n % k
    print(min(a, k-a))

main()
N, K = map(int, input().split())
print(min([N % K, K - (N % K)]))
n, k = map(int, input().split())

if n > k:
    print(min(n % k, abs(n % k - k)))
elif n == k:
    print(0)
elif n < k:
    print(min(n, k - n))
n,k=map(int, input().split())
a=n//k
print(min(abs(n-k*a), abs(n-k*(a+1))))
n, k = map(int , input().split())

if n%k > abs(n%k-k):  
  print(abs(n%k-k))
else :
  print(n%k)
n,k=map(int,input().split())
amari=n%k
print(min(amari,abs(amari-k)))
#!/usr/bin python3
# -*- coding: utf-8 -*-

import sys
input = sys.stdin.readline

def main():
    N,K=map(int, input().split())
    r=N%K
    r = min(r, abs(r-K))
    print(r)

if __name__ == '__main__':
    main()
import math  # noqa: F401
from typing import Dict, List, Optional, Tuple, Union  # noqa: F401


def main(N: int, K: int):
    if N == K:
        print(0)
        return

    if N > K:
        N -= N // K * K

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


if __name__ == "__main__":
    N, K = map(int, input().split())
    main(N, K)
#!/usr/bin/env python3

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

x = N % K
done_list = [x]
min_x = x
while True:
    x = abs(x - K)
    done_list.append(x)
    min_x = min(x, min_x)
    if x in done_list:
        break
ans = min_x

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

Nn = N%M
Nm = abs(Nn - M)

if(Nn >= Nm):
    print(Nm)
else:
    print(Nn)
import math
n, k = map(int, input().split())

aaa = (n % k)

if aaa == 0:
  ret = 0
else:
  if n > k:
    ret = min(abs(aaa - k), aaa)
  else:
    ret = min(k - n, n)

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

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

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

a=N//K

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

mod1 = n%k
mod2 = abs(n%k - k)

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

print(min(n % k, k - n % k))
N,K=map(int,input().split())
print(min(N%K,-N%K))
n, k = [int(x) for x in input().split(' ')]

positive_sol = n % k

if positive_sol > k // 2:
    print(k - positive_sol)
else:
    print(positive_sol)
import sys

if sys.platform =='ios':
    sys.stdin=open('input_file.txt')
   
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


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

if k==1:
	print("0")

else:
	n=n%k
	while n>k//2:
		n=abs(n-k)
	
	print(n)
N, K = map(int, input().split())

a = N%K

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

amari = N % K

print(min(amari, abs(amari - K)))
# -*- coding: utf-8 -*-

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

t=N//K


ans = min(N - K * t, -(N - K * (t + 1)))
print(ans)
import sys
input = sys.stdin.readline
from collections import Counter, deque

INF = float('inf')


def main():
    N, K = map(int, input().split())
    print(min(N % K, K - (N % K)))


if __name__ == '__main__':
    main()
N,K = map(int,input().split())
print(min(abs(N%K),abs((-N)%K)))
if __name__ == '__main__':
	N, K = map(int, input().split())

	ans = 0

	if N < K:
		ans = min(N, K-N)
	elif N >= K:
		ans = min(N%K, abs(N%K - K))

	print(ans)
N,K = (int(x) for x in input().split())
L = N % K
M = abs(K-L)
print(min(L,M))
n, k = map(int, input().split())
print(min(abs(n - (n // k) * k), abs(n - (n // k + 1) * k)))
n,k = map(int,input().split())

a = n//k
n=n-a*k

n_m=abs(n-k)
if n_m > n:
    print(n)
else:
    print(n_m)
N, K = map(int, input().split())
print(N%K if N%K < abs(K-N%K) else abs(K-N%K))
x,y = map(int,input().split())
d = x % y
d_ = y - d
if d > d_:
  print(d_)
else:
  print(d)
N, K = map(int,input().split())

min = N



while True:
  if N >= K:
    N = N % K
  else:
    N = K - N

  if N < min:
    min = N
  elif N == min:
    break
  
print(min)
import math
def I(): return(list(map(int,input().split())))
n,m=I()
print(min(n%m,m-n%m))
N, K = map(int, input().split())
if N <= K:
    while True:
        if N > abs(N - K):
            N = abs(N - K)
        else:
            break
else:
    N = N % K + K
    while True:
        if N > abs(N - K):
            N = abs(N - K)
        else:
            break
print(N)
import array
N, K = map(int, input().split(' '))

minimum = x = N % K

cache = set()
while True:
    x = abs(x - K)
    minimum = min(x, minimum)
    if x in cache:
        break
    else:
        cache.add(x)

print(minimum)
N, M = map(int, input().split())

set_list = {N}
len_list = -1

while len(set_list) != len_list:
    # print(set_list)
    len_list = len(set_list)
    if N >= M:
        set_list.add(N%M)
        N = N%M
    else:
        set_list.add(abs(N-M))
        N = abs(N-M)

    # print(set_list, len_list, N%M)
print(min(set_list))
N, K = map(int, input().split())

if N % K == 0:
    print(0)
else:
    N = N % K
    print(min([N, abs(N-K)]))
N, K = list(map(int, input().split()))

n = N % K
n_another = K - n

ans = min(n, n_another)
print(ans)
N, K = map(int, input().split())
q, mod = divmod(N, K)
if mod == 0:
    print(0)
else:
    if mod*2 > K:
        print(abs(mod-K))
    else:
        print(mod)
N,K = map(int, input().split())

x = N-(N//K)*K
print(min(abs(x-K), x))
N, K  = map(int, input().split())

N = N % K

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


resolve()
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

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

mod = 10**9 + 7

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

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

N,K = LI()

count = N//K

N = N-count*K
print(min(N,abs(N-K)))
a = list(map(int, input().split()))
n = a[0]
k = a[1]

if n%k < k - n%k:
    print(n%k)
else:
    print(k - n%k)
import math
N,K=map(int,input().split())
amari=N%K
a=abs(amari-K)
print(min(amari,a))
N,K = map(int,input().split())
a = min(N, N%K)
b = abs(N - K * (N//K+1))
print(min(a,b))
N,K=map(int,input().split())
print(min(abs(N-(K*(N//K))),abs(N-(K*((N//K)+1)))))
def main():
    n,k = map(int, input().split())

    tmp = n - (n//k)*k
    print(min(tmp, abs(tmp-k)))


if __name__ == '__main__':
    main()
n, k = map(int, input().split())

n = n % k
print(min([abs(k-n), n]))
N, K = map(int, input().split())
N = N%K
while(1):
    tmpN = abs(N-K)
    if tmpN < N:
        N = tmpN
    else:
        break;
print(N)
N, K = map(int, input().split())

res = N

if N == K:
    print(0)

elif N < K:
    print(min(K- N, N))

else:
    r = N // K
    print(min(N, N - K * r, K * (r + 1) - N))
n,k=map(int,input().split())
r=n//k
a=min(abs(n-k*r),abs(n-k*(r+1)),abs(n+k*r),abs(n+k*(r+1)))
print(a)
# -*- coding: utf-8 -*-
import numpy as np

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

if n > k:
    tmp = n % k
    if tmp >= k - tmp:
        ans = k - tmp
    else:
        ans = tmp
elif n < k:
    if n >= k - n:
        ans = k - n
    else:
        ans = n
else:
    ans = 0

print(ans)
def main():
    n, m = map(int, input().split())
    n = abs(n - (n // m) * m)
    n = min(n, abs(n - m))
    print(n)

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

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


print(min(N, (N - (N // K)*K), abs(N - (N // K + 1)*K)))
n, k = map(int, input().split())
print( min( n % k, -n % k ) )
N, K = map(int, input().split())
ANS = N % K
if ANS > abs(ANS - K):
    print(abs(ANS - K))
else:
    print(ANS)
import math

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

amari = n % k
n = amari

ab = abs(n - k)

while n > ab:
  n = ab
  ab = abs(n - k)

print(n)
n,k=map(int,input().split())
p=n%k
print(min([p,-(p-k)]))
import sys
import os

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

N = N - K * (N // K)

NN = -(N - K)

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


if N == 0:
    print(N)
elif K == 0:
    print(K)
elif N < K:
    k = K % N
    ans = abs(N - k)
    print(ans)
elif N > K:
    k = N % K
    ans = abs(K - k)
    if k == 0:
        print("0")
    else:
        print(ans)
else:
    print("0")
#!/usr/bin/env python3
import collections
import itertools as it
import math
import numpy as np

#  = input()
#  = int(input())
N, K = map(int, input().split())
#  = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()

if N > K:
    seimin = N - (N//K) * K
    print(min(seimin, abs(seimin-K)))
else:
    print(N)
a,b= map(int, input().split())
min = 100000000000000000000


if a== 0:
  print(a)
elif a <= b:
  while min > abs(a-b):
    min = abs(a-b)
    a   =abs(a-b)
elif a > b:
  a = a%b
  while min > abs(a-b):
    min = abs(a-b)
    a   =abs(a-b)
  
print(min)
N, K = map(int,input().split())

while K != 0:
    N,K = K,N%K

print(N)
n, k = (int(i) for i in input().split())
if k > n:
    print(n)
elif n % k == 0:
    print(0)
else:
    q = n % k
    q = abs(q-k)
    print(min(k, q))
s = input().split(" ")
N = int(s[0])
K = int(s[1])

if N == 0:
    print(K)
elif N == 1 or K == 1:
    print("0")
elif N == K:
    print("0")
elif N % K == 0 or K % N == 0:
    print("0")
elif N % 2 == 0 and K % 2 == 0:
    print("2")
else:
    print("1")
N, K = input().split();
N = int(N);
K = int(K);

amari = N % K;

hu = amari - K;

max_N = max(amari,hu);

print(max_N);
i = list(map(int, input().split()))
N = i[0]
K = i[1]
 
for i in range(1000000):
    if i == 0:
        min = abs(N-K)
    min = abs(min-K)
    if (min-K) < 0:
        break
if min > 5:
    print('0')
else:
    print(min)
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 > 5:
            return y


# if K == 0:
#     print(N)
# elif K == 1:
#     print(0)
# else:
#     print(x(N, K))
print(x(N, K))
N, K = input().split();
N = int(N);
K = int(K);

amari = N % K;

hu = -1* (amari - K);

max_N = max(amari,hu);

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

ans = []
a = N

if N < K:
    print(N)
elif N == K or K == 1:
    print(0)
else:
    ans2 = N % K
    ans1 =  (N % K ) - K
    if abs(ans2) > abs(ans1):
        print(abs(ans1))
    else:
        print(abs(ans2))
def gcd(m, n):
    r = m % n
    return gcd(n, r) if r else n

n, k = map(int, input().split())
print(min(gcd(n, k), n%k))
N,K=map(int, input().split()) 

if N>K:
  y = int(N/K)
  x = N - y*K
else:
  x = N 
#print(x)
cnt = 0
ans = x
while cnt<10:
  x = abs(x-K)
  ans = min(ans, x)
  cnt += 1
ans = min(ans, 0)
print(ans)
import sys
n,k = map(int,input().split())
m = n
c = 0
r = True
if n%k == 0:
    print(0)
    sys.exit()
while r == True:
    if k >= 2*m:
        r = False
    elif k >= m:
        c += 1
        r = False
    else:
        print(n%k)
        sys.exit()

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

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