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


from queue import Queue
from queue import LifoQueue as Stack
from math import sqrt, floor, ceil, log2
from fractions import gcd
from itertools import permutations, combinations
from operator import itemgetter
from functools import cmp_to_key


INF=1001001001
__MOD__=(10**9)+7
yn = 'YNeos'
judge = False
cnt = 0
ans = None


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

def intinput():
    return int(input())

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

def lineinputs(func=intinput):
    datas = []
    while True:
        try:
            datas.append(func())
        except EOFError:
            break
    return datas

class ModInt():
    def __init__(self, x):
        self.__x = (x % __MOD__)
    def __add__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x + other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x + other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __sub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x - other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x - other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __mul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __truediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __pow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(self.__x, other.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(self.__x, other.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")
    def __modinv(self, m=__MOD__):
        a = self.__x
        if a == 0:
            raise ZeroDivisionError()
        if gcd(a, m) != 1:
            raise Exception("%sの逆数は求まりません。" % a)
        b, u, v = m, 1, 0
        while b != 0:
            t = a//b;
            a -= t*b
            a, b = b, a
            u -= t * v
            u, v = v, u
        u %= m
        if u < 0:
            u += m
        return u
    def __int__(self):
        return self.__x



if __name__ == '__main__':
    N, K = mulinputs()
    d = (N+K-1)//K

    
    ans = int(abs(N-d*K))
    if N < ans:
        print(N)
    else:
        print(ans)

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

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

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

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

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

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

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

while True:
    if abs(N-K) < N:
        if N > K:
            if N % K < N:
                N = N % K
            else:
                break
        else:
            if K % N < N:
                N = K % N
            else:
                break
    else:
        break

print(N)

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

r = n % k

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

Solution (Python):

import sys


def solve(N: int, K: int):
    q = N // K
    r = N % K
    count = q 
    if abs(r - K) < r:
        r = abs(r - K)
    print(r)
    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):



def solve(*args: str) -> str:
    n, k = map(int, args[0].split())

    return str(min(n % k, abs(n % k-k)))


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

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

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

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

if N < K:
    print(min(abs(N-K),N))
    sys.exit()
if N % K == 0:
    print(0)
else:
    print(min(abs((N%K)-K),N%K))

Solution (Python):


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

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

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

Solution (Python):
N, K = list(map(int, input().split()))
L1 = N//K
L2 = L1 + 1
print(min([N-L1*K, L2*K-N]))

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

a=N%K

b=abs(K-a)

print(min(a,b))

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

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

Solution (Python):

nk = [int(x) for x in input().split()]
n = nk[0]
k = nk[1]

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

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

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

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

Solution (Python):
if __name__ == "__main__":
  _n, _k = map(int, input().split())
  candidate = _n % _k
  ans = min(candidate, abs(candidate-_k))
  print(int(ans))

Solution (Python):




import sys
input = sys.stdin.readline



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

    res = N % K

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

    print(res)



main()

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

min_val = result
while True:
    result = abs(result - k)
    if (min_val <= result):
        break
    min_val = result
print(min_val)

Solution (Python):
import math

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

now = N
waru = N // K

A = abs(N-(K*(waru+1)))
B = abs(N-(K*(waru)))

if A >= B:
    print(B)
else:
    print(A)

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

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

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

i_i = lambda: int(i_s())
i_l = lambda: list(map(int, stdin.readline().split()))
i_s = lambda: stdin.readline().rstrip()

N, K = i_l()

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

N = N % K
arr = []
while not N in arr[:-1]:
    N = abs(N-K)
    arr.append(N)

print(min(arr))

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

Solution (Python):
def resolve():
    N, K = map(int, input().split())
    div, mod = divmod(N, K)
    N = N - K * div
    print(min(abs(N-K), N))

resolve()

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

print(ans)

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

Solution (Python):
import sys
import numpy as np
import itertools
import collections
from collections import deque

sys.setrecursionlimit(10**6)
readline = sys.stdin.readline

h = itertools.combinations_with_replacement
p = itertools.permutations

def main():
    
    N, K = map(int, input().split())
    rest = N%K
    reverse = abs(rest - K)
    ans = min(N, rest, reverse)

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):



input_line = input()
Line = input_line.split()
Line = [int(s) for s in Line]

a = Line[0] % Line[1]

b = y = abs(a- Line[1])



print(min(a,b))

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


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

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

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

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

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

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

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

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

print(ans)

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)

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

answer = N % K
while True:
    new = abs(answer - K)
    if new > answer:
        break
    answer = new
print(answer)

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

Solution (Python):



import sys
sys.setrecursionlimit(10000000)


dxdy=((1,0),(0,1))

def pin(type=int):
    return map(type,input().rstrip().split())

def resolve():
    N,K=pin()
    x=N%K
    print(min(K-x,x))
