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):
nk=list(map(int,input().split(' ')))
nk[0]=nk[0]%nk[1]
while(True):
    if abs(nk[0]-nk[1])<nk[0]:
        nk[0]=abs(nk[0]-nk[1])
    else:
        break
print(nk[0])

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

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

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


if __name__ == '__main__':
    main()

Solution (Python):
n,k = map(int,input().split())
i = 0
while(i==0):
  
  if n>k:
    a = n%k
    if a<n:
      n = a
  else:
    a = abs(k-n)
    if a<n:
      n = a
    else:
      print(n)
      exit()

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

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

Solution (Python):


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

th = .5 * K

n = N % K
while n > th:
    n = abs(n - K)

print(n)

Solution (Python):
import bisect,collections,copy,itertools,math,numpy,string
def I(): return int(input())
def S(): return input()
def LI(): return list(map(int,input().split()))
def LS(): return list(input().split())

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

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

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

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

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

Solution (Python):
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
rr = range


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols
    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])
    return ret


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_map_as_int(H):
    '''
    
    '''
    ret = []
    for _ in range(H):
        ret.append([1 if s == '
        
        
    return ret


MOD = 10**9 + 7
INF = 2**31  

from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  
from fractions import gcd


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


N, K = read_ints()





x = N
x1 = x % K  
x2 = x1 - K  


print(min(x1, -x2))

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

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

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

Solution (Python):

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

n = n % k
f = True
ans = n
while f:
    t = abs(n-k)
    if t < ans:
        ans = t
        n = t
    else:
        f = False

print(ans)

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

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%K

if N == 0:
  ans = 0
elif K-N <= N:
  ans = K-N
else:
  ans = N
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_base = N % K

print(min(ans_base, K - ans_base))

Solution (Python):



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



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

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

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



print(min(n,k-n))

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

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

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

left = n%k
right = k

dif = abs(k - n%k)

if left >= dif : 
    print(dif)
else : 
    print(left)

Solution (Python):


def input_multiple_number():
    return map(int, input().split())

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


N,K = input_multiple_number()


N = N%K

N = min(N,K-N)

print(N)

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

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

N = N%K

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

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

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

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

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

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

print(min((x[0]%x[1]),(abs((x[0]%x[1])-x[1]))))

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

Solution (Python):

import math
import itertools
import statistics

import collections


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

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

Solution (Python):
import sys
from math import sqrt
from collections import Counter, defaultdict

input = sys.stdin.readline


def I():
    return int(input())


def MI():
    return map(int, input().split())


def LI():
    return list(MI())


def LIN(n: int):
    return [I() for _ in range(n)]


inf = float("inf")
mod = 10 ** 9 + 7


def main():
    n, k = MI()
    mod = n % k
    if mod > abs(mod - k):
        print(abs(mod - k))
    else:
        print(mod)

    pass


if __name__ == "__main__":
    main()

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

Solution (Python):


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

if __name__ == "__main__":
    main()

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

def ope(i, j):
    return abs(i - j)

appeared = set()

if n > k:
    c = n // k
    n -= k * c

while True:
    result = ope(n, k)
    if result in appeared:
        print(min(appeared))
        exit(0)
    else:
        appeared.add(result)
        n = result

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

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


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

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

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

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

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

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

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

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

n, k = nm()
x=n//k
n=n-(x-1)*k
while n>k/2:
    n=abs(n-k)
print(n)

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

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

Solution (Python):
import sys
sys.setrecursionlimit(700000)

def s_in():
    return input()

def n_in():
    return int(input())

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

def print_l(l):
    print(' '.join(map(str, l)))

class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res

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

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

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

Solution (Python):

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

n_af = n % k
if k - n_af <= n_af:
    n_af = k - n_af

print(n_af)

Solution (Python):
import string
def num():
    return int(input())
def nums():
    return list(map(int,input().split()))

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

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

	ans = 0

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

	print(ans)

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

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

Solution (Python):
import sys
from fractions import gcd
def input(): return sys.stdin.readline().strip()


def main():
    N, K = map(int, input().split())
    if N % K == 0:
        print(0)
    else:
        d = gcd(N, K)
        n = N // d
        k = K // d
        cand = n % k
        print(d * min(cand, k - cand))
    

if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.readline
inputt = lambda: range(int(input()))
inputi = lambda: map(int, input().split())
inputl = lambda: list(inputi())
from math import *
from heapq import *
from bisect import *
from functools import reduce, lru_cache
from collections import Counter, defaultdict
from builtins import *

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

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

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

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

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

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

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


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





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

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

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

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

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

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

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

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

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

if(N <= K):
    ans = min(abs(N-K), abs(abs(N-K)-K))
    print(ans)
else:
    mod = N%K
    ans = min(abs(mod-K), mod)
    print(ans)

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

if n == 0:
    print(0)
    exit()

q, r = divmod(n, k)

if r > k - r:
    print(k-r)
else:
    print(r)

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

if n%k == 0:
    print(0)
    exit()

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

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

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

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)

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

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

print(min(a,b))

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

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

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

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

Solution (Python):
import sys
import math
import fractions
from collections import deque
from collections import defaultdict
sys.setrecursionlimit(10**7)

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

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

print(ans)

Solution (Python):
import sys

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

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

if __name__ ==  '__main__':
    main()

Solution (Python):
import sys
import math

from collections import defaultdict
from collections import deque
def load(vtype=int):
    return vtype(input().strip())
def load_list(seplator=" ", vtype=int):
    return [vtype(v) for v in input().strip().split(seplator)]
def exit():
    import sys
    sys.exit(0)
def perm_sub(li, used):
    if len(li) == len(used):
        return [deque()]
    k = []
    for i in range(len(li)):
        if i in used:
            continue
        used.add(i)
        sub_list = perm_sub(li, used)
        for sub in sub_list:
            sub.appendleft(li[i])
        k.extend(sub_list)
        used.discard(i)
    return k
def perm_li(li):
    return perm_sub(li, set())
def perm_n(n):
    return perm_sub(list(range(n)), set())
def join_i(li, sep=""):
    return sep.join([str(e) for e in li])
def li2n(li):
    n, base = 0, 1
    for i in range(len(li)-1, -1, -1):
        n += li[i] * base
        base *= 10
    return n
def sli2ili(li):
    return [int(s) for s in li] 
def prime_list(n):
    li = [i for i in range(2, n+1)]
    for i in range(len(li)):
        if li[i] >= int(math.sqrt(n)):
            break
        if li[i] == -1:
            continue
        for j in range(i+1, len(li)):
            if li[j] % li[i] == 0:
                li[j] = -1
    return [n for n in li if n != -1]
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while True:
        r = a % b
        if r == 0: return b
        a, b = b, r
def lcm(a, b):
    return int(a * b / gcd(a,b))
def all_subset(li):
    s = []
    n = len(li)
    for bit in range(0, (1<<(n+1))):
        ss = set()
        for i in range(0, n):
            if (bit & (1 << i)):
                ss.add(li[i])
        s.append(ss)
    return s
def factorial(n):
    if n == 1:
        return 1
    return n * factorial(n-1)
def mCn(m, n):
    def factorial_ntimes(m, n):
        r = 1
        while n:
            r *= m
            m -= 1
            n -= 1
        return r
    return int(factorial_ntimes(m, n) / factorial(n))


n, k = load_list()


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

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

Solution (Python):
from collections import deque
from collections import Counter
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect




from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7

def main():
  n, k = map(int, readline().split())
  a = n//k
  ans = min(n-a*k, abs(n-(a+1)*k))
  print(ans)
  
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())

