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

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

Solution (Python):
import math  
from typing import Dict, List, Optional, Tuple, Union  


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

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

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


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

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

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

Solution (Python):

n,k=map(int,input().split())
min_s=10**18

while True:
    if n<k:
        n=abs(n-k)
    n=n%k
    if n>min_s:
        break
    min_s=min(n,min_s)
    if min_s==0:
        break
print(min_s)

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

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

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

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

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

  
print(answer)

Solution (Python):

from math import gcd
n,k=map(int,input().split())
print(min(n%k,k-n%k))

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


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

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

print(x)

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

Solution (Python):
n, k = map(int, open(0).read().split())

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

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

Solution (Python):
def abc161_C(N,K):
    if K == 1:
        return print(0)
    Min = 0
    N = N%K
    diff_list = [N]
    while 1:
        diff = abs(N - K)
        N = diff
        if diff in diff_list:
            return print(min(diff_list))
        elif diff < K/2:
            diff_list.append(diff)
            return print(min(diff_list))
        else:
            diff_list.append(diff)
    
N,K = map(int,input().split())
abc161_C(N,K)

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

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

print(N)

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

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

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

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

Solution (Python):

from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


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


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


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

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


def main():
    solve()


if __name__ == '__main__':
    main()

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

while x > K/2:
    x = abs(K-x)

print(x)

Solution (Python):









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

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

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

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

s, a = divmod(n, k)

t = k - a

if (a < t):
    print(a)
else:
    print(t)

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

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

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

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

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

t = N % K

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

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

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


print(ans)

Solution (Python):
n,k = map(int,input().split())
n %= k
while True:
  if abs(n-k) < n:
    n = abs(n-k)
  else:
    break
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())
m = N % K
print(min(m,K - m))

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

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

print(ans)

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

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

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

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

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

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

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

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

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

if n <= k :
    print(min(n, k-n))
    exit(0)
if n>k:
    n = n%k
    print(min(n, k-n))

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

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

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



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

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

ans = 0
while True:
    if n >= k:
        n = n % k
        if n == 0:
            break
    else:
        if abs(n-k) < n:
            n = abs(n-k)
        break