"""         


"""
"""
お前カッコ閉じ,るの忘れてるだろ
"""
import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """7 4"""
        output = """1"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """2 6"""
        output = """2"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """1000000000000000000 1"""
        output = """0"""
        self.assertIO(input, output)


if __name__ == "__main__":
    resolve()

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

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

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

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

Solution (Python):
import sys
N, K = map(int, sys.stdin.readline().split())
a = N % K
if a < K/2:
  print(a)
else:
  print(K-a)

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

    while True:
        tmp = abs(tmp-k)
        if tmp < min:
            min = tmp
        else:
            break
    print(min)
    
    
if __name__ == "__main__":
    resolve()

Solution (Python):
def gcd(x, y): 
  
   while(y): 
       x, y = y, x % y 
  
   return x 
a,b=map(int,input().split())
if a==0:
    print(0)
else:
    if a%b==0:
        print(0)
    else:
        x=a%b 
        while True:
            if x>abs(x-b):
                x=abs(x-b)
            else:
                break 
        print(x)

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

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

if 2*n > k:
  print(k-n)
else:
  print(n)

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

rem = n % k 

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

Solution (Python):
import numpy as np

NK=np.array(input().split(" "),dtype=np.int64)

N=NK[0]
K=NK[1]

x=N%K

abs_x=abs(x-K)

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

Solution (Python):


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

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

if __name__ == '__main__':
    main()

Solution (Python):
import sys
import copy
import math
import bisect
import pprint
import bisect
from functools import reduce
from copy import deepcopy
from collections import deque
from decimal import *


def lcm(x, y):
    return (x * y) // math.gcd(x, y)


if __name__ == '__main__':
    b, c = map(int, input().split())
    a =abs(b% c)
    
    if abs(a-c) < a:
        print(abs(a-c))
    else:
        print(a)

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

n_1 = N - (K*T)
n_2 = N - (K*(T+1))

print(min(abs(n_1),abs(n_2)))

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

a = n % k
while True:
    t = abs(a - k)
    if t < a:
        a = t
    else:
        break
print(a)

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

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

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

Solution (Python):
N, K = map(int, input().split())
N = N % K
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())
    if abs(N-((N // K)*K)) < abs(N-(((N // K)+1)*K)):
        print(abs(N-((N // K)*K)))
    else:
        print(abs(N-(((N // K)+1)*K)))
    return 0


if __name__ == '__main__':
    main()

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

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

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

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

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

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

print(min(x, amari))

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

items = set([N])

def check(x, K):
	if x == K:
		x = 0
	elif x < K:
		x = K - x
	else:
		x = x % K
	return x

x = N
while True:
	new_x = check(x, K)
	if new_x in items:
		break
	items.add(new_x)
	x = new_x


print(min(items))

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

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

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

lis=[]

cur=N%K
mini=N%K
while True:
    cur=abs(cur-K)

    if cur in lis:
        break
    else:
        lis.append(cur)
        mini=min((mini,cur))


print(mini)

Solution (Python):

n, k = map(int, input().split())
if n > k:
    n = int(n%k)

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

Solution (Python):
def rep(n, k):
    if n < k:
        if n <= k-n:
            print(n)
        else:
            print(k-n)
    elif n == k:
        print(0)
    else:
        r = n % k
        if r <= k-r:
            print(r)
        else:
            print(k-r)

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

rep(n, k)

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

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

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

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

print(N)

Solution (Python):


n, k = map(int, input().split())
while(True):
    if n - k > 0:
        if n // k > 0:
            n = n - k * (n // k)
        else:
            if n - k < n:
                n = n - k
            else:
                break
    else:
        if abs(n - k) < n:
            n = abs(n - k)
        else:
            break

print(n)

Solution (Python):

import sys


def solve(N: int, K: int):
    if abs(N-K) > N:
        print(N)
    else:
        diff = max(N, K) - min(N, K)
        k = diff//min(N, K)
        MAX = max(N, K)-k*min(N, K)
        MIN = min(N, K)
        a1 = MAX
        a2 = abs(MAX-MIN)
        a3 = abs(a2 - MIN)
        print(min(a1, a2, a3))



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


if __name__ == '__main__':
    main()

Solution (Python):
import sys

def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getlist():
    return list(map(int, input().split()))
def getlist2():
    return [int(x) * 100000 for x in input().split()]
import math
import bisect
import heapq
from decimal import Decimal

MOD = 10**9 + 7
INF = 10**15
def main():
    n,k = getlist()
    mo = n % k
    print(min(mo, abs(mo - k)))

if __name__ == '__main__':
    main()

"""
9999
3