min = n % k

if min > abs(min - k):
  min = abs(min - k)

print(min)

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

X=A%B
print(min(X,B-X))

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

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

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

temp_res = N % K

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

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

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):
import sys
input = sys.stdin.readline
N, K = list(map(int, input().split()))

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

x = N
if N > K:
	x = N % K

min_x = x
h = {}
h[x] = 1
while True:
	x = abs(x-K)
	min_x = min(min_x, x)
	if x in h:
		break
	h[x] = 1

print(min_x)

Solution (Python):
n,x=map(int,input().split())
temp1=n//x
temp2=n%x

temp3=x-temp2

ans=min(temp2,temp3)

print(ans)

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

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

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

r=N%K

print(min(r,K-r))

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

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

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

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

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

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

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

if N<=K:
    print(min(N, K-N))
else:
    M = N-(N-1)//K*K
    print(min(M, K-M))

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

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

Solution (Python):

import sys


def solve(N: int, K: int):
    if N % K == 0:
        print(0)
        return
    print(min(N, abs((N % K) - K)))
    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())
x = (n+k)%k
print(min(x, k-x))

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

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

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

Solution (Python):
def solve(n,m):
  p = n % m
  ans = min(p,m-p)
  return ans
l = [int(x) for x in input().split(' ')]
print(solve(l[0],l[1]))

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

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

