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 = list(map(int, input().split()))

counter = 0
x_min = n
x = n
while not counter == 3:
    x = x % k
    if x_min > x:
        x_min = x
        counter = 1
    elif x_min == x:
        counter = counter + 1
    x = k - x

print(x_min)

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

Solution (Python):
import math
n,k = map(int,input().split())
q,mod = divmod(n,k)
print(min(mod,k-mod))

Solution (Python):
N, K = map(int, input().split())
if N > K:
    N = N % K
if 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())

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

Solution (Python):

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

if K == 1:
  print(0)
else:
  a = N % K
  if a > K-a:
    print(K-a)
  else:
    print(a)

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

Solution (Python):
a,b=map(int,input().split())
k=a%b
if k<=b/2:
    print(k)
else:
    print(b-k)

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

ans1 = n%k
ans2 = abs(k - n%k)

print(min(ans1,ans2))

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

Solution (Python):
if __name__ == "__main__":
    n, k = map(int, input().split())
    tmp = n // k
    print(min(n - tmp*k, abs(n-(tmp+1)*k)))

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

n=n%k

while True:
    old=n
    new=abs(n-k)
    if new < old:
        n=new
    else:
        break
print(n)

Solution (Python):
N,K=(int(x) for x in input().split())
tmp=N%K
print(min(tmp,abs(K-tmp)))

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

shou,lis = n//k,[]
if shou!=0: n=n%(shou*k)
for i in range(10):
  n=abs(n-k)
  lis.append(n)
print(min(lis))

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

if N == K:
    print(0)
elif N < K:
    print(min(N, K-N))
else:
    N = K + 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)
else:
    num1 = N % K
    num2 = abs(K - num1)
    print(min(num1, num2))

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

c = n//k
plus = abs(n - c*k)
minus = abs(n - (c+1)*k)

print(min(plus,minus))

Solution (Python):
import sys
input = sys.stdin.readline

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

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

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

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

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

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

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

Solution (Python):

import sys
input = sys.stdin.buffer.readline


from collections import defaultdict

con = 10 ** 9 + 7

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


def main():
	N, K = getlist()
	ans = min(N % K, (K - N) % K)
	print(ans)


if __name__ == '__main__':
	main()

Solution (Python):
import sys
import math
from collections import deque
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def S(): return sys.stdin.readline()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h,w): return [[i for j in range(w)] for k in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)

MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**6+10)


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

Solution (Python):
n,k=[int(x) for x in input().split()]
a=n%k
b=k-a
if a>b:
  print(b)
else:
  print(a)

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



x = n % k
y = k - x

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

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

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

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

a = N % K

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

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

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

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

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

Solution (Python):
import sys;input=lambda:sys.stdin.readline().rstrip();aint=lambda:int(input());ints=lambda:list(map(int,input().split()))
import math;floor,ceil=math.floor,math.ceil
from collections import deque
Yes=lambda b:print('Yes')if b else print('No');YES=lambda b:print('YES')if b else print('NO')
is_even=lambda x:True if x%2==0 else False

def main():
    n,k = ints()
    print(min(n%k,k-n%k))

if __name__ == '__main__':
    main()

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

a=N//K

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

if p>m:
  print(m)
else:
  print(p)

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

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

print(ans)

Solution (Python):
N,K=(int(x) for x in input().split())
x=N%K
if x>(K/2):x-=K
print(abs(x))

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

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

x = N % K
y = K - x 

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

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

Solution (Python):
M, K = map(int,input().split())
M = M % K
switch = 1001001001
while True:
    if switch < 0:
        break
    switch = M-K
    M = min(M,abs(M-K))
print(int(M))

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

Solution (Python):
s = input()
n = int(s.split()[0])
k = int(s.split()[1])

current = n
absV = 0
if current - k > 100000000:
    current = current % k
while True:
    absV = abs(current - k)
    if current < absV:
        print(current)
        exit()
    else:
        current = absV

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

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

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

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

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

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

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

amari = N % K
z_amari = abs(amari - K)

print(min([amari, z_amari]))

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

j = N//K

if N-j*K > -(N-(j+1)*K):
    print(-(N-(j+1)*K))
else:
    print(N-j*K)

Solution (Python):
inputnum = input().split()
N = int(inputnum[0])
K = int(inputnum[1])

Surplus = N % K
K_Surplus = K - Surplus

if Surplus <= K_Surplus:
  print(Surplus)
