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

if n % k == 0:
    ans = 0
    print(ans)
    exit()
else:
    n = n % k
    for i in range(10000000000000000000):
        res = abs(n - k)
        if n <= res:
            ans = n
            print(ans)
            exit()
        else:
            n = res

Solution (Python):
list=list(map(int,input().split()))
x=list[0]
k=list[1]
if list[0]>=list[1]:
  x=x%k
z=abs(k-x)
if z >=x:
  print(x)
else:
  print(z)

Solution (Python):
import sys

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

if K == 1:
    print(0)
    sys.exit()
if N == K:
    print(0)
    sys.exit()

d = []

d.append(N % K)
while True:
    d.append(abs(d[-1] - K))
    if d[-1] > d[-2]:
        print(d[-2])
        sys.exit()

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

Solution (Python):
N, K = [int(i) for i in input().split()]
if N > K:
    if N % K == 0:
        print(0)
    else:
        print(K % (N % K))
elif N <= K:
    if K < 2 * N:
        print(K - N)
    else:
        print(N)

Solution (Python):
def main():
  
  
  
  n,k = list(map(int,input().split()))
  
  
  
  
  
  ans = n
  zen = n
  count = 0
  lis = []
  
  if k == 1:
    print(0)
  elif n <= abs(n-k):
    print(n)
  else:
    ans = n % k
    if ans > 2:
      while 1:
        hi = abs(ans-k)
        if hi < zen:
          zen = hi
          ans = hi

        else:
          print(ans)
          exit()
    else:
      print(ans)
    
      
if __name__ == '__main__':
  main()

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

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

if N > K:
    N = N % K

while 1:
    a = abs(N - K)
    if a >= N:
        break
    else:
        N = a

print(N)

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

if N <= K:
    ans = abs(N-K)
    lis = []

    for i in range(3):
        ans = abs(ans - K)
        lis.append(ans)
    
    print(min(lis))
    sys.exit()