2916
"""

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

Solution (Python):
import sys
input = sys.stdin.readline
def main():
    N,K = map(int, input().split())
    ans = min(N%K,abs(N%K-K))
    print(ans)
if __name__ == "__main__":
    main()

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

if N > K:
  N = N % K

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

Solution (Python):

import sys


def solve(N: int, K: int):
    x = N%K
    while True:
        tmp = x
        x = min(abs(x - K), x)
        if tmp == x:
            break
    print(x)



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):
x = input().split()
N = int(x[0])
K = int(x[1])

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

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

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

rem = n % k
rem_minus = (rem - k) * - 1

if rem >= rem_minus:
  print(rem_minus)
else:
  print(rem)

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

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

Solution (Python):

from fractions import gcd
N, K = map(int, input().split())
print(min((N%K), abs(N%K - K)))

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

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

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

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

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

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


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


N, K = inpl()

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

Solution (Python):
import sys
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]



N, K = mi()

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

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

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

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

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

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

    tmp = n % k
    return min([tmp, n, k, k - tmp])


if __name__ == "__main__":
    result = solve()
    print(result)

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

Solution (Python):
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

n,k = iim()
n = 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):
import heapq
from collections import defaultdict, deque
from math import ceil, factorial
from fractions import gcd
import sys
sys.setrecursionlimit(10**7)
INF = float('inf')
MOD = 10**9+7

si = lambda: input().strip()
ii = lambda: int(input())
mii = lambda: map(int, input().split())
lmii = lambda: list(map(int, input().split()))
smii = lambda: sorted(map(int, input().split()))


N, K = mii()

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

d, m = divmod(N, K)

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

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

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

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

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

y = n // x

print(min(n - y * x, abs(n - x * (y+1))))

Solution (Python):
n,k = map(int, input().split())
if n == 0 or k == 1:
  print(0)
elif n < (k + 1)//2:
  print(n)
elif n < k:
  print(k - n)
elif n == k or n % k == 0:
  print(0)
else:
  print((k * (n//k + 1)) % n)

Solution (Python):
nk=input().split()
N=int(nk[0])
K=int(nk[1])
a=N%K
b=K-a
if a>b :
  print(b)
else :
  print(a)

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

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

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

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

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

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

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

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

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

ans = N % K

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

Solution (Python):



import sys

sys.setrecursionlimit(10**9)

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

mod = N % K

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

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

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

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

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

print(answer)

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

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

x = N % K
num = [x, K - x]

print(min(num))

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

amari = N % K

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

Solution (Python):
from sys import stdin


def get_result(data):
    N, K = data
    q, mod = divmod(N, K)
    ans = min([mod, abs(mod-K)])
    return ans


if __name__ == '__main__':
    data = list(map(int, stdin.readline().rstrip().split(' ')))
    result = get_result(data)
    print(result)

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

    while abs(n - k) < n:
        if n > k:
            n -= k * (n // k)
        else:
            n = abs(n - k)

    print(n)

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

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


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

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

Solution (Python):
import math
from decimal import Decimal

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

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

check_set = set()
number = N
if number > K:
    number = number % K

while number not in check_set:
    check_set.add(number)
    number = abs(K - number)

print(min(check_set))

Solution (Python):
def listint():
    a = input().split()
    for i in range(len(a)):
        a[i] = int(a[i])
    return a
def main(n,k):
    z = n % k
    if z > k-z:
        return k-z
    else:
        return z
n,k = listint()
print(main(n,k))

Solution (Python):
input = raw_input

n, k = map(int, input().split(" "))
n1 = n % k
n2 = k - n1
out = min(n1, n2)
print(out)

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

Solution (Python):
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True 
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

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

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

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

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

if k == 1:
    print('0')
else:
    n = n % k
    print(min(n, abs(n - k), k))

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

    ans = n % k

    if abs(ans - k) % k < ans:
        ans = abs(ans - k) % k

    print(ans)

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

Solution (Python):


import sys
import math
import collections
import itertools
INF = 10 ** 10
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def gcd(x, y) : return y if x % y == 0 else gcd(y, x % y)
def lcm(x, y) : return (x * y) // gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def PL(L) : print(*L, sep="\n")

N, K = LI()

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

Solution (Python):
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

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

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


if __name__ == "__main__":
    main()

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

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

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

n_k = n % k

ans = min(n_k, k - n_k)

print(ans)

Solution (Python):

N, K = list(map(int, input().split()))
mod = N%K
min_ = mod
while True:
    tmp = abs(min_ - K)
    if (tmp < min_):
        min_ = tmp
    else:
        break
print(min_)

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

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

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

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

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

print(min(b, c))

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

n = n % k

while flag:
  if min(abs(n-k),abs(n)) != n:
    n = min(abs(n-k),abs(n))
  else:
    print(n)
    flag = 0

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

if N > K:
    N = N % K

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

Solution (Python):
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n,k = mi()

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

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

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

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

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

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

Solution (Python):

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

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

c=a[0]%a[1]
if c > abs(c-a[1]):
  print(abs(c-a[1]))
else:
  print(c)

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

while (N >= 0):
  N -= K
  if (abs(N) < ans):
    ans = abs(N)

print(ans)

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

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

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

ans_ = n % k

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

print(ans)

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

Solution (Python):
N,K = [int(i) for i in input().split()]
a = N//K
amari = N - K*a
abs_K = abs(amari-K)
if abs_K < amari:
    print(abs_K)
else:
    print(amari)

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

A = N//K

B = abs(N-(A-1)*K)
C = abs(N-A*K)
D = abs(N-(A+1)*K)

print(min(B,C,D))

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