else:
  print(K_Surplus)

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

Solution (Python):

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

  v = n % k

  print(min(v, k - v))

main()

Solution (Python):
import sys

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

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

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

Solution (Python):






import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')






def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]


def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr


def lcm(a, b):
    return a * b // gcd (a, b)





N,K = inputIL()

r = N%K

r2 = K-r

print(min(r,r2))

Solution (Python):
N , K = (int(x) for x in input().split())

remin = N%K

ans = min(remin,K-remin)

print(ans)

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

Solution (Python):
def sovle(x, y):
    while(x>=y):
        x=x-x//y*y
    if(x>y-x):
        return y-x
    else:return x



if __name__ == '__main__':
    nums = list(map(int, input().split(" ")))
    print(sovle(nums[0], nums[1]))

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

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

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

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

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

Solution (Python):
'''
最大の繰り返し回数はN // K + 1
上記の回数回して結果のリストを作成し、
最小値を出力する

興味があるのは、差が正から負へ変わった瞬間の２つの値のみ

NをKで割った商をKにかけた数を引いた数とそれにKを一回足した数
上記２つの数の内小さい方を出力すればよい
'''
import math

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

num1 = abs(N - math.ceil(N // K) * K)
num2 = abs(num1 - K)

print(num1) if num1 < num2 else print(num2)

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())
t = n % k
print(min(t, k - t))

Solution (Python):
from sys import stdin
import math

N, K = list(map(int, stdin.readline().rstrip().split()))

mod = N % K

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

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()


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

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

if __name__ == '__main__':
  main()

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

Solution (Python):
N,K = map(int, input().split())
if N == 0:
    print(0)
else:
    for i in range(18):
        while True:
            if N > K*10**(18-i):
                N = N-(K*10**(18-i))
            else:
                break

    x0 = N 
    x1 = abs(x0-K) 
    while True:
        if x0 < x1:
            print(x0)
            break
        else:
            x0,x1 = x1,abs(x0-K)
            continue

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

m = n % k
m1 = k - m

if m > m1:
  ans = m1
else:
  ans = m
print(ans)

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

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

Solution (Python):
N=[int(x) for x in input().split()]
if N[0]%N[1]==0:
   print("0")

elif abs(N[0]%N[1]- N[1])<N[0]:
     print(abs(N[0]%N[1]- N[1]))

else:
     print(N[0])

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

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

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

Solution (Python):
import sys
import math
s = input().split(' ')
n, k = int(s[0]), int(s[1])
p = [n, n % k, abs((n % k) - k)]
res = p[0]
for x in p:
	res = min(res, x)
print (res)

Solution (Python):



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

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

ans=0

if n<k:
  ans=min(n,abs(n-k))
  print(ans)
else:
  ans=min(int(n%k),abs(int(n%k)-k))
  print(ans)

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

while True:
    if B>abs(B-A):
         B=abs(B-A)
    else:
         break

print(B)

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

s = n // k
a = n % k
if a == 0:
    ans = 0
else:
    ans = min(abs(n - (s * k)), abs(n - ((s + 1) * k)))
print(ans)

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

amari = N % K
nokori = abs(amari-K)

print(min(amari, nokori))

Solution (Python):
N, K = map(int, input().split())
ans = N
x = abs(N - K * max(N // K, 1))
while ans > x:
    ans = x
    x = abs(x - K * max(x // K, 1))
print(ans)

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

Solution (Python):

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


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

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

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

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

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

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

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

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

Solution (Python):
inp=[int(x) for x in input().split()]
n,k=inp[0],inp[1]
if n>k:
  print(min(n%k,k-n%k))
elif n<k:
  print(min(n,k-n))
else:
  print(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, abs(k-n%k)))

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

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

Solution (Python):
N,K = map(int,input().split())
r = N % K
if r < -(r - K):
    print(r)
else:
    print(-(r - 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())
n=n-(n//k)*k
print(min(n,k-n))

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

Solution (Python):



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()

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())
print(min((n % k), (-n % k)))

Solution (Python):
a, b = map(int, input().split())

m = (a - a%b) / b
print(min(a%b, b - a%b))

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

Solution (Python):


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

t_1 = N % K
t_2 = K - t_1

if t_1 > t_2:
    print(t_2)
else:
    print(t_1)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    n, k = map(int, rl().split())
    ans = min(n % k, abs(n % k - k))
    print(ans)


if __name__ == '__main__':
    solve()

Solution (Python):


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

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

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

Solution (Python):


nim, mike = map(int,input().split())
print(abs(min(nim%mike, mike-nim%mike)))

Solution (Python):
n,k=map(int,input().split())
if n<=k:
    print(min(n,k-n))
else:
    p=n//k
    print(min(n-p*k,(p+1)*k-n))

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

loop = [0]

for i in loop:
  if num[0] < num[1]:
    difference = abs(num[0] - num[1])
    if difference > num[0]:
      break
    else:
      num[0] = difference
      break
  elif num[1] == 1:
    num[0] = 0
    break
  elif num[1] == 0:
    break
  if i == 0:
    num[0] = num[0] % num[1]
  num[0] = abs(num[0] - num[1])
  loop.append(i+1)

print(num[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, abs(N % K - K)))

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

a = int(N/K)
b = N % K
c = abs(b-K)


print(min(b,c))

Solution (Python):
N,K = map(int, input().split())
import math
a = N % K;
b = K - a;

print(a if a <= b else b)

Solution (Python):
import numpy as np


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

time = N // K
min_ = N- time*K
min = N- (time+1)*K
print (min_ if min_<np.abs(min) else np.abs(min))

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

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

mod = N % K
mod2 = abs(mod - K)

print(min(mod, mod2))

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

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

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

Solution (Python):


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

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

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

Solution (Python):
import math

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

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

print(ans)

Solution (Python):
n, k = map(int, input().split())
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,abs(n%k-k)))

Solution (Python):
import sys
input = sys.stdin.readline

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

num = n // m
n -= m * num
print(min(n, m - n))

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())

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

elif N<K and N<=K-N:
    print(N)

elif N<K and N>K-N:
    print(K-N)

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

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

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

if d<m:
    print(d)
else:
    print(m)

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

Solution (Python):
N,K=map(int,input().split())
if K==1:
  if N==0:
    print("1")
    exit()
  else:
    print("0")
    exit()
if N > K :
  N-=(N//K)*K
if abs(K-N) < N:
  print(abs(K-N))
else:
  print(N)

Solution (Python):
N,K=map(int,input().split())
if K==1:
  if N==0:
    print("1")
    exit()
  else:
    print("0")
    exit()
if N > K :
  N-=(N//K)*K
if abs(K-N) < N:
  print(abs(K-N))
else:
  print(N)

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

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

def abs(a):
    if a>=0:
        return a
    else:
        return -a

m=n%k

while True:
    b=m
    m=abs(m-k)
    if b<=m:
        print(b)
        break

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

Solution (Python):
def main():
    N, K = map(int, input().split()) 
    a,b = divmod(N, K)
    print(min(b, K - b))
if __name__ == "__main__":
    main()

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

n = x%k

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

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

po=(N//K+1)*K-N
ne=N%K

print(min(po,ne))

Solution (Python):
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]:
    dif += (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))

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

Solution (Python):
n=[int(i) for i in input().split()]
a=n[0]%n[1]
if a<abs(a-n[1]):
    print(a)
else:
    print(abs(a-n[1]))

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

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

Solution (Python):
n,k = map(int,input().split())
if n > k:
    a = n // k
    n = n - a * 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 = list(map(int, input().split()))
x = N % K
print(min(x, K-x))

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

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

Solution (Python):
from collections import defaultdict
def main():
    n,k=map(int,input().split())
    n%=k
    print(min(n,k-n))
main()

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

Solution (Python):
import sys

A, B = map(int, sys.stdin.readline().split())

r = A%B

print(min(r, abs(r - B)))

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

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())
ans = min(k-(n % k), n % k)
print(ans)

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

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

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
from bisect import bisect_left 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

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

Solution (Python):
il = input().split()
N = int(il[0])
K = int(il[1])

old = N
new = 0

a = N // K
b = N % K
c = abs(N - (K * (a+1)))

if (b > c):
    print(c)
else:
    print(b)

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

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())
import numpy as np
ans1 = N % K
ans2 = np.abs(ans1 - K)

if ans1 < ans2:
  ans = ans1
else:
  ans = ans2
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())