if N % K != 0:
    print(abs(N-(((N//K) + 1)*K)))
if N % K == 0:
    print(abs(N-((N//K)*K)))

Solution (Python):
N, K = map(int, input().split())
if K == 1 or N == 0 or N % K == 0:
    print(0)
elif N*2 < K:
    print(N)
elif N < K < 2*N:
    print(K-N)
else:
    tmp = abs(N%K-K)
    print(min(N%K,tmp))

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

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

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

Solution (Python):

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

min_value = 10**18

def check(x):
    global min_value

    if x % K == 0:
        min_value = 0
        return
    
    if x < K:
        if K % x == 0:
            min_value = x
            return
        else:
            if abs(x - K) == min_value:
                return
            else:
                if min_value > abs(x - K):
                    min_value = abs(x - K)
                check(abs(x - K))
    else:
        min_value = x % K
        check(x % K)

check(N)

print(min_value)

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

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

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

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

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

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


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

print(ans)

Solution (Python):
import sys
N, K = map(int, input().split())
if N == 0 or K == 1 or N == K:
    print(0)
    sys.exit()

if N > K:
    N = N%K




ans = [N]
for _ in range(100):
    ans.append(abs(N-K))
    N = abs(N-K)

print(min(ans))

Solution (Python):
L=list(map(int, input().split()))
N=L[0]
K=L[1]
m0=0
m1=0
m2=0
m3=0
m=0

if N==K:
    print(0)
else:
    m0=N
    m1=abs(N % K)
    m2=abs((N % K) - K)
    m3=abs(N-K)
    m=min(m0,m1,m2,m3)
    print(m)

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

amari = n%k

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

Solution (Python):

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

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

print(ans)

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

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

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())
if N > K:
    a = N % K
    ans = min(a,K-a)
    print(ans)
elif N < K:
    ans = min(N,K-N)
    print(ans)
elif N == K:
    print(0)

Solution (Python):

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


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

    p = n
    while True:
        t = abs(p-K)
        if t > p:
            break
        p = t
    print(p)

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

set_list = {N}
len_list = -1

while len(set_list) != len_list:
    
    len_list = len(set_list)
    if N >= M:
        set_list.add(N%M)
        N = N%M
    else:
        set_list.add(abs(N-M))
        N = abs(N-M)

    
print(min(set_list))

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

Solution (Python):
n,k = map(int,input().split())
answer = 0
if k >= n:
  gap = k-n
  if gap > n:
    answer = n
  else:
    answer = gap
else:
  sho = n // k
  if n % k == 0:
    dec = sho - 1
  else:
    dec = sho
  n = n - k*dec
  if k >= n:
    gap = k-n
    if gap > n:
      answer = n
    else:
      answer = gap
print(answer)

Solution (Python):
from collections import defaultdict

import sys
sys.setrecursionlimit(10**6)
ii = lambda: int(input())
mis = lambda: map(int,input().split())
lmis = lambda: list(mis())
INF = float('inf')

def main():
    n,k=mis()
    if n>=k:
        n%=k
        print(k%n if n!=0 else 0)
        exit()    
    print(n if n<=k//2 else k-n)



if __name__ == "__main__":
    main()

Solution (Python):

N,K = map(int,input().split())
a = N//K
if abs(K*(a+1)-N)<abs(K*a-N):
    ans = abs(N - (a+1)*K)
else:
    ans = abs(N-a*K)

print(ans)

Solution (Python):
N, K = [int(i) for i in input().split()]
r = N % K
ans = min(r, abs(r-K))
print(ans)

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

if N//K==0:
    if abs(N-K) > N:
        print(str(N))
    else:
        print(str(abs(N-K)))
elif N%K==0 or N==0:
    print("0")
elif K==2:
    print("1")
else:
    if a < b:
        print(str(a))
    else:
        print(str(b))

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

Solution (Python):
import sys

input = sys.stdin.readline

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

if N < K:
    print(min(N, K - N))
    exit()

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

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

print(min(abs(m-k), abs(n-k), n) if m!=0 else m)

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

Solution (Python):
import sys

input = sys.stdin.readline

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

if N < K:
    print(min(N, K - N))
    exit()

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

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

ans1 = N % K
ans2 = abs(ans1 - K)

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

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

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

Solution (Python):
from fractions import gcd
N, K = map(int, input().split())
if (N > K):
    N = N % K
print(min(N, K - N))
exit(0)

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

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


else:
    print(min(amari,k-amari))

'''    
while n > abs(n-k):
    n=abs(n-k)
print(n)
'''

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

val1 = N % K
val2 = K - val1

if val1 >= val2:
    print(val2)
else:
    print(val1)

Solution (Python):
import sys
from io import StringIO
import unittest


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

    if n == 0:
        print("0")
        return

    if k == 1:
        print("0")
        return

    
    q, mod = divmod(n, k)
    
    

    
    minus_edge = abs(mod - k)

    print(str(min(mod, minus_edge)))




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):
def min(a, b):
    if a >= b:
        res = a - b
    elif a < b:
        res = b - a
    return res

n,k = map(int, input().split())
if n%k == 0:
    print(0)
    quit()
else:
    n = n%k

min_num = float('inf')
res_list = []
while(1):
    res = min(n, k)
    if res < min_num:
        min_num = res
    if res == 0:
        break
    if res in res_list:
        break
    n = res
    res_list.append(res)

print(min_num)

Solution (Python):
from fractions import gcd
n,k = map(int,input().split())

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

	for i in range(100000):
		a = min(a,abs(a-b))
	return a
if n == 0:
	print(0)
elif n ==1:
	if k == 1:
		print(0)
	else:
		print(1)
elif k == 1:
	print(0)
else:
	print(process(n,k))

Solution (Python):
n,k = map(int,input().split())
if n % k ==0:
    print(0)
else:
    l = [n]
    p = n//k
    c = n-(p*k)
    half = k//2
    while c > half:
        c = abs(c-k)
    l.append(c)
    print(min(l))

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



while 2*n > k:
    
    if n > k:
        n = n%k
    elif n <= k:
        n = k-n

print(n)

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

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

print(ans)

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

if n % k == 0:
    print(0)
else:
    ans1 = n % k
    ans2 = k - ans1

    print(min(ans1,ans2))

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

aaans = abs(N-K)

if N == K:
    ans = 0
elif a == 1:
    ans = N
else:
    ans = (a*K)-N
    
aa = (N//K)
if N == K:
    aans = 0
elif aa == 1:
    aans = N
else:
    aans = N - (aa*K)

aaaans = min(ans,aans)
print(min(aaans,aaaans))

Solution (Python):
n, k = map(int, input().split())
a = []
if n % k == 0:
  print(0)

elif n // k == 0 :
  if -(n - k) > n :
    print(n)
  else :
    print(-(n - k))

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

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

  
if abs(c)>abs(d):
    print(str(abs(d)))
else:
    print(str(abs(c)))

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

x = min(N%M, M-N%M)
print(x)

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

if n % k == 0:
    print('0')
else:
    ans = n % k
    if ans > abs(ans - k):
        ans = abs(ans - k)
    print(ans)

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

flag = True

min = N

if K == 1:
    ans = 0
elif N % K == 0:
    ans = 0
elif abs(N - K) > N:
    ans = N
else:
    if (N % K) > (K / 2):
        ans = abs((N % K) - K)
    else:
        ans = N % K

print(ans)

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

f = N // K
e = abs(N - (f + 1) * K)

if abs(N - f * K) > e:
  print(e)
else:
  print(abs(N - f * K))

Solution (Python):


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

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

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

min = N



while True:
  if N >= K:
    N = N % K
  else:
    N = K - N

  if N < min:
    min = N
  elif N == min:
    break
  
print(min)

Solution (Python):
N,K = map(int, input().split())
xo=N%K
xn=abs(xo-K)
while True:
    if xo>xn:
        xo=xn
        xn=abs(xn-K)
    else:
        break
print(xo)

Solution (Python):

NK = input().split()
N = int(NK[0])
K = int(NK[1])




def func(N,K):
    prev = N 

    while(1):
        N = abs(N-K)
        if prev<=N:
            return prev
        prev = N


def fun(N,K):
    N = (N % K) 
    return N  

S=fun(N,K)
T = func(S,K)


print(T)

Solution (Python):


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

remainder = N % K
another_N = remainder

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

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

temp = n % k
if temp == 0:
    print(0)
elif abs(temp - k) < temp:
    print(abs(temp - k))
elif abs(temp -k) > temp:
    print(temp)

Solution (Python):

n,k = map(int,input().split())
n = abs(n-k) % k
if 2*n > k:
    n = abs(n-k)
print(n)

Solution (Python):

def resolve():
    import sys
    input = sys.stdin.readline
    n, k = [int(x) for x in input().rstrip().split()]
    n_set = set()
    while n not in n_set:

        n_set.add(n)
        if n == 0:
            break

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

    print(min(n_set))

resolve()

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

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

if amari == 0:
    result = 0
else:
    if amari>= (K-amari):
        result = K-amari
    else:
        result = amari
print(int(result))

Solution (Python):

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

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

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

if K > N :
    print( min(N, K-N) )
else :
    temp = N//K
    N = N-(temp*K)
    print( min(N, K-N))

Solution (Python):
N,K=map(int,input().split())
N=N%K
while(1):
    m=N
    N=abs(N-K)
    if(m<=N):
        print(m)
        exit()

Solution (Python):
def main(n: int, k: int):
    m = min(n, n % k)

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


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

Solution (Python):
def replaceInt(N, M):
    N = abs(N-M)
    return N
    
N, M = [int(i) for i in input().split()]

if N == M :
    output = 0
elif N < M :
    if N <= M / 2:
        output = N
    else:
        output = abs(M-N)
else :
    if M == 1:
        output = 0
    else :
        N = N % M
        output_candidates = []
        while True:
            N = replaceInt(N,M)
            if N in output_candidates:
                break
            output_candidates.append(N)
        output = min(output_candidates)
        
print(output)

Solution (Python):


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

if n == 0 or n == k:
    print(0)
elif k == 1:
    print(0)
else:
    if n > k:
        r = n % k
        if r == 0:
            print(0)
        else:
            n_1 = abs(r - k)
            n_2 = abs(n_1 - k)
            print(min(n_1, n_2))
    else:
        n_1 = abs(n - k)
        n_2 = abs(n_1 - k)
        print(min(n_1, n_2))

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

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

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

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

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

Solution (Python):

import sys

N, K = map(int, input().split())
if K == 1:
  print(0)
  sys.exit()
  
if N / K > 1:
  N = N - int(N / K) * K
a = abs(N-K)
while abs(a - K) < a:
  a = abs(a-K)

print(int(a))

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()

if N <= K:
    print(min(K-N, N))


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

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

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

    if K == 1:
        print(0)
        return

    N = N % K

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

    print(N)


if __name__ == "__main__":
    main()

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

loop = True
last2 = 0
last1 = 0
current = N % K
while loop:
  current = abs(K - current)

  if (current == last2):
    loop = False
    break
  else:
    last2 = last1
    last1 = current
    continue

result = last1 if (last1 < last2) else last2
print(result)

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

Solution (Python):




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

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


if __name__ == "__main__":
    main()

Solution (Python):
n, k = map(int, input().split())
mi = n
while mi >= n and mi > 0:
    if n < mi:
        mi = n
    
    
    if int(mi/k) > 1:
        mi = abs(mi - (k*int((mi/k))))
        n = mi
        continue
    n = abs(n - k)

print(mi)

Solution (Python):
N,K=map(int,input().split())
while N>100000000000000000*K:
  N=N-100000000000000000*K
while N>100000000000000*K:
  N=N-100000000000000*K
while N>1000000000000*K:
  N=N-1000000000000*K
while N>10000000000*K:
  N=N-10000000000*K
while N>100000000*K:
  N=N-100000000*K
while N>1000000*K:
  N=N-100000*K
while N>1000*K:
  N=N-1000*K
while N>100*K:
  N=N-100*K
while N>10*K:
  N=N-10*K
while N>K:
  N=N-K

N2=N-K
if N2<0:
  N2=-N2

if N<N2:
  print(N)
else :
  print(N2)

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

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

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

def calc_next_x(n,k):
    if n > k:
        return x % k
    else:
        return abs(n-k)

x_prev = 10 ** 18 + 1
x = n
while x < x_prev:
    x_prev = x
    
    x = calc_next_x(x, k)

print(min(x, x_prev))

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

div =N//K
N=N-div*K

if K!=1:
    while True:
        N=abs(N-K)
        tmp=N
        if abs(N-K)>tmp:
            print(tmp)
            exit(0)
else:
    print(0)

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

Solution (Python):
N, K = input().split()
s = int(N) % int(K)
t = s - int(K)
if t < 0:
  t = t * (-1)
  
print(min(s, t))

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

if N >= K:
  N = N - K*(N//K)
  if N >= K/2:
    N = abs(N-K)
else:
  if N >= K/2:
    N = abs(N-K)

print(N)

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

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

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

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

if __name__ == '__main__':
    main()

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

Solution (Python):
s=input().split()

N=int(s[0])
K=int(s[1])

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

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

if N==K:
    print(0)



if N > K:

    


    if N >2*K:
        L=N//K
        N=N-L*K
    else:
        N=N-K
    

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

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

    if N==K:
        print(0)

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

mod = n % k
candidate = abs(mod - k)

print(min(mod, candidate))

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

if N >= K:
    print(min(N % K,abs(N % K - K)))
else :
    A = [N]
    M = N 
    N = abs(N-K)
    A.append(N)
    while N != M:
        N = abs(N-K)
        A.append(N)
    print(min(A))

Solution (Python):
N, K = map(int, input().split())
if N == 0:
    print(N)
elif int(K / N) > 1:
    print(N)
else:
    amari = N % K
    mainasu_noatai = abs(amari - K)
    if (amari > mainasu_noatai):
    	print(mainasu_noatai);
    else:
        print(amari)

Solution (Python):
import sys
a = [int(i) for i in input().split()]
N = a[0]
K = a[1]

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


def resZ(i, K):
    return abs(i - K)

ab = resZ(N, K)

if N < ab:
    print(N)
    
    sys.exit()

N = N % K
while True:
    ab = resZ(N, K)
    
    if N < ab:
        print(N)
        
        sys.exit()

    N = ab

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

Solution (Python):

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

mod = n % k
kari = mod - k

if abs(mod) < abs(kari) :
    print(abs(mod))
else:
    print(abs(kari))

Solution (Python):
NK = input().split()
N = int(NK[0])
K = int(NK[1])
if N%K<K-N%K:
    print(N%K)
else:
    print(K-N%K)

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

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

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

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

Solution (Python):


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

if N % K == 0:
    print(0)
elif abs(N-K) < N:
    N = N % K
    while True:
        N_ = abs(N-K)
        if N_ < N:
            N = N_
        else:
            break
    print(N)

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

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

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

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

if __name__ == '__main__':
    main()

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

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

Solution (Python):
from sys import stdin
import sys

N, K = [int(x) for x in stdin.readline().rstrip().split()]
ans = N
temp = 0

if N % K == 0:
    print("0")
    sys.exit()
N = N % K
while N != K:
    
    N = abs(N-K)
    
    if abs(N-K) == temp:
        print(ans)
        sys.exit()
    temp = N
    if N < ans:
        ans = N

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

Solution (Python):
def judge(n, k, pre):
  if n > k:
    sa = n - k * (n // k)
  else:
    sa = k - n
  
  if sa > pre:
    return pre
  else:
    return judge(sa, k, sa)
    

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

print(judge(N, K, N))

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

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

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

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

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

    dist = abs(n-k)

    if n < dist:
        print(n)
        exit()

    if n == dist:
        print(0)
        exit()
                
    print(abs((n // k + 1) * k - n))
    exit()


if __name__ == '__main__':
    main()

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

if n==k:
    ans = 0
elif n<k:
    ans = min(n, abs(n-k))
else:
    if n%k==0:
        ans=0
    else:
        ans = min(n%k, k-(n%k))

print(ans)

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

else:
  p = (n+k)%k
  q = abs(p-k)
  if p > q:
    print(q)
  else:
    print(p)

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

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

if n > float(k/2):
    n = abs(n-k)

print(n)

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

ans1 = N%K
ans2 = K - N%K

print min(ans1, ans2)

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

if N>=K:
    amari = N % K
    X = K - amari
    if X > amari:
        x = amari
    else :
        x = X
else:
    if N >= (K-N):
        x = K-N
    else:
        x = N        


print(x)

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

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

if(N == 0):
  print(N)
elif(K == 1):
  print(0)
else:
  mod = N % K
  if(N<=K):
    if(K-mod < mod):
      print(K-mod)
    else:
      print(mod)
  else:
    print(K-mod)

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

t = n

if n % k == 0:
  print(0)
else:
  if n < k:
    a = abs(n - k)
    b = abs(a-k)
    if a < b:
      print(a)
    else:
      print(b)
  else:
  	x = n % k
  	print(k - x)

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

Solution (Python):
n,k = map(int,input().split())
if n % k ==0:
    print(0)
else:
    l = [n]
    p = n//k
    c = n-(p*k)
    half = k//2
    while c > half:
        c = abs(c-k)
    l.append(c)
    print(min(l))

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

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

if k==1:
    print(0)
elif k==n:
    print(0)
else:
    n2=n%k
    n3=k-n2
    print(min(n2,n3))

Solution (Python):
def main():
    import math
    N, K = map(int, input().split())
    
    
    amari = N % K
    
    tmp = K - amari

    if amari <= tmp:
        print(amari)
    else:
        print(tmp)

main()

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

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians

from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))
ZIP = lambda: zip(*(MAP() for _ in range(n)))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N, K = MAP()

    r = N%K
    if r == 0:
        print(0)
    else:
        if 2*r > K:
            print(abs(r - K))
        else:
            print(r)


if __name__ == '__main__':
    main()

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

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

while n > k:
    n = n - k

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

print(min(n,k-n))

Solution (Python):
import math

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

while True:
    if min(n, k) == 0:
        break
    if n <= k/2:
        break
    m = max(n, k) % min(n, k)
    
    if m < n:
        n = m
    else:
        break

print(n)

Solution (Python):
in1 = input().split()
in1num = [int(x) for x in in1]
N = in1num[0]
K = in1num[1]






q, mod = divmod(N, K)

if mod == 0:
  print(0)
else:
  if K - mod < mod:
    print(K-mod)
  else:
    print(mod)

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

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

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

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

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

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

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