print(n)

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

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

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

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

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(abs(N-(K*(N//K))),abs(N-(K*((N//K)+1)))))

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

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

n = N//K
m1 = N-K*n
m2 = abs(N-K*(n+1))
ans = min(m1, m2)
print(ans)

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

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

print(min(x, y))

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

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

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

x = n // k
n -= (x-1) * k
nmin = n
for _ in range(3):
   n = abs(n - k)
   nmin = min(n, nmin)
print(nmin)

Solution (Python):
import math

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

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

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

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

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

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

n = abs(n - k)

if k == n:
    print(0)
    sys.exit()
elif k > n:
    print(min(n, k-n))
    sys.exit()
else:
    while n > k:
        n %= k
    print(min(n%k, abs(n-k)))

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

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

a = N % K
b = K - a

if a < b:
  print(a)
else:
  print(b)

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

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

Ans=min(amari,tmp)
print(Ans)

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

seen = {N}
while True:
    if N >= K:
        N = N % K
    else:
        N = K - N
    if N in seen:
        break
    seen.add(N)
print(min(seen))

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

Solution (Python):
def main():
    n, k = map(int, input().split())
    x = n // k
    x2 = x + 1
    xr = abs(n - x * k)
    x2r = abs(n - x2 * k)
    r = min(xr, x2r)
    print(r)

if __name__ == '__main__':
    main()

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

while True:
  if n > k:
    n = n - (n//k)*k
  num = abs(n-k)
  if num > n:
    print(n)
    exit(0)
  else:
    n = num

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

if N >= K:
  A = N//K
  B = N - A*K
  print(min([B, abs(B-K)]))
else:
  if K >= 2*N:
    print(N)
  else:
    print(abs(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()))
t = n%k
print(min(abs(k-t), abs(k-(k-t)),n))

Solution (Python):
n,K=map(int,open(0).read().split())

if n%K==0:
  print(0)
elif n%K!=0:
  a = n%K
  if a>abs(a-K):
    a = abs(a-K)
    print(a)
  elif a<=abs(a-K):
      print(a)

Solution (Python):
n,K=map(int,open(0).read().split())

if n%K==0:
  print(0)
elif n%K!=0:
  a = n%K
  if a>abs(a-K):
    a = abs(a-K)
    print(a)
  elif a<=abs(a-K):
      print(a)

Solution (Python):
n, k = map(int, input().split())
'''
while True:
  tar = abs(n - k)
  if (tar)
'''

diff = n - k
div = n // k

if (diff == 0):
    ans = 0
elif (diff < 0):
    ans = min(n, abs(n - k))
else:
    temp = n - div * k
    ans = min(temp, abs(temp - k))


print(ans)

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

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

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

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

Solution (Python):
n,k=map(int,input().split())
n=n%k
flag=n
li=[n]
for i in range(10**10):
  n=abs(n-k)
  li.append(n)
  if n==flag:
    break
  
print(min(li))

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

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

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

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

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

m = N
t = -1
s = -1
N = N%K
if N < m:
    m = N
while(t!=N):
    t = s
    s = N
    N = abs(N-K)
    if N < m:
        m = N
print(m)

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

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

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

ans1=abs(N-N//K*K)
ans2=abs(N-(N//K+1)*K)

print(min(ans1,ans2))

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 = 0

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

print(ans)

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

print(t)

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

print(min(ans1, ans2))

Solution (Python):
import math
def action(x,K):
    x=abs(x-K)
    return x
N,K=map(int,input().split())
if N>K:
    N=N-math.floor((N-K)/K)*K
n=N
while True:
    N=action(n,K)
    if N>n:
        print(n)
        break
    n=N

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

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

a = abs(N%K)
b = abs(a-K)
print(min(a,b))

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

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




from __future__ import division, print_function 

import sys
import os
from collections import Counter, deque, defaultdict
import itertools
import math
import io


"""Uncomment modules according to your need"""













if sys.version_info[0] < 3:
    input = raw_input
    range = xrange

    filter = itertools.ifilter
    map = itertools.imap
    zip = itertools.izip



"""value of mod"""
MOD = 998244353
mod = 10**9 + 7

"""Uncomment next 4 lines if doing huge recursion"""




def prepare_factorial(mod=mod):
    """ returns two lists, factorial and inverse factorial modulo argument by default 10**9 +7 """
    
    fact = [1]
    for i in range(1, 200005):
        fact.append((fact[-1] * i) % mod)

    ifact = [0] * 200005
    ifact[200004] = pow(fact[200004], mod - 2, mod)
    for i in range(200004, 0, -1):
        ifact[i - 1] = (i * ifact[i]) % mod

    return fact, ifact

def modinv(n, p):
    """ returns N inverse modulo p """
    return pow(n, p - 2, p)

def ncr(n, r,  fact, ifact):
    """ takes 4 arguments: n , r and factorial and inverse factorial lists"""
    t = (fact[n] * (ifact[r]*ifact[n-r]) % MOD)% MOD
    return t


def get_n(Sum):    
    """this function returns the maximum n for which Summation(n) <= Sum"""
    ans = (-1 + sqrt(1 + 8*Sum))//2
    return ans

def sieve(n):
    """ returns a list of prime numbers till n """
    if n < 2: return list()
    prime = [True for _ in range(n + 1)]
    p = 3
    while p * p <= n:
        if prime[p]:
            for i in range(p * 2, n + 1, p):
                prime[i] = False
        p += 2
    r = [2]
    for p in range(3, n + 1, 2):
        if prime[p]:
            r.append(p)
    return r



def divs(n, start=1):
    """ returns a list of all divisors till n """
    divisors = []
    for i in range(start, int(math.sqrt(n) + 1)):
        if n % i == 0:
            if n / i == i:
                divisors.append(i)
            else:
                divisors.extend([i, n // i])
    return divisors



def divn(n, primes):
    """ returns the number of divisors, two arguments n and the sieve till n """
    divs_number = 1
    for i in primes:
        if n == 1:
            return divs_number
        t = 1
        while n % i == 0:
            t += 1
            n //= i
        divs_number *= t



def lrfind(d, x, default=-1):
    """ Takes 2 arguments an iterable and an element. returns a tuple (firstoccurence,lastoccurence) -1 if not found """
    left = right = -1
    for i in range(len(d)):
        if d[i] == x:
            if left == -1: left = i
            right = i
    if left == -1:
        return default, default
    else:
        return left, right

def gcd(x, y): 
    """ returns greatest common divisor of x and y """
    while y:
        x, y = y, x % y
    return x

def check_sorted(a):
    ''' returns True/False '''
    for i in range(len(a)-1):
        if a[i]>a[i+1]:
            return False
    return True
def ceil(n, k=1): return n // k + (n % k != 0) 
def input(): return sys.stdin.readline().strip()
def ii(): return int(input()) 
def mi(): return map(int, input().split()) 
def li(): return list(map(int, input().split())) 
def lw(): return input().split() 
def lcm(a, b): return abs(a * b) // gcd(a, b) 
def prr(a, sep=' ', end='\n'): print(sep.join(map(str, a)), end=end) 
def dd(): return defaultdict(int) 
def ddl(): return defaultdict(list) 
def write(s): return sys.stdout.write(s)













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




""" -------- Python 2 and 3 footer by Pajenegod and c1729 ---------"""

py2 = round(0.5)
if py2:
    from future_builtins import ascii, filter, hex, map, oct, zip
    range = xrange

import os, sys
from io import IOBase, BytesIO

BUFSIZE = 8192

class FastIO(BytesIO):
    newlines = 0

    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.writable = "x" in file.mode or "w" in file.mode
        self.write = super(FastIO, self).write if self.writable else None

    def _fill(self):
        s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
        self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
        return s

    def read(self):
        while self._fill(): pass
        return super(FastIO, self).read()

    def readline(self):
        while self.newlines == 0:
            s = self._fill();
            self.newlines = s.count(b"\n") + (not s)
        self.newlines -= 1
        return super(FastIO, self).readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.getvalue())
            self.truncate(0), self.seek(0)


class IOWrapper(IOBase):

    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        if py2:
            self.write = self.buffer.write
            self.read = self.buffer.read
            self.readline = self.buffer.readline
        else:
            self.write = lambda s: self.buffer.write(s.encode('ascii'))
            self.read = lambda: self.buffer.read().decode('ascii')
            self.readline = lambda: self.buffer.readline().decode('ascii')


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')



""" main function"""

if __name__ == '__main__':
    main()

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

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



if __name__ == "__main__":
    main()

Solution (Python):
N, K = map(int, input().split())
N = N%K
history = []
min_num = 0
while True:
  if (N==0):
    min_num = N
    break
  elif N in history:
    min_num = min(history)
    break 
  history.append(N)
  N = abs(N-K)

print(min_num)

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

if __name__ == '__main__':
    main()

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

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

Solution (Python):
N,K = map(int,input().split())
a = []
t = N%K
tt = abs(t-K)
a.append(t)
a.append(tt)

print(min(a))

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

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

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

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

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

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

Solution (Python):

from decimal import *
from itertools import permutations, combinations,combinations_with_replacement,product
s = input().split()
t = [int(p) for p in s]

N = t[0]
K = t[1]





a = N % K
b = K - a


print(min(a,b))

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

print(min(ans1, ans2))

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

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

print(ans)

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

if n%k > k/2:
  print(k - n%k)
else:
  print(n%k)

Solution (Python):
import sys
n,k=map(int,input().split())
if n==0 or n%k==0:
    print(0)
    sys.exit()
n=n%k
tmp=abs(n-k)
while tmp<n:
    n=tmp
    tmp=abs(n-k)
print(n)

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

if __name__ == "__main__":
    main()

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

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

Solution (Python):















mod = int(1e9+7)

INF = 1<<50

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

def main():
    n,k = readInt()
    ans = n % k
    ans = min(ans,abs(ans-k))
    print(ans)
    return

if __name__=='__main__':
    main()

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())
a = (N // K) + 1

ans1 = -(N-K*a)
ans2 = N - (K*(N//K))
print(min(ans1,ans2))

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

a = n%k

num = min(a, abs(a-k))

print(num)

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

if n >= k:
  n %= k

print (min(k-n,n))

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

ans1 = n%k
ans2 = k - n%k

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

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

print(min(a, b))

Solution (Python):
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do():
    n,k=INTM()
    a=n%k
    b=k-a
    print(min(a,b))
    
if __name__ == '__main__':
    do()

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

a = n % k
b = abs(a-k)

print(min(a, b))

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

Solution (Python):
N,K  = (int(X) for X in 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())
print(min(abs(n%k-k),n%k))

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

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

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

Solution (Python):


import sys
input=sys.stdin.readline

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

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

Solution (Python):

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

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

if N%K == 0:
    print(0)
else:
    N = N%K
    if abs(N-K) > N:
        print(N)
    else:
        print(abs(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())

D1 = N//K
D2 = N//K+1

Ans = min(abs(N-D1*K) ,abs(N-D2*K))
print(Ans)

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

D = N%K
print(min([abs(D-K), D]))

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

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

print(ans)

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

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

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

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

Solution (Python):
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007



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

isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

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

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

answer = N % K
x = K - answer

if answer > x:
    answer = x

print(answer)

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

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

Solution (Python):
import sys
N,M=map(int,input().split())
if M==0:
  print(N)
  sys.exit()
c=N%M
d=abs(c-M)
print(min(c,d))

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 > k/2:
    print(k-(n % k))
else:
    print(n % k)

Solution (Python):




import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter











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



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

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

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

Solution (Python):
n, k = map(int, input().split())
if n%k == 0:
    print(0)
else:
    a = n//k
    if a != 0:
        n -= a*k
        b = abs(k - n)
        print(min(b, abs(k - b)))
    else:
        print(min(n, abs(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())
modp = n % k
modm = abs(modp - k)
if modp <= modm:
    print(modp)
else:
    print(modm)

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

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

Solution (Python):
N, K = map(int, input().split(' '))
if N%K == 0:
    print(0)
else:
    tmp = [abs(N - N//K*K)]
    while True:
        if abs(tmp[-1] - K) in tmp:
            break
        else:
            tmp.append(abs(tmp[-1] - K))
    print(min(tmp))

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


resolve()

Solution (Python):

import sys
from decimal import *

def solve(N: int, K: int):
    N = Decimal(str(N))
    K = Decimal(str(K))
    print(abs(N - K*round(N/K)))



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

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

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

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

Solution (Python):
import sys


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

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

main()

Solution (Python):

import sys, re
from decimal import *
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, floor, trunc
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx):   return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
    temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
    toret = {}
    for tup in temp:
        toret[tup[0]] = tup[1]
    return toret
def sortDictWithKey(passedDic):
    return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

n,k = MAP()

if(k>=n):
    if(k>2*n):
        ans = n
    else:
        ans = k-n
else:
    rem = n%k
    quot = n//k

    ans = n-quot*k

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

print(ans)

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

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

print(ans)

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

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