inst = n%k
print(min([inst,k-inst]))

Solution (Python):


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

a = n%k 
ans = min([a, k-a])
print(ans)

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

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

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

remainder = N // K
N = N - K*remainder

if N == 0:
    print(N)
    exit();

abc = abs(N-K)

for i in range(10**18):
    N = abc
    abc = abs(N-K)
    if N + abc == K and abc < N:
        break;

print(abc)

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

Solution (Python):
a=list(map(int,input().split()))
n=a[0]
k=a[1]
if n>=k:
    if n%k==0:
        print("0")
    else:
        if (n%k)<=(k-(n%k)):
            print(n%k)
        else:
            print(k-(n%k))
else:
    c=k-n
    if c<=n:
        print(c)
    else:
        print(n)

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

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

N = N % K

ans_candidate_list = [N]
while True:
    N = abs(N - K)
    
    
    if N not in ans_candidate_list:
        ans_candidate_list.append(N)
    else:
        break

print(sorted(ans_candidate_list)[0])

Solution (Python):
i = [int(x) for x in input().split()]
n = i[0]
k = i[1]

t = n % k
t2 = k -t

print(min(t, t2))

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

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

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

mod = N % K

if mod > abs(mod - K):
  print(abs(mod - K))
else:
  print(mod)

Solution (Python):
def main():
    s = input().split()
    n = int(s[0])
    k = int(s[1])
    x = n % k
    if x > k/2:
        x = abs(x - k)
    print(x)