Solution (Python):
N,M=[int(x) for x in input().split()]
print(min(abs(N-N//M*M),abs(N-(N//M+1)*M)))

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

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

Solution (Python):
def main():
    N, K = map(int, input().split())
    
    div = N // K
    min1 = abs(N - div*K)
    min2 = abs(N - (div+1)*K)

    print(min(min1,min2))

if __name__ == '__main__':
    main()

Solution (Python):

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

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

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

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

elif N==K:
    print(0)
else:
    amari = N%K
    if amari>K/2:
        print(K-amari)
    else:
        print(amari)

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

Solution (Python):
n, k = map(int, input().split())
print(min(n, abs(n%k), abs((-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())
t = n//k
n = abs(n - (k*t))
while n > 0:
    if n <= abs(n-k):
        print(n)
        exit(0)
    n = abs(n-k)
print(n)

Solution (Python):

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

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

Solution (Python):
n,k = map(int,input().split())
if n > k:
    n = n % k
m = n
lst = [n]
for i in range(1,100001):
    n = abs(n-k)
    m = min(m,n)
    lst.append(n)
    if i >= 2:
        if lst[i-2] == n:
            print(m)
            exit()

Solution (Python):


iim = lambda: map(int, input().rstrip().split())

def resolve():
    N, K = iim()

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

if __name__ == "__main__":
    resolve()

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

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

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

d = N % K

print(min(d,K-d))

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

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

if N>K:
    tmp = (N-K)//K + 1
    N -= tmp*K

while N>s:
    N = abs(K-N)
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())

tmp = N % K
m = abs(tmp - K)
if m > tmp:
  m = tmp
print(m)

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

while True:
    anss=abs(val-k)
    if val<=anss:
        print(val)
        break
    val=anss

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

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

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

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

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

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

if n > k:
    n = n % k

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

a = N % K
b = K - a

print(min(a, b))

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

if n % k == 0:
    print("0")
else:
    sub = n % k
    sub2 = n // k
    sub3 = (k * (sub2 + 1)) - n
    print(min(sub, sub3))

Solution (Python):










"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""
  
def main():
  n,k = map(int,input().split())
  WANTS = n//k
  EX = n - k*WANTS
  dic = {}
  mi_ = float("inf")
  mi_ = min(mi_,EX)
  
  while True:
    if str(EX) in dic:
      break
    else:
      dic[str(EX)] = 1
      EX = abs(EX-k)
      mi_ = min(mi_,EX)
  print(mi_)
  
  
if __name__ == '__main__':
  main()

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

if __name__ == '__main__':
    main()

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

x = N % K

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

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

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

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

Solution (Python):
def main():
    N, K = [int(x) for x in input().split()]
    m1 = N % K
    m2 = abs(m1 - K)
    if m1 < m2:
        print(m1)
    else:
        print(m2)

if __name__ == "__main__":
    main()

Solution (Python):

import sys
import math
import numpy as np
import itertools



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

a = n % k
c = k - a

print(min(a,c))

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

positive_sol = n % k

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

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

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





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

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

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

print(res)

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

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

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

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

Solution (Python):

"""
Created on Sat Apr  4 21:12:41 2020

@author: Kanaru Sato
"""

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

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

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

if n > k:
    n = k + n % k

res = 10**19
while res > n:
    res = n
    n = abs(n - k)

print(res)

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

waru = N // K
N = N - waru*K

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

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

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

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

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

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

print(tmp)

Solution (Python):


import sys
import math
import time

import collections
from collections import deque
from collections import Counter
import queue
import copy
import bisect
import heapq








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

while True:
  if(abs(temp-K)<temp):
    temp = abs(temp-K)
  else:
    break

print(temp)

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


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

Solution (Python):
n,k=map(int,input().split())
t = 1
c = n
b = n//k
c = abs(c-k*b)
while t:
	c_p=c
	c = abs(c-k)
	if c > c_p:
		t = 0
print(c_p)

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

N %= K

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

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

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

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

Solution (Python):
n,k=map(int,input().split())
l=n//k
n_d=abs(n-k*l)
n_u=abs(n-k*(l+1))
if n_u>n_d:
    print(n_d)
else:
    print(n_u)

Solution (Python):
N,K=list(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())
kk=n//k
N=n-kk*k
print(min(N,abs(N-k)))

Solution (Python):


MOD = 10**9
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors

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

amari1 = N//K
amari2 = N//K+1
ans = min(abs(N-amari1*K), abs(N-amari2*K))
print(ans)

Solution (Python):

"""
Created on Sat Apr  4 20:49:45 2020

@author: naoki
"""

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

amari1 = N % K
amari2 = abs(K - amari1)


print(min(amari1, amari2))

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

Solution (Python):
import math

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

mod = N % K

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

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

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

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


print(a)

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


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

print(ans)

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

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

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

print(min(a,b))

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

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

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

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

ans=N-K*(N//K)
while True:
    ans=abs(K-ans)
    if ans<=K//2:
        break
print(ans)

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

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

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

N = N % K
if K - N < N:
  N = K - N

print(N)

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

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

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

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

res = N

if N == K:
    print(0)

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

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

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

Solution (Python):
import sys
import numpy as np


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



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

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%k

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

print(ans)