Problem:
 Score : 300 points    Problem Statement Given any integer x , Aoki can do the operation below.  Operation: Replace x with the absolute difference of x and K .  You are given the initial value of an integer N . Find the minimum possible value taken by N after Aoki does the operation zero or more times.      Constraints  0 ≤ N ≤ 10^{18}  1 ≤ K ≤ 10^{18}  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  K       Output Print the minimum possible value taken by N after Aoki does the operation zero or more times.        Sample Input 1 7 4      Sample Output 1 1  Initially, N=7 .  After one operation, N becomes |7-4| = 3 .  After two operations, N becomes |3-4| = 1 , which is the minimum value taken by N .       Sample Input 2 2 6      Sample Output 2 2  N=2 after zero operations is the minimum.       Sample Input 3 1000000000000000000 1      Sample Output 3 0

Solution (Python):
N, K = map(int, input().split())

if K > 1:
  N = N - N // K * K
else:
  N = 0
minimum = N
count = 0

while count <= N / K:
  N = abs(N - K)
  minimum = min(N, minimum)
  count += 1
print(minimum)

Solution (Python):
n,k=map(int,input().split())

print(min(n-k*(n//k),-n+k*(n//k+1)))

Solution (Python):
N, K = 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)

Solution (Python):
n,k=map(int,input().split())
print(min(n%k,-n%k))

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

Solution (Python):
n,k=map(int,input().split())
print(min(n%k,abs(n%((-1)*k))))

Solution (Python):
n,k = map(int, input().split())
i=n//k
a = abs(n-k*i)
b = abs(n-k*(i+1))
print(min(a,b))

Solution (Python):
import math
a,b=[int(i) for i in input().split()]
c=a//b
e=abs(a-(b*c))
f=abs(a-(b*(c+1)))
print(min(e,f))

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

if a <= abs(a-K):
    print(a)
else:
    print(abs(a-K))

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

Solution (Python):
n, k = map(int, input().split())

a = n % k
if (a <= int(k/2)):
  print(a)
else:
  print(k-a)

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

Solution (Python):
n,k = map(int,input().split())
check = []
check.append(n%k)
check.append(k-check[0])
print(min(check))

Solution (Python):
import math
n,k = map(int,input().split())
s = n//k
if n-k*s>abs(n-k*s-k):
  print(abs(n-k*s-k))
else:
  print(n-k*s)

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

Solution (Python):
n, k = map(int, input().split())
if n > k:
    n = n % k
print (min(n, abs(n-k)))

Solution (Python):

N, Y = map(int, input().split())
ans = min(N%Y, Y-(N%Y))
print(ans)

Solution (Python):
def sep():
    return map(int,input().strip().split(" "))
def lis():
    return list(sep())
import sys
sys.setrecursionlimit(10**6)

n,k=sep()
if n>=k:
    n=(n%k)
print(min(n,abs(k-n)))

Solution (Python):

a,b = map(int,input().split())
yon = 100000000000000000000000
a = a%b
yon = min(a,abs(a-b))
print(yon)

Solution (Python):

import sys


def solve(N: int, K: int):
    ans = N
    N = abs(N - K * (N // K))
    if ans > N:
        ans = N
    N = abs(N -K)
    if ans > N:
        ans = N


    print(ans)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    solve(N, K)

if __name__ == '__main__':
    main()

Solution (Python):

import sys


def solve(N: int, K: int):
    ans = N
    N =  abs(N - K * (N // K))
    if ans > N:
        ans = N
    for i in range(3):
        N = abs(N - K)
        if ans > N:
            ans = N


    print(ans)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    solve(N, K)

if __name__ == '__main__':
    main()

Solution (Python):
n, k = map(int, input().split())

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

Solution (Python):
n,k = map(int,input().split())
a = abs(n-k*(n//k))
b = abs(n-k*(-(-n//k)))
print(min(a,b))

Solution (Python):
n,k = map(int,input().split())

print((n%k) if (n%k) < (k-(n%k)) else (k-(n%k)))

Solution (Python):








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







if N%K==0:
    print(0)
else:
    et=N//K
    a1=et*K
    a2=(et+1)*K
    mi = min(abs(a1-N),abs(a2-N))
    print(mi)

Solution (Python):
n, k = map(int, input().split())


x = n % k

y = abs(x - k)

if x >= y:
    print(y)
else:
    print(x)

Solution (Python):
n, k =map(int,input().split())
ans1 = n%k
ans2 = k-ans1
print(min(ans1,ans2))

Solution (Python):
n,k = map(int,input().split())


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

Solution (Python):
N,K = map(int,input().split())
count = N//K
N = abs( N - count*K )
N_2 = abs( N - K )
print( N if N <= N_2 else N_2 )

Solution (Python):
N, K = map(int, input().split())

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

print(ans)

Solution (Python):
n, k = map(int, input().split())

x = n % k
y = abs(x - k)

if x  < y:
    print(x)
else:
    print(y)

Solution (Python):
N, K = map(int, input().split())

mod1 = N % K
mod2 = (K - mod1)

print(mod1 if mod1 < mod2 else mod2)

Solution (Python):
N,K=map(int,input().split())

if N==K:
  print(0)

elif N<K:
  lis1=[N,abs(N-K)]
  print(min(lis1))
  
else:
  lis2=[N%K,abs(N%K-K)]
  print(min(lis2))

Solution (Python):
N,K = map(int, input().split())

y = N%K
z = abs(K-y)

ans = min(y,z)

print(ans)

Solution (Python):
def N():
    return int(input())
def L():
    return list(map(int,input().split()))
def NL(n):
    return [list(map(int,input().split())) for _ in range(n)]
import math

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

Solution (Python):
a,b=[int(s) for s in input().split()]
print(min([a%b,(b-a)%b]))

Solution (Python):
n, k = map(int, input().split())
print(min(n % k, k - n % k))

Solution (Python):
a,b=[int(s) for s in input().split()]
print(min([a%b,(b-a)%b]))

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

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

Solution (Python):
N, K = map(int, input().split())
minCandidate1 = N % K
minCandidate2 = K - minCandidate1
if minCandidate1 <= minCandidate2:
  print(minCandidate1)
else:
  print(minCandidate2)

Solution (Python):
N,K = map(int,input().split())
n = N - K * (N//K)
n = min(n, abs(n-K))
print(n)

Solution (Python):
def resolve():
    n,k = map(int,input().split())
    print(min(n-k*(n//k),abs(n-k*(n//k)-k)))
resolve()

Solution (Python):
N,K = map(int,input().split())

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

Solution (Python):
N, K = map(int, input().split())

result = N % K
if result <= abs(result-K):
  print(result)
elif result > abs(result-K):
  result = abs(result-K)
  print(result)

Solution (Python):
[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)

Solution (Python):
n,k = map(int, input().split())
a = n // k
ans = min(abs(n - a*k),abs(n - (a+1)*k))
print(ans)

Solution (Python):
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))

Solution (Python):
n, k = map(int, input().split())
if n >= k:
    ans = min(n % k, k - (n % k))
else:
    ans = min(n, k - n)
print(ans)

Solution (Python):

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

ans = N % K
if abs(ans - K) < ans:
    ans = abs(ans - K)

print(ans)

Solution (Python):
N, K = map(int, input().split())

x = N
pre_x = x

if x // K > 0:
    x = abs(x - K * (x // K))
    pre_x = x

while True:
    pre_x = x
    x = abs(x - K)

    if pre_x < x:
        print(pre_x)
        exit()

Solution (Python):
import numpy

n, m = input().split()
n = numpy.int64(n)
m = numpy.int64(m)
n = n % m
print(min(n, m-n))

Solution (Python):
import numpy

n, m = input().split()
n = numpy.int64(n)
m = numpy.int64(m)
n = n % m
print(min(n, m-n))

Solution (Python):
N, K = map(int, input().split())
s = N//K
s1 = abs(N - K*s)
s2 = abs(N - K*(s+1))
print(min(s1, s2))

Solution (Python):
n, k = map(int, input().split())

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

print(n)

Solution (Python):
n,k = map(int,input().split())
print(min(n%k,-((n%k)-k)))

Solution (Python):
n, k = map(int, input().split())
w = n%k
w2 = abs(w-k)
if w > w2:
    print(w2)
else:
    print(w)

Solution (Python):


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

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

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys

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

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

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

print(min)

Solution (Python):
n,k = map(int,input().split())

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

Solution (Python):
import sys
import copy
a = [int(c) for c in input().split()]
N=a[0]
K=a[1]



while True:
    i = 0
    while K*10**(i+1)<N:
        i+=1
    N = abs(N-K*10**i)
    if K*10 >= N:
        break
Nmin = 10**18
while True:
    N = abs(N-K)
    if Nmin>N:
        Nmin=N
    else:
        print(Nmin)
        sys.exit(0)

Solution (Python):
n,k = map(int,input().split())

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

Solution (Python):
n, k = map(int, input().split())

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

Solution (Python):
N,K = map(int,input().split())

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

print (min)

Solution (Python):
N, K = list(map(int, input().split()))
if abs(N - K) > N:
    print(N)
else:
    if N % K == 0:
        N = 0
    else:
        N = abs(N % K - K)
    print(N)

Solution (Python):
n, k = map(int, input().split())
a = n % k
b = abs(a - k)
print(min(a, b))

Solution (Python):
n, k = map(int, input().split())
print(min(n % k, k - n % k))

Solution (Python):
n, k = map(int,input().split())
x1 = n%k
x2 = k - x1
print(min(x1, x2))

Solution (Python):
n,k =map(int,input().split())
if n//k != 0:
        n -= (n//k)*k
else:
        pass
def p(n):
        if n > abs(n-k):
                return True
        else:
                return False
while p(n):
        n = abs(n-k)

print(n)

Solution (Python):
n,k =map(int,input().split())
if n//k != 0:
        n -= (n//k)*k
else:
        pass
def p(n):
        if n > abs(n-k):
                return True
        else:
                return False
while p(n):
        n = abs(n-k)

print(n)

Solution (Python):

N, K = map(int, input().split())
ans = 10**10
mod = N % K
ans = min(mod, abs(mod-K))
print(ans)

Solution (Python):
[N, K] = [int(i) for i in input().split()]
print(min(N%K, -N%K))

Solution (Python):
INF = int(10e20)


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

    minval = N
    sub = N // K - 5
    if sub > 0:
        N = N - K * sub

    for i in range(20):
        N = abs(N - K)
        minval = min(N, minval)

    print(minval)


main()

Solution (Python):
n,k=map(int,input().split())
s=n//k
m=min((n-s*k),((s+1)*k-n))
print(m)

Solution (Python):
N, K = map(int, input().split())

t = N % K

print(min(t, K-t))

Solution (Python):
N,K =map(int, input().split())
a = N % K
if a > K / 2:
  print(K - a)
else:
  print(a)

Solution (Python):
N,K = map(int, input().split())

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

Solution (Python):

import sys
input=sys.stdin.readline

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

if __name__=="__main__":
    main()

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

Solution (Python):
n,k = map(int,input().split())
print(min(n%k,k-n%k))

Solution (Python):

n,k = map(int,input().split())
x = n
t = n%k
ans = min(t, k-t)
print(ans)

Solution (Python):
n,k = map(int, input().split())
r = n % k
s = abs(r - k)
print(min(r,s))

Solution (Python):
n,k = [int(x) for x in input().split()]
a = n // k
n -= a * k
print(min(n,abs(n-k)))

Solution (Python):
N, K = map(int, input().split())
N = N % K

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

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

Solution (Python):

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

if __name__ == "__main__":
    main()

Solution (Python):
N, K = map(int, input().split(' '))
print(min(N % K, abs(N % K - K)))

Solution (Python):
N, K = map(int, input().split())
print(min(N%K, abs(N%K-K)))

Solution (Python):
n,k = map(int,input().split())

if n < k:
    rival_n = k - n
    if n >= rival_n:
      print(rival_n)
    else:
      print(n)

elif n == k:
    print(0)

else:

    n = n % k
    rival_n = k - n
    if n >= rival_n:
        print(rival_n)
    else:
        print(n)

Solution (Python):

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

Solution (Python):
N,K = map(int,input().split())

def solve(n,k):
  n %= k
  return min(n,abs(n-k))

print(solve(N,K))

Solution (Python):
N, K = map(int, input().split())
ans = min(abs(N%K), abs(N-K*(N//K+1)))
print(ans)

Solution (Python):
N, K = map(int, input().split(' '))
print(min(N % K, abs(N % K - K)))

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

    N %= K
    ans = abs(N-K)
    print(ans if ans < N else N)


if __name__ == '__main__':
    main()

Solution (Python):


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

m = n % k
l = map(abs,[m-k,m,m+k])
print(min(l))

Solution (Python):
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)

Solution (Python):
n, k = map(int, input().split())

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

Solution (Python):

x,y = map(int,input().split())
d = x % y
d_ = y - d
if d > d_:
  print(d_)
else:
  print(d)

Solution (Python):
n,k = map(int, input().split())
r = n % k
s = abs(r - k)
print(min(r,s))

Solution (Python):
N, K = map(int, input().split(' '))
print(min(N % K, abs(N % K - K)))

Solution (Python):
n,k = map(int,input().split())

top = n - n//k*k
low = n - ((n//k)+1)*k

ans = min(abs(top),abs(low))
print(ans)

Solution (Python):
n, k = map(int , input().split())

if n%k > abs(n%k-k):  
  print(abs(n%k-k))
else :
  print(n%k)

Solution (Python):
N, K = map(int,input().split())

if N > K:
  N = N % K

min_N = float("inf")
while min_N != N:
  min_N = min(N,min_N)
  if N == K or N == 0 or K == 0:
    print(0)
    exit(0)
  
  N = abs(N - K)
  
print(min_N)

Solution (Python):
n, k = [int(i) for i in input().split()]
ans = min((n % k), (k - n % k))
print(ans)

Solution (Python):
n, k = map(int, input().split())

t = n // k
re = n - t * k


if abs(re - k) < re:
    re = abs(re - k)
    i = True

print(re)

Solution (Python):
n, k = map(int, input().split())

t = n // k
re = n - t * k

i = True

while i == True:
    if abs(re - k) < re:
        re = abs(re - k)
        i = True
    else:
        i = False

print(re)

Solution (Python):
N, K = map(int, input().split(' '))
print(min(N % K, abs(N % K - K)))

Solution (Python):
n,k = map(int, input().split())
ans = n % k

print(min(abs(ans-k),ans))

Solution (Python):
n,k = map(int,input().split())
p = n % k
m = n % k - k
print(min(p,-m))

Solution (Python):
N, K = map(int, input().split())

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

Solution (Python):
N, K = map(int, input().split())
ans = N % K

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

print (abs(ans))

Solution (Python):
N, K = map(int, input().split(' '))
print(min(N % K, abs((N % K) - K)))

Solution (Python):
n, k = map(int, input().split())
t = n % k
t1 = k-t
if t < t1:
    print(t)
else:
    print(t1)

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

Solution (Python):
N,K = map(int,input().split())
i = N // K
N = N - K * i

ans = N
while True:
  N = abs(N - K)
  if ans > N:
    ans = N
  else:
    break
    
print(ans)

Solution (Python):
N,K = map(int, input().split())
N = N%K
while(abs(N-K) < N):
  N = abs(N-K)
print(N)

Solution (Python):
N, K = map(int, input().split())

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

Solution (Python):
N, K = map(int,input().split())
n = N // K
print(min(-N + (n + 1) * K, N - n * K))

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

Solution (Python):

import sys
def input():
    return sys.stdin.readline()[:-1]

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

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

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
N, K = map(int, input().split(' '))
print(min(N % K, abs(N % K - K)))

Solution (Python):
n, k = map(int, input().split())

ans = n
if abs(n-k) > k:
    x = abs(n-k)//k
    n = abs(n-k) - x*k
ans = min(ans, n, abs(n-k), abs(abs(n-k)-k))
print(ans)

Solution (Python):
N,K=map(int,input().split())

a=N//K

ans=min(abs(N-a*K), abs(N-(a+1)*K))
print(ans)

Solution (Python):
n, k = map(int, input().split())  
n = n % k
while (n > abs(n-k) ):
    n = abs(n-k)
print(n)

Solution (Python):
N, K = map(int, input().split())

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

Solution (Python):
N, K = map(int, input().split())
res = N % K
if abs(K-res) < res:
  res = abs(K-res)
print(res)

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

Solution (Python):
N, K = map(int, input().split())


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

Solution (Python):
n,k=map(int,input().split())

a=n%k

b=k-a

print(min(a,b))

Solution (Python):
N, K = map(int, input().split())

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

print(ans)

Solution (Python):
n, k = map(int,input().split())

n = n % k

print(min(n,abs(k-n)))

Solution (Python):
def manipulate(x, k):
	return abs(x - k)

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

while n > k / 2:	
	n = manipulate(n, k)
	
print(n)

Solution (Python):
N, K = map(int, input().split())

cand_1 = N % K
cand_2 = K - cand_1

min = min(cand_1, cand_2)

print(min)

Solution (Python):

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

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

Solution (Python):
n, k = map(int, input().split())

d = n // k
ans = min(abs(n - k * d), abs(n - k * (d + 1)))

print(ans)

Solution (Python):
N,K=map(int,input().split())
a=N//K
if a==0:
    x=N
    y=abs(N-K)
    if x>y:
        print(y)
    else:
        print(x)
elif N%K!=0:
    x=N-a*K
    y=abs(N-(a+1)*K)
    if x>y:
        print(y)
    else:
        print(x)
else:
    print(0)

Solution (Python):
n,k=map(int,input().split())
a=n//k
print(min(abs(n-a*k),abs(n-(a+1)*k)))

Solution (Python):
n,k=map(int,input().split())

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

Solution (Python):
def main():
    n, k = map(int, input().split())

    if n == k:
        ans = 0
    elif n > k:
        a = n // k
        ans = min(n - a * k, abs(n - (a + 1) * k))
    elif n < k:
        ans = min(n, k - n)

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
a,b=map(int,input().split())
print(min(b-(a%b),a%b))

Solution (Python):
x = list(map(int, input().split()))

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

y = n % k
if y < abs(y - k):
    print(y)
else:
    print(abs(y - k))

Solution (Python):
N,K = map(int,input().split())

valueA = N % K
valueB = K - valueA

if valueA > valueB:
  print(valueB)
else:
  print(valueA)

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

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

Solution (Python):
def main():
    n, k = 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()

Solution (Python):
def main():
    n, k = 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(3):
            if abs(minv - k) < abs(minv):
                minv = abs(minv - k)
            else:
                print(minv)
                exit()
        print(minv)
        
    min_x(minv)


if __name__ == '__main__':
    main()

Solution (Python):
N, K = map(int, input().split(' '))
rst = 0
rst = min(N % K, abs(N % K - K))
print(rst)

Solution (Python):
N, K = map(int, input().split())
A = N // K
B = N % K
if B > abs(B-K):
  print(abs(B-K))
else:
  print(B)

Solution (Python):
n, k = map(int, input().split())
if n>k:
  n= n-(n//k)*k
while n >abs(k-n):
  n= abs(k-n)
print(n)

Solution (Python):
n,k = map(int,input().split())
a = n%k
b = k - a
print(min(a,b))

Solution (Python):
N, K = map(int, input().split())

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

print(ans)

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

Solution (Python):
N, K = map(int, input().split())

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

print(ans)

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

Solution (Python):
import sys
from collections import deque
import numpy as np
import math
sys.setrecursionlimit(10**6)
def S(): return sys.stdin.readline().rstrip()
def SL(): return map(str,sys.stdin.readline().rstrip().split())
def I(): return int(sys.stdin.readline().rstrip())
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def solve():
    x = n%k
    ans = min(n,abs(x-k),x)
    print(ans)
    return

if __name__=='__main__':
    n,k = IL()
    solve()

Solution (Python):
def main():
    N,K = map(int,input().split())
    syo = N//K
    amari = N%K
    N = abs(amari-K)
    ans = min(N,amari)
    return ans
    

print(main())

Solution (Python):
N, K = map(int, input().split())
list1 = []
if N <= abs(N - K):
  print(N)
  exit()
else:
  N = N % K
  while N > abs(N-K):
    N = abs(N - K)
    list1.append(N)
  
if list1 ==[]:
  print(0)
else:
  print(min(list1))

Solution (Python):
N,K=map(int,input().split())

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

print(ans)

Solution (Python):
n,k=[int(x) for x in input().split()]
if n%k==0:
  print(0)
else:
  m=n//k
  a=abs(n-m*k)
  b=abs(a-k)
  ans=min(a,b)
  print(ans)

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

Solution (Python):
N,K=list(map(int,input(' ').split(' ')))
print(min(N%K,(-N)%K))

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

Solution (Python):
n,k = map(int, input().split())
x = abs(k*((n//k)+1)-n)
y = abs(k*((n//k)-11)-n)
z = abs(k*(n//k)-n)
print(min(n,x,y,z))

Solution (Python):
N,K=map(int,input().split())
if N%K==0:
    print(0)
else:
    a=N%K
    b=abs(a-K)
    if a<b:
        print(a)
    else:
        print(b)

Solution (Python):
def abc161c_replacing_integer():
    n, k = map(int, input().split())
    val = n // k
    result = min(n - val * k, abs(n - (val+1) * k))
    print(result)

abc161c_replacing_integer()

Solution (Python):
def resolve():
    N, K = list(map(int, input().split()))
    boundleft = N%K
    boundright = K-N%K
    print(min(boundleft, boundright))

if '__main__' == __name__:
    resolve()

Solution (Python):
import sys
import numpy as np
import numba
from numba import jit

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

@jit
def main(n, k):
    n = n%k
    return min(n, abs(n-k))


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

print(main(n, k))

Solution (Python):
N,K = map(int,input().split())
inc = abs(N-K)

if N <= inc :
    print(N)
elif K/2 < N%K :
    print(K-N%K)
else :
    print(N%K)

Solution (Python):
n,k=map(int,input().split())
ans=n%k

if ans>k//2:
    print(abs(ans-k))
elif ans<=k//2:
    print(ans)

Solution (Python):
N, K = map(int, input().split())

ans = 0

ans_1 = N//K
ans_2 = ans_1 + 1

ans = min(abs(N-ans_1*K), abs(N-ans_2*K))
print(ans)

Solution (Python):
N, K = map(int, input().split())
ans=N%K
print(min(ans, abs(ans-K)))

Solution (Python):
n,k=map(int,input().split())

if n%k==0:
  print(0)
elif n<=k:
  print(min(n,k-n))
else:
  num=n%k
  print(k-num)

Solution (Python):
n, k = map(int, (input().split()))

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

Solution (Python):
n,m=map(int,input().split())

c=n%m

if c<abs(c-m):
  print(c)
else:
  print(abs(c-m))

Solution (Python):
n,k=map(int,input().split())

if n>=k:
    n%=k

ans=min(n,k-n)

print(ans)

Solution (Python):
N, K = map(int, input().split())

tmp = N // K

ans = N - K * tmp

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

print(ans)

Solution (Python):
n,k = map(int,input().split())
m = n%k
a = abs(k - m)
l = [m,a]
print(min(l))

Solution (Python):



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

if K-a<=a:
    print(K-a)
else:
    print(a)

Solution (Python):
n,k=map(int,input().split())

if n%k<=abs(n%k-k):
    print(n%k)
else:
    print(abs(n%k-k))

Solution (Python):
n,m=map(int,input().split())
s=n-((n//m)*m)
c=abs(s-m)
print(min(s,c))

Solution (Python):
n,k = map(int,input().split())
print(min(n%k,k-(n%k)))

Solution (Python):
N, K = map(int, input().split())
cad = N // K
n = N - K * cad
m = K * (cad + 1) - N
print(min(n, m))

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

Solution (Python):
N, K = map(int,input().split())

rem = N % K
res = K - rem

result = [rem, res]

print(min(result))

Solution (Python):
N,K=map(int,input().split())
x=N%K
y=abs(x-K)    
print(min(x,y))

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())


def main():
    N, K = MI()
    a = N % K
    b = K - a
    print(min(a, b))
if __name__ == "__main__":
    main()

Solution (Python):
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def main():
    N,K=mi()
    
    n = N//K
    ans = INF
    for m in [n-1,n,n+1]:
        ans = min(ans,abs(N-K*m))
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
a,b = map(int, input().split())
if abs(a-b) > a:
    print(a)
else:
    if a%b == 0:
        print(0)
    else:
        print(abs(a%b-b))

Solution (Python):
n,k = map(int,input().split())
r = n%k
print(min(r,k-r))

Solution (Python):
x,k = map(int,input().split())
y = x % k
print(min(y,k-y))

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

Solution (Python):








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

tmp = n//k

a = abs(n-k*tmp)
b = abs(a-k)

print(min(a,b))

Solution (Python):
N, K = map(int, input().split())
if N < K:
    print(min(abs(N - K), N))
else:
    div, mod = divmod(N, K)
    print(min(abs(mod-K), mod))

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

Solution (Python):
n, k = map(int, input().split())
print(min(n % k, k-n % k))

Solution (Python):
n,k=map(int,input().split())
n=n%k
print(min(n,k-n))

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

if n < k:
    ans = min(n, abs(n - k))
else:
    q = n // k
    ans = min(abs(n - (q * k)), abs(n - (q * k) - k))

print(ans)

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

while True:
    if abs(N - K) >= N:
        break
    else:
        if N >= K:
            N = N - (N // K) * K
        else:
            N = N - (N // (2 * N - K)) * (2 * N - K)

print(N)

Solution (Python):
def solve():
    N, K = list(map(int, input().split()))
    if(N >= K):
        tmp = N - ((N // K) * K)
        ans = min(tmp, abs(abs(tmp) - K))
    elif(N < K):
        if(K <= 2*N):
            ans = abs(N - K)
        else:
            ans = abs(N)
    print(ans)

if __name__ == "__main__":
    solve()

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

Solution (Python):
n, k = map(int, input().split())
print(min(n%k, k-n%k))

Solution (Python):

def main():
    n, k = map(int, input().split())
    if n == k:
        return 0
    elif n < k:
        tmp = abs(n - k)
        if tmp < n:
            return tmp
        else:
            return n
    else:
        tmp = n // k
        n -= k * tmp
        tmp = abs(n - k)
        if tmp < n:
            return tmp
        else:
            return n
            
    
print(main())

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

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

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

Solution (Python):
n,k = map(int, input().split())
n = (n - (n//k-1)*k) if n > k else n
n_min = 0
while n_min <= n:
    n_min = abs(n-k)
    if n_min >= n and n < k:
        print(min(n_min, n))
        break
    n = n_min

Solution (Python):
n,k=map(int,input().split())
amari=n%k

if amari>k:
  print(k)
else:
  print(min(amari,abs(amari-k)))

Solution (Python):
n,k=map(int,input().split())
x_max=(n//k)
a=n-k*x_max
b=abs(a-k)
print(min(a,b))

Solution (Python):
n, k = map(int, input().split())

if n % k == 0:
  print(0)
else: 
  ans = n % k
  
  while ans > k//2:
    n = abs(ans - k)
    ans = min(n, ans)
  print(ans)

Solution (Python):
N, K=map(int, input().split())
m=int(N%K)
M=K-m
if 2*m>K:
  print(M)
else: print(m)

Solution (Python):
N, K = map(int, input().split())
if N >= K:
    print(min(N - (N // K) * K, ((N + K) // K) * K - N))
else:
    print(min(N, K - N))

Solution (Python):
N, K = map(int, input().split())
a = min(N%K, K-N%K)
print(a)

Solution (Python):

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

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


if __name__ == '__main__':
    main()

Solution (Python):
N, K = list(map(lambda n: int(n), input().split(" ")))
 
while N > int(K/2):
  N = min([abs(N-K), N % K])

print(N)

Solution (Python):
N, K = list(map(int, input().split()))
print(min(N%K,abs(N-((N//K)+1)*K)))

Solution (Python):
n, k = map(int, input().split())
ans_list = []
a = n%k
if k-a > a:
    print(a)
else:
    print(k-a)

Solution (Python):
N, K = list(map(int, input().split()))
if N >= K:
  N = N % K
print(min(N, max(N-K, K-N)))

Solution (Python):
def solve():
    N,K = [int(i) for i in input().split()]
    min_N = N
    if min_N > K:
        min_N = min_N - (min_N//K)*K
    min_N = min(min_N, abs(min_N-K))
    print(min_N)

if __name__ == "__main__":
    solve()

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

N = N % K

if N < K - N:
  print(N)
else:
  print(K - N)

Solution (Python):
def repint(n, k):
	if n % k == 0:
		return 0
	res = abs((n % k) - k)
	if (n % k) > res:
		return res
	return (n % k)
n, k = map(int, input().split())
print(repint(n, k))

Solution (Python):
n, k = map(int, input().split())
x = n % k
x = min(x, k - x)
n = x
print(n)