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):
import itertools
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush,heapify
import math
from copy import deepcopy
import queue
import numpy as np

Mod = 1000000007
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[y] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

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

Solution (Python):
N,K =list(map(int,input().split()))
if N < K:
    mod=N
else:
    mod=N%K

if abs(mod-K) < mod:
    ans=abs(mod-K)
else:
    ans=mod
print(ans)

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

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

if N % K == 0:
  print(0)
else:
  q = N // K
  d = N - K*q
  print(min(d,abs(d-K)))

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


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

Solution (Python):

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

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

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

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

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





def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))





def main():
	n,k=MI()
	ans=n%k

	print(min(ans,k-ans))










if __name__ == "__main__":
	main()

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

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

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

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

Solution (Python):
import numpy as np
n, k = map(int, input().split())
ans = n%k , (k-n%k)%k 
answer = np.min(ans)
print(answer)

Solution (Python):
import sys
n,k=map(int,input().split())
if n == 0:
  print(n)
  sys.exit()
if n > k:
  n = n % k
if 2*n>k:
  n = k-n
print(n)

Solution (Python):
n, k = map(int, input().split())
if k == 1:
    print(0)
    exit()
m = n // k
ans1 = n - k * m
ans2 = - (n - k * (m + 1))
print(min(ans1, ans2))

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

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

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

div=int(N//K)
sub=N-K*div
if sub>abs(sub-K):
    min_val=abs(sub-K)
else:
    min_val=sub

print(min_val)

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

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

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

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

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

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

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

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

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

temp_c = N % K

if temp_c < K:
    ans = min([temp_c, abs(K-temp_c)])

print(ans)

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

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

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

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

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

Solution (Python):
n,k=map(int,input().split())
a=set()
for i in range(1,100000):
    n=n%k
    a.add(n)
    n=abs(n-k)
a=list(a)   
a.sort()
print(a[0])

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

if(n_>abs(l)):
    print(-l)
else:
    print(n_)

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

Solution (Python):

import sys


def solve(N: int, K: int):
    t = N % K
    ans = min(t, K - t)
    print(ans)
    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())
if n>k:
    n=n%k
    
n2=k-n
print(min(n,n2))

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

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

print(ans)

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

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

print(ans)

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

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

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

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

mod = N % K

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

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

div_result = N // K
div_remainder = N % K
abs_value = abs(div_remainder - K)

if div_remainder <= abs_value:
    if div_result == 0:
        print(N)
    else:
        print(div_remainder)
else:
    print(abs_value)

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

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

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

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

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

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

n %= k

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

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

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

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

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


if __name__ == '__main__':
    main()

Solution (Python):


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


if __name__ == '__main__':
    N, K = map(int, input().split())
    solve(N ,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(" "))
a = n % k
b = (k - a) % k

ans = min(a, b)
print(ans)

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

Solution (Python):
import sys

sys.setrecursionlimit(1000000)


def dfs(g: {}, v: int, path_list: [], added: {}):
    path_list.append(v)
    for next_v in g[v]:
        
        if next_v in path_list:
            continue
        
        if len(path_list) == 3:
            if path_list[0] not in g[next_v]:
                dic_a = added.setdefault(path_list[0], set())
                dic_a.add(next_v)
                dic_b = added.setdefault(next_v, set())
                dic_b.add(path_list[0])
        
        elif len(path_list) == 1:
            copied = [v]
            dfs(g, next_v, copied, added)
        
        elif len(path_list) == 2:
            copied = path_list.copy()
            dfs(g, next_v, copied, added)


def main():
    n, k = map(int, input().split())
    if k == 1:
        print(0)
        return

    y = n // k
    x = n - k*y

    x1 = x
    x2 = abs(x - k)
    print(min(x1, x2))



main()

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

a = n // k
b = a + 1

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

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

a = N - K
if a < 0:
  a += K
b = a % K
if K-b < b:
  b = K-b
print(b)

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

if N >= K:
    N = N % K

print(min(K - N, N))

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

n %= k

n = abs(n)

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

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

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

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

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

