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())
print(min(n%k,abs(k*(n//k+1)-n)))

Solution (Python):



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


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


print(ans)

Solution (Python):
def main():
    from builtins import int, map, list, print, min
    import sys
    sys.setrecursionlimit(10 ** 6)

    input = sys.stdin.readline
    input_list = (lambda: input().rstrip().split())
    input_number = (lambda: int(input()))
    input_number_list = (lambda: list(map(int, input_list())))

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



if __name__ == '__main__':
    main()

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

while True:
  if N >= K:
    N = N%K
  else:
    if abs(N-K) < N: N = abs(N-K)
    print(N)
    sys.exit()

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

m_1 = n % k
m_2 = k - m_1
print(min(m_1, m_2))

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

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



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

print(min(ans1, ans2))

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

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

Solution (Python):

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



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

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

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

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
input_ints = lambda: list(map(int, input().split()))
N, K = input_ints()

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

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

m = N % K

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

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

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

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

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

Solution (Python):
n,k=(int(i) for i in input().split())
print(min(n%k,(((n//k)+1)*k)-n))

Solution (Python):




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

    times = n // k
    ans = n - k * times

    if ans < k-ans:
        print(ans)
    else:
        print(k-ans)

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

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())
if n >= k:
    n = n%k
if k-n < n:
    n = k-n
print(n)

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

if N%K == 0:
    print(0)
else:
    print(min(abs(N%K - K), N, N%K))

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

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

if N == K:
    print(0)
elif N < K:
    if K - N > N:
        print(N)
    else:
        print(K-N)
else:
    if N % K == 0:
        print(0)
    else:
        p = N % K
        print(min(p, K-p))

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 = abs(k-a)
print(min(a,b))

Solution (Python):




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


rem = n % k
ans = min(abs(rem - k), rem)


print(ans)

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

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

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


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

Solution (Python):
def solve():
    n, k = map(int, input().split())
    if n % k == 0:
        print(0)
    else:
        if n - k > 0:
            r = n % k
            d = abs(r - k)
            print(min(r, d))
        else:
            d = abs(n - k)
            print(min(n, d))


if __name__ == '__main__':
    solve()

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:
    print(0)
else:
    if abs(k - n%k) < n%k:
        print(abs(k - n%k))
    else:
        print(n%k)

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

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

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

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

Solution (Python):

from sys import stdin, stdout


def read():
    x0 = stdin.readline().rstrip()
    return x0


def __main__():

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

    if n == 0 or k == 1:
        r = 0
    else:
        r = n % k
        if abs(r-k) < r:
            r = abs(r-k)

    print(r)
    
__main__()

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

ans = n % k

i = False

if ans == 0:
  i = True

while not i:
  if ans > abs(ans - k):
    ans = abs(ans - k)
  else:
    i = True

print(ans)

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

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

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

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

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

Solution (Python):
def op(x,K):
    return min(abs(x), abs(x-K))


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

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

Solution (Python):
n, k = map(int, input().split())
x = n%k
ans = []
for i in range(2):
    x = abs(x-k)
    ans.append(x)
print(min(ans))

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

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

Solution (Python):
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial
from bisect import bisect_left 

sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
def pri(x): print('\n'.join(map(str, x)))

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

d = defaultdict(int)
res = N
x = N
while 1:
    x = res%K
    y = abs(res-K)
    
    res = min(res, x, y)
    d[res] += 1
    
    if d[res] > 10:
        break
print(res)

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

if n > k:
  count = n // k
  res1 = n - (k * count)
  res2 = res1 - k
  res2 = max(res2, -res2)
  res = min(res1, res2)
  print(res)
  
else:
  res1 = n
  res2 = res1 - k
  res2 = max(res2, -res2)
  res = min(res1, res2)
  print(res)

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

v = N

while abs(v - K) < v:
  if v > K:
    v = v % K
    
  v = min(v, abs(v - K))
  
print(v)

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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)


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

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


main()

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

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

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

print(N)

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

if N > K:
    N = N % K



print(min(N, K - N))

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

Solution (Python):
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N, K = map(int, input().split())

    x = N % K
    y = abs(x - K)
    print(min(x, y))


main()

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

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

aa = abs(n-k*waru)
bb = abs(aa-k)
if aa<bb:
    print(aa)
else:
    print(bb)

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

d=abs(N-K)
c=N%K

if abs(N-K)>N:
  print(N)
else:
  print(min(N%K,K-N%K))

Solution (Python):
"""
NTC here
"""
import sys
inp = sys.stdin.readline
def input(): return inp().strip()




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


 
def main():
    T = 1
    while T:
        T-=1
        n, k = lin()
        n = n%k
        ans = n
        done = set()
        while n not in done:
            n = abs(n-k)
            ans = min(n, ans)
            done.add(n)
        print(ans)


 
 
 
 
 
main()

Solution (Python):
import math
n,k = map(int,input().strip().split())
f = 1
x = n
while True:
    if x>k:
        poss = x//k
        x = x-poss*k
    if abs(x-k)<x:
        x = abs(x-k)
        f = 1
    else:
        break
print(x)

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

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

x=N%K

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

Solution (Python):


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

NpK = N%K
if 2 * (NpK) < K:
  print(NpK)
else:
  print(K-NpK)

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

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

Solution (Python):
def solve(n, k):
    mod = n % k
    if mod == 0:
        print(0)
    elif abs(mod - k) < mod:
        print(abs(mod - k))
    else:
        print(mod)

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

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

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())
if K > N :
  print(min(abs(N-K),N))
else:
  first = N // K 
  first_1 = N % K 
  print(min(first_1, K - first_1))

Solution (Python):
n, k = tuple(map(lambda x: int(x), input().split(" ")))
if k < n:
    n = n % k

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

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

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

ans=N%K

if ans==0:
  print(0)
elif ans>abs(ans-K):
  print(abs(ans-K))
else:
  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())
plus_min = n % k

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

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

Solution (Python):

MOD = 10 ** 9 + 7
INFI = 10**10

import math

import itertools
import heapq


import copy
from functools import lru_cache

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default




def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)


def main():
    n,k=map(int,input().split())
    a=[]
    a.append(n)
    ans=n
    while True:
        if n>=k:
            dif=n-k
            kai=dif//k
            n-=kai*k
            if n>=k:
                n=abs(n-k)
        if n>k:
            tempn=n
            aftn=abs(n-k)
            if tempn<=aftn:
                break
            else:
                n=aftn
                break
        else:
            tempn = n
            aftn = abs(n - k)
            if tempn <= aftn:
                break
            else:
                n=aftn
                break
    print(n)


if __name__ == "__main__":

    main()

Solution (Python):

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

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

Solution (Python):


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)

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

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

if N%K == 0:
  ans = 0
elif N%K != 0:
  _ = N%K
  __ = (K-_)
  ans = min(_,__)
  
print(ans)

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

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

p = n % k
q = k - p

if p >= q:
  print(q)
else:
  print(p)

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

Solution (Python):
import math
nk=list(map(int, input().split()))
n=nk[0]
k=nk[1]
r=n%k
rd=-(r-k)
answer=min(r,rd)
print(answer)

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

ans1 = N%K
ans2 = -1 * (N % (-1 * K))

if ans1	< ans2:
    print(ans1)
else:
    print(ans2)

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

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

Solution (Python):

def main():
  
  
  
  
  
  
  
  
  
  
  
  
  import sys
  sys.setrecursionlimit(10**6)
  readline = sys.stdin.readline
  ints = lambda: map(int, readline().split())
  floats = lambda: map(float, readline().split())
  pyes = lambda: print("Yes")
  pno = lambda: print("No")
  lli = lambda I, J: [[0] * J for _ in range(I)]
  mod = 10**9+7

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

if __name__ == '__main__':
  main()

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

a = N % K
b = abs(a - K)
ans = min(a, b)
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())
a=n%k
if n<k:
    print(min(n,abs(n-k)))
    exit()

if n%k==0:
    print(0)
else:
    print(min(k-a,a))

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

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

if N>K:
    num = N//K
    N = N - K*num
else:
    pass

spr = K//2 if K%2==0 else K//2+1
ans = N if N<spr else K-N
print(ans)

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

if a > b:
    tmp = a % b
    tmp2 = -1 * (tmp - b)
    if tmp > tmp2:
        print(tmp2)
    else:
        print(tmp)
elif a < b:
    tmp = -1 * (a - b)
    if a < tmp:
        print(a)
    else:
        print(tmp)
elif a == b:
    print(0)

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

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

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

n = N % K
n_another = K - n

ans = min(n, n_another)
print(ans)

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

temp1=a%b
temp2=abs(a%b-b)
print(min(temp1,temp2))

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

n %= k
temp = {n}
ans = n

while True:
    n = abs(n - k)
    if n in temp:
        break
    temp.add(n)
    

    if n < ans:
        ans = n

print(ans)

Solution (Python):
inp = list(map(lambda x: int(x), input().split(" ")))
N = inp[0]
K = inp[1]

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

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


if n >= k:
  tmp = n % k
else:
  tmp = n
if tmp > k/2:
  print(int(tmp-k)*(-1))
else:
  print(tmp)

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

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

if n < k:
    a = n
else:
    a = n % k

print(min([k - a, a]))

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



if __name__ == "__main__":
    resolve()

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


b = N % K
while 2*b > K:
    b = K - b

print(b)

Solution (Python):
from sys import stdin

def main():

    input = stdin.readline

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

    ans_num = N % K

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



if __name__ == "__main__":
    main()

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

over_n = N - (N//K) * K
under_n = abs(over_n-K)
print(min(over_n, under_n))

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

s = n % k

if s > k // 2:
    s = k - s

print(s)

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

Solution (Python):
n,k=map(int,input().split())
if (n-k)!=0:
	print(min([abs(n%k), abs(abs(n%k)-k)]))
else:
	print("0")

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

Solution (Python):
n, k = map(int, input().split())
n = n % k
flag = False
while not flag:
  if abs(n - k) < n:
    n = abs(n - k)
  else:
    flag = True
print(n)

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

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

print(min(ans, ans1, ans2))

Solution (Python):
import sys

N, K = map(int, sys.stdin.readline().strip().split())

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

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

a = n

n %= k

b = n

n = abs(n-k) % k

c = n

print(min(a,b,c))

Solution (Python):
n, k = list(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=abs(a-k)
c=min(a,b)
print(c)

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

Solution (Python):


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

a = N // K

x = (N - a * K)

if abs(x - K) < x:
  print(abs(x - K))
else:
  print(x)

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

x = a % b
y = b - x

if x == 0 :
    print(0)
    
else:
    print(min([x,y]))

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

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

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

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

if(n==k):
    print(0)
elif(n>k):
    n=n-k*(n//k)
    print(min(n,abs(n-k)))
else:
    print(min(n,abs(n-k)))

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

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

Solution (Python):

import sys
import itertools
import fractions
import copy
import bisect


from operator import itemgetter
from sys import stdin

from operator import mul
from functools import reduce
from collections import Counter


N=3
M=3
i=0
j=0
k=0
n=3
r=1
a=[0]*5
b=[]






"1行1つの整数を入力を取得し、整数と取得する"

N, K=map(int, input().split())
q=N//K
ans1=abs(N-K*(q+1))
ans2=abs(N-K*(q))
ans3=abs(N-K*(q-1))
print(min(ans1, ans2, ans3))


"12 21 332 とか入力する時に使う"
"1行に複数の整数の入力を取得し、整数として扱う"

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

for i in number_list:
    print(number_list[i-1])

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'''
x=[]
y=[]
for i in range(N):
    x1, y1=[int(i) for i in input().split()]
    x.append(x1)
    y.append(y1)
print(x)
print(y)
"複数行に2数値を入力する形式 x座標とy座標を入力するイメージ"
'''

'''
mixlist=[]
for i in range(N):
    a,b=input().split()
    mixlist.append((int(a),b))
print(mixlist)
"複数行にintとstrを複合して入力するやつ，今回はリスト一つで処理している"
'''

'''



"正方行列にstr型の値を入力"
'''




'''
入力
1 2
4 5
7 8
出力結果
[[1, 2], [4, 5], [7, 8]]
'''
"列数に関して自由度の高いint型の値を入力するタイプの行列"


'''
table = [[int(i) for i in input().split()] for m in range(m)]

print(type(N))
print(N)
print(type(M))
print(M)

print(type(table))
print(table)
'''











'''
入力
1234
出力
1
[[1, 2, 3, 4]]
'''




'''
word_list= input().split(" ")
print(word_list[0])
"連続文字列の入力"
"qw er ty とかの入力に使う"
"入力すると空白で区切ったところでlistの番号が与えられる"
'''



'''
A, B, C=stdin.readline().rstrip().split()
print(A)
"リストではなく独立したstr型を入れるなら以下のやり方でOK"
'''




"aという変数に入っているものを大文字にして出力"

















'''
def cmb(n,r):
    
    r= min(n-r,r)
    
    if r == 0: return 1
    over = reduce(mul, range(n, n-r, -1))
    
    
    

    under = reduce(mul, range(1, r+1))
    
    
    
    

    return over // under
    
    
    
    
a = cmb(n, r)

'''

'''




for i in a.most_common(n):print(i)





 


'''









'''
bisect.bisect_left(a, x, lo=0, h=len(a))
引数
a: ソート済みリスト
x: 挿入したい値
lo: 探索範囲の下限
hi: 探索範囲の上限
(lo, hiはスライスと同様の指定方法)
bisect_leftはソートされたリストaに対して順序を保ったままxを挿入できる箇所を探索します。leftが示す通り、aにすでにxが存在している場合は、挿入箇所は既存のxよりも左側になります。また、lo, hiを指定することで探索範囲を絞り込むことも可能です。デフォルトはaの全体が探索対象です。
'''

Solution (Python):
n, k = map(int, input().split())
x = n
r = 0
x = n - (n // k) * k
pre_x = x
while True:
	if k == 1 or n == 0:
		break
	pre_x = x
	x = abs(x - k)
	if pre_x < x:
		r = pre_x
		break
print(r)

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

Solution (Python):
from sys import *
N, K = map(int, stdin.readline().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:
  print(min(k-n, n))
else:
  print(min(n%k, abs(k-n%k)))

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

Solution (Python):


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

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

if N >= K:
    div = N // K
    diff = N - div * K
    print(min(diff, abs(diff-K)))
else:
    print(min(N, abs(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())

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

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

Solution (Python):
aa=list(map(int,input().split()))
n=aa[0]
k=aa[1]
minn=n%k
print(min(n%k,abs((n%k)-k)))

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

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

Solution (Python):
N,K = (int(x) for x in input().split())
while(N>abs(N-K)):
    if(N > K):
        a = N//K
        N -= K *a
    else:
        N = abs(N-K)
print(N)

Solution (Python):
d = input().split(" ")
n = int(d[0])
k = int(d[1])
a = n % k
b = k - a
print(min([a, b]))

Solution (Python):
def solve(N, K):
    tmp = N % K

    if abs(tmp - K) < tmp:
        return abs(tmp - K)
    else:
        return tmp

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

if __name__ == "__main__":
    main()

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

plus_N = n - ((n//k)*k) 
minus_N = abs(n - ((n//k+1)*k))

print(min(plus_N,minus_N))

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

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

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

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

n-=k*(n//k)
ans=n
while True:
  tmp=abs(n-k)
  if tmp>=ans:
    break
  else:
    ans=min(ans,tmp)
    n=tmp
print(ans)

Solution (Python):
n,k = map(int,input().split())
x = n%k
mn = x
for _ in range(2): 
    x = abs(x - k)
    mn = min(x, mn)
print(mn)

Solution (Python):
N, K = [int(s) for s in input().split()]
if N > K:
  print(min(N%K, abs(K-N%K)))
elif N == K:
  print(0)
else:
  print(min(K-N, N))

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

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


if __name__ == '__main__':
    main()

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

N = N % K

print(min(N, K - N))

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

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

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

a = n - k
d = a % k

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

Solution (Python):
for _ in range(1):
	
	n,k = map(int,input().split())
	
	
	print(min(-n%k,n%k))

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

cnt = n // k
a = n - k * cnt
b = abs(n - k * (cnt + 1))

print(min(a,b))

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

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

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

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

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

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

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

Solution (Python):
import sys
input = sys.stdin.readline
N, K = (int(i) for i in input().split())



nc = N
for i in range(nc):
    if N < K:
        break
    else:
        N = N-(N//K)*K

for i in range(nc):
    if abs(N-K) < N:
        N = abs(N-K)
    else:
        break



print(N)

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

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


if __name__ == '__main__':
    main()

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

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

rem = n % k 

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

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

if N >= K:
    tmp = N%K
    print(min(tmp, abs(tmp-K)))
else:
    print(min(N, K-N))

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

Solution (Python):
from sys import stdin
from sys import setrecursionlimit
from collections import Counter, deque, defaultdict
setrecursionlimit(1000000)


INF = int(1e10)

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

def main():
    from builtins import int, map
    N, K = map(int, input().split())
    Norg = N

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

    if N > K:
        N = N % K

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

    
    



if __name__ == '__main__':
    main()

Solution (Python):
def c1 ( N , K ) :
	if N == K :
		x = 0 ;
	elif K > N : 
		x = min ( N , abs ( N - K ) ) ; 
	else :
		x = c1 ( N % K , K ) ;
	return x ;

N , K   = map( int , input().strip().split(" ") ) ;
print ( c1 ( N , K ) ) ;

Solution (Python):
from sys import stdin


def main():
    N, K = [int(x) for x in stdin.readline().rstrip().split()]
    x = N % K
    print(min(x, abs(x - K)))


if __name__ == "__main__":
    main()

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

if k <= n:
  num = n//k
  n = n - k*num

temp = k - n
if n <= temp:
  ans = n
elif temp < n:
  ans = temp

print(ans)

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

m0 = n - (n // k) * k

m1 = abs(m0 - k)
m2 = abs(m1 - k)

print(min(m0,m1,m2))

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

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