main()

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

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

Solution (Python):
import math
N, K = map(int, input().split())
N = N%K
while True:
    if abs(N-K) < N:
        N = abs(N-K)
    else:
        print(N)
        break

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

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

else:
    n = n - (n//k)*k
    if n<= k/2:
        print(n)
    else:
        print(abs(n-k))

Solution (Python):
import math

def main():
    s = input().split()
    n = int(s[0])
    k = int(s[1])
    
    if n<=(k/2):
        print(n)
        return
    
    n = n - (n//k)*k
    if n<= k/2:
        print(n)
    else:
        print(abs(n-k))

main()

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

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

elif N == K:
    print(0)

else:
    N = K + (N - K) % K
    N = abs(N - K)

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

    elif N == K:
        print(0)

Solution (Python):
import os

def main():
	n,k = map(int, input().split())
	mod = n % k
	min_num = abs(mod-k)
	if min_num < mod:
		print(min_num)
	else:
		print(mod)


if __name__ == "__main__":
	main()

Solution (Python):

N,M= map(int, input().split())
amari= N % M
if(amari<=(M/2)):
    print(amari)
else:
   amari = abs(amari-M)
   print(amari)

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

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

one = n
while n > k:
    n = n % k
    one = n
two = abs(n-k)

print(min(one,two))

Solution (Python):
import math

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


def check(K,N ):
    min = 0
    abs_num = 0

    N = N%K

    OLD_ABS =N
    NEW_ABS = abs(K-N)

    while NEW_ABS < OLD_ABS:
        OLD_ABS = N 
        N = NEW_ABS 
        NEW_ABS =abs(K-N)
    
    print(N)     
            
K,N = nyu()
check(K,N)

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

def fun(n, k):
    a = n%k
    return min(a, k-a)

print(fun(n, k))

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

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)


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

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

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

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

def replace(a,b):
    if a < abs(a - b):
        return a
    else:
        return replace(abs(a - b), b)

if K == 0:
    print(N)
elif N % K == 0:
    print(0)
else:
    print(replace(N%K, K))

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

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

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

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

N %= K

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

print(N)

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

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

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

Solution (Python):
import numpy as np
N, K  = map(int, input().split())

m = np.mod(N,K)

while m > abs (m-K):
  m = abs (m-K)

print(m)

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

Solution (Python):
N,K=map(int,input().split())
x=N
if N>K:
    x=N%K
inLoop=True
while inLoop:
    x=abs(K-x)
    if x<=K/2:
        inLoop=False
print(x)

Solution (Python):
n,k = map(int,input().split())
p = n % k
q = k - p
ans = 0
if p < q:
    ans = p
else:
    ans = q
print(ans)

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

Solution (Python):
X = input().split()
N = int(X[0])
K = int(X[1])

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

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


while True:
  if abs(x-k) < x:
    x = abs(x-k)
  else:
    break
print(x)

Solution (Python):
n,k =[int(x) for x in input().split()]

pre_value = n+1
tmp = n
a = n // k 
tmp = n - a*k 
while tmp < pre_value:
    pre_value = tmp
    tmp = abs( tmp - k)
    
print(pre_value)