Solution (Python):
def resolve():
    N, K = map(int, input().split())
    ans = N-(N//K)*K
    if ans > K/2:
        ans = abs(ans - K)
    print(ans)
resolve()

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

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

Solution (Python):
import math

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

alist = []

tmp = abs(n % k)
for i in range(10000):
    alist.append(tmp)
    tmp = abs(tmp - k)
    
print(min(alist))

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

Solution (Python):
n,k=map(int,input().split())
n=n%k
ans=min(n,k-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())

if n % k == 0:
    print(0)
elif n < k < 2 * n:
    print(k-n)
elif n * 2 <= k:
    print(n)
else:
    print(min(abs(n - k * (n // k)), abs(n - k * (n // k + 1))))

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

while True:
    n = abs(x1 - k)
    x2 = abs(n - k)
    if x1 >= n and n <= x2:
        print(n) 
        sys.exit()
    else:
        x1 = n

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

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

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

def solve():
    global n, k
    n = n - (k * (n // k))
    pre = n
    while True:
        n = min(n, abs(n-k))
        if n == pre:
            break
        pre = n
    return n

print(solve())

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

n = N % K

ans = min(n,K-n)

print(ans)

Solution (Python):
from sys import stdin, stdout

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

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

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

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

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

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

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

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

t1 = n%k
t2 = abs(t1-k)

print(min(t1,t2))

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

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

Solution (Python):

import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

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

Solution (Python):













def main():
    N, K = map(int, input().split())
    dt = N//K
    ans = min(abs(N - dt * K), abs(N - (dt+1)*K))
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict

n,k = map(int, input().split())
dic = defaultdict(int)
prev = []
if n%k==0:
    print(0)
    exit()

    


while True:
    if n > k:
        m = n//k
        ab=n-m*k
    else:
        ab = abs(n-k)
    if dic[ab] == 0:
        prev.append(ab)
        dic[ab] = 1
        n = ab
    else:
        prev.sort()
        break

print(prev[0])

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

A = N%K

if A >= K/2:
    A = K - A

print(A)

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

mod1 = n%k
mod2 = abs(n%k - k)

print(min(mod1, mod2))

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

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

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

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

P = N % K
Q = K - P

if P > Q:
  print(Q)
else:
  print(P)

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

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

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

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

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

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

print(min(N, abs(N-K)))
"""
"""
m = -(-N//K)
print(abs(N - m*K))
"""

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

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

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


ans = n%k

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

print(ans)

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

Solution (Python):
N,K = [int(i) for i in 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())
se_ = set([N])
a = N % K
se_.add(a)
while True:
    a = max(K-a,a-K)
    if a in se_:
        break
    se_.add(a)

print(min(se_))

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

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

print(ans)

Solution (Python):
def f(n, k):
    if n % k == 0:
        print(0)
    else:
        n1 = n % k
        n2 = abs(n1 - k)
        print(min(n1, n2))

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

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

if N > K:
    N = N % K

ans = min(N, K - N)

print(ans)

Solution (Python):

N, K = [int(_) for _ in input().split()]
N = N % K
print(min(abs(N-K), N))

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

Solution (Python):
N, K = map(int, input().split())
x = N%K
check = [x]

if not x == 0:
    while True:
        x = abs(x-K)
        if x in check:
            break
        check.append(x)
print(min(check))

Solution (Python):

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


n, k = invr()
n = n % k
while abs(n-k) < n:
    n = abs(n-k)
print(n)

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

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

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

lst = []
lst.append(a)
lst.append(b)
print(min(lst))

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

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

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

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

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

ans=[]
tmp=N%K

for i in range(10**18):
    tmp=abs(tmp-K)
    
    
    if i == 0:
        ans.append(tmp)
    elif tmp>ans[i-1]:
        ans.append(tmp)
        break
    else:
        ans.append(tmp)

print(min(ans))

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

N = s[0]
K = s[1]

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

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

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

        print("{}".format(ans))

if __name__=="__main__":
        main()

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

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

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

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

    print(ans)


if __name__ == "__main__":
    main()

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())
ans =[k]
mod = n%k 
ans.append(mod)

ans.append(k - mod) 
print(min(ans))

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

N_set = set([])
N_set.add(float('inf'))
N_new = N

while (N_new > 0 and N_new < min(N_set)):
    N_set.add(N_new)
    if N_new >= K:
        N_new = N_new % K
    else:
        N_new = K - N_new
 
N_set.add(N_new)

ans = min(N, min(N_set))

print(ans)

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

if n <= k//2:
  print(n)
elif n < k:
  print(k-n)
elif n == k:
  print(0)
else:
  plus = n-n//k*k
  minus = abs(n-n//k*k-k)
  print(min(plus,minus))

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

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

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

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

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

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


if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin, setrecursionlimit


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


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

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

N = N % K

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

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

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

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

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

print(res)

Solution (Python):
from math import ceil
N, K = map(int, input().split())


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

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

if(n <= k):
    ans = min(abs(k-n),n)
else:
    m = n//k
    ans = min(abs(n-m*k),abs(n-(m+1)*k))

print(ans)

Solution (Python):
n, k = [int(_) for _ in input().split()]
n = n % k
if abs(n - k) < n:
    print(abs(n -k))
else:
    print(n)

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

ans1=N%K
ans2=K-ans1

if ans1<=ans2:
    print(ans1)

else:
    print(ans2)

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

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

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

loop = False
nmin = 1e+18

if n > k:
    n = n%k
while not loop:
    n = abs(n-k)
    if nmin > n:
        nmin = n
    else:
        loop = True
        print(nmin)
        break

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

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



if n>k:
        a=n//k
        b=n-a*k
        print(min(b,abs(n-(a+1)*k), abs(2*n-(a+1)*k)))
else:
        print(min(n,k-n))

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

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

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

Solution (Python):

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


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

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

Solution (Python):
def solve(string):
    n, k = map(int, string.split())
    n %= k
    return str(min(n, abs(n - k)))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
def solve(string):
    n, k = map(int, string.split())
    n %= k
    while n > abs(n - k):
        n = abs(n - k)
    return str(n)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

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

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

if __name__ == '__main__':
    main()

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

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

Solution (Python):



import sys
sys.setrecursionlimit(10**7)
from pprint import pprint as pp
from pprint import pformat as pf



import math

import bisect


if __name__ == '__main__':
    x, k = list(map(int, input().split()))
    m = math.inf
    if x > k:
        x = x % k
    for _ in range(3):
        x = abs(x - k)
        m = min(x, m)
    print(m)

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

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

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

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

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

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

main()

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

s = N % K

print(min(s, K-s))

Solution (Python):
N,K = map(int,(input().split()))
flg=0
first_mod=0
while(flg==0):

  if(first_mod==0):
    N=N%K
    first_mod=1
    
  new_N=abs(N-K)

  if(new_N>N):
    flg=1
  else:
    N=new_N
print(N)

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

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

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

ans=min(n,(m-n))
print(ans)

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

diff_abs_list = []
diff_list = []
judge = 0
count = 0

if N <= K:
    answer = min(abs(N - K), abs(abs(N - K) - K)) 
elif N % K == 0:
    answer = 0
else:
    while True:
        N = N % K
        judge = N -K
        N = abs(N - K)
        diff_abs_list.append(N)

        if judge < 0:
            break
        judge = 0
        count += 1
    answer = min(diff_abs_list[count -1], diff_abs_list[count])

print(answer)

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

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

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

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

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

Solution (Python):

import sys
from collections import Counter
from itertools import permutations, combinations
from fractions import gcd

from math import ceil, floor
import bisect
sys.setrecursionlimit(10 ** 6)
inf = float("inf")

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

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

if __name__ == '__main__':
    main()

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

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


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


if __name__ == '__main__':
    main()

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

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

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

a = N % K

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

Solution (Python):
n, k = map(int, input().split())
n %= k
visited = set()
ans = n
while True:
  visited.add(n)
  n = abs(n-k)
  ans = min(ans, n)
  if n in visited:
    break
print(ans)

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

a = abs(N - K * (N // K))

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

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

N=N-(N//K)*K
while True:
    N_new=abs(N-K)
    if N_new>=N:
        print(N)
        break
    N=N_new

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

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

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

m = 10**18
n = n % k
while True:
    n = min(n, abs(n-k)) 
    if n < m:
        m = n
    else:
        break

print(m)

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

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

print(min(x, y))

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

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

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

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

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

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

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

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

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

a = n // k

if n - k * a <= -(n - k * (a + 1)):
    print(n - k * a)

else:
    print(-(n - k * (a + 1)))

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

print(min(a,b))

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

if k == 0:
    print(n)
elif n < k:
    print(min(n, k-n))
else:
    r = n%k
    print(min(r, k-r))

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

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

Solution (Python):
import math
def py():
    print("Yes")
def pn():
    print("No")
def iin():
    x = int(input())
    return x

neko = 0
nya = 0
nuko = 0

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

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

if M==1:
    print(0)
    exit()
t1= N//M
t2 =t1+1

print(min(N-M*t1,M*t2-N))

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

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

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

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

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

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

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

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

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

Solution (Python):
from sys import stdin
import math
from decimal import Decimal

n, k = [Decimal(x) for x in stdin.readline().rstrip().split()]
print(int(min(abs(n-math.floor(n/k)*k), abs(n-math.ceil(n/k)*k))))

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

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

Solution (Python):

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

if N <= K / 2:
    print(N)
elif N < K:
    print(K-N)
else:
    if N - N // K * K <=K /2:
        print(N - N // K * K)
    else:
        print((1 + N // K) * K - N)

Solution (Python):

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

Solution (Python):
N ,K = map(int,input().split())
ans = N
while (1):
    
    if ans > K:
        if abs(ans - K) > ans:
            break
        else:
            ans = ans % K
    
    elif ans <= K:
        if abs(ans - K) > ans:
            break
        else:
            ans = abs(ans - K)
print(ans)

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

if k == 1:
    n = 0
else:  
    while abs(n - k) < np:
        i = 1
        if k ** i < n:
            while k ** i < n :
                i += 1
            n = n - k ** (i - 1)
        np = n
        n = abs(n - k)
print(n)

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

if k == 1:
    n = 0
else:
    if abs(n - k) > np:
        print(n)
        exit()
    
    while abs(n - k) < np:
        i = 1
        if k ** i < n:
            while k ** i < n :
                i += 1
            n = n - k ** (i - 1)
        np = n
        n = abs(n - k)
print(n)

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

def f(a,b):
    s = a//b 
    a = a-s*b
    ans = min(a,abs(a-b))
    return ans


if(n%k==0):
    print(0)
else:
    print(f(n,k))

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

Solution (Python):
N, K = map(int, input().split())
num = N//K
delta = abs(N-num*K)
if delta > abs(delta-K):
    print(abs(delta-K))
else:
    print(delta)