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())
while True:
    if n > k:
        a = n % k
    else:
        a = abs(n-k)
    if n > a:
        n = a
    else:
        print(n)
        break

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

n, k = map(int, input().split()) 
n = n % k
d = set([n])
while True:
    diff = abs(n - k)
    pre = n
    n = min(diff, n)
    if n in d:
        break
    else:
        d.add(n)
print(min(d))

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

Solution (Python):

import sys


def solve(N: int, K: int):
    rest = N % K

    if rest > K / 2:
        print(abs(rest - K))
    else:
        print(rest)
    
    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 = list(map(int, input().split()))
print(min(N % K, K - N % K))

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

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

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

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

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

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

while n // m > 0:
  n  = n % m 
print(min(n, abs(n-m)))

Solution (Python):

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

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

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

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

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

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

read = sys.stdin.read
readline = sys.stdin.readline

N, K = map(int, read().split())
if N == K:
    print(0)
elif N > K:
    N %= K
    print(min(N, abs(N - K)))
else:
    print(min(N, abs(N - K)))

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

Solution (Python):

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

Solution (Python):

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

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



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

print(min(a,b))

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

n=n%k

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

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

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

if N >= K:
    a = N // K
    print(min(N-(a*K), abs(N-(a+1)*K)))
else:
    print(min(N, K-N))

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

ans_1 = n%k

ans_2 = abs(ans_1-k)



print(min(ans_1,ans_2))

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


x = N

tmp = x // K
x -= K * tmp

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

print(x)

Solution (Python):
import numpy as np
N,K = list(map(int, input().split()))
if N==K:
    N=0
if N>K:
    N=N%K

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

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

amari = n % k

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

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

Solution (Python):


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


NK = N // K


ans = min(abs(N - K * NK), abs(N - K * (NK + 1)))

print(ans)

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

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

Solution (Python):
n,m = map(int,input().split())
syo,amari = divmod(n,m)
ans = m-amari
if ans >= n:
  ans = n
if amari == 0:
  ans = 0
print(ans)

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

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

Solution (Python):

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


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


print(Ans)

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

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

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

if x == y or x//2==y or y == 1:
    print(0)
else:
    print(min(abs(x%y-y),x%y))

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

x = 1000000000000000000
if N < K:
  x = min(N, K - N)
elif N == K:
  x = 0
else:
  m = N//K
  x = min((N - m * K),((m + 1) * K - N))

print(x)

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

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
if N < K:
    if N != 0:
        q, mod = divmod(K, N)
        ans = min(N, K - N)
    elif N == 0:
        ans = 0
elif N == K:
    ans = 0
else:
    q, mod = divmod(N, K)
    ans = min(mod, abs(K - mod))
print(ans)

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

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

while n > abs(n - k):
    count = n // k
    n = min(abs(n - count * k), abs(n - (count+1) * k)) 
    
print(n)

Solution (Python):
n = list(map(int, input().split()))
k = n[0] % n[1]

if abs(k - n[1]) > k:
  print(k)
else:
  print(abs(k - n[1]))

Solution (Python):

n, k = [int(x) for x in input().split()]
ans = 0
if k > n:
    if k - n < n:
        ans = k - n
    else:
        ans = n
else:
    t = n - k * (n // k)
    
    
    if t > -(t-k):
        ans = -(t-k)
    else:
        ans = t
print(ans)

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

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

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

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

min_0 = N
min_1 = abs(N-m*K)
min_2 = abs(min_1-K)
m=int(min_2/K)
min_3 = abs(min_2-m*K)

print(min(min_0, min_1, min_2, min_3))

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


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

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

if __name__ == "__main__":
    main()

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())
last = 0
if n > k:
    n %= k
while last + n != k:
    last = n
    n = abs(n-k)
print(min(last,n))

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

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

x = n
x = x % k
while True:
  tmp = abs(x-k)
  if x > tmp:
    x = tmp
  else:
    break

print(x)

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

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

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

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

a = N // K

suj1 = abs(N - a*K)
suj2 = abs(N - (a+1)*K)

print(min(suj1, suj2))

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

if __name__ == "__main__":
    main()

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

def main():
    N, K = map(int, read().split())
    ans = N
    if N >= K:
        ans = N % K
    ans = min(ans, K - ans)
    print(ans)
    return

if __name__ == '__main__':
    main()

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

if N <= K:
    while abs(N - K) < N:
        N = abs(N - K)
else:
    N1 = N % K
    N2 = abs(N - int(N // K + 1) * K)
    N = N1 if N1 < N2 else N2

print(N)

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

x = N%K

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

Solution (Python):

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

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

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



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



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

Solution (Python):
N,K=map(int,input().split())
if N%K==0:
  print("0")
elif N<K and N<abs(N-K):
  print(N)
else:
  t=N//K
  if abs(N-t*K)<abs(N-(t+1)*K):
    print(abs(N-t*K))
  else:
    print(abs(N-(t+1)*K))

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

ans = []
t1 = int(N/K)
for i in range(-50,50):
    t = t1 + i
    ans.append(abs(N-K*t))
    
print(min(ans))

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

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

print(ans)

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

if N < K:
    Ans = min(abs(N-K),N)
else:
    Ans = min(abs((N%K)-K),N%K)

print(Ans)

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

Min = N%K
t = abs(Min-K)

print(min(Min,t))

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

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

Solution (Python):


import sys
import math

from itertools import count, permutations
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint

INF = float('inf')

ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
sys.setrecursionlimit(1000000000)


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



n,k = mis()

mod = n % k

mod_k = abs(mod - k)

if mod_k < mod:
    print(mod_k)
else:
    print(mod)

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

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

r =int(N%K)
l = abs(r-K)
print(min(r,l))

Solution (Python):

N,K = map(int,input().split())
remain = N % K
num = abs(remain - K)



if num >= remain:
  print(remain)
else:
  print(num)

Solution (Python):
from collections import defaultdict, deque
import sys
import heapq
import bisect
import math
import itertools
import string
import queue
import copy
import time

from fractions import gcd

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


def inp(): return int(sys.stdin.readline())


def inp_list(): return list(map(int, sys.stdin.readline().split()))


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


N, K = inp_list()

mod = N % K
sho = N // K

if mod == 0:
    print(0)
else:
    ans = min(abs(sho * K - N), abs((sho + 1) * K - N))
    print(ans)

Solution (Python):


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

while True:
    if N >= K:
        d, m = divmod(N, K)
        if m == 0:
            print(0)
            exit(0)
        N = N - (K * d)
    else:
        N = K - N
    if ans > N:
        ans = N
    else:
        break
print(ans)

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

mod = N%K

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

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

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

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

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

m = n % k

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

print(ans)

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

Solution (Python):
N, K = map(int, input().split())
while True:
    q, r = divmod(N, K)
    if q == 0:

        print(min(abs(N - K), N))
        break
    else:
        N = r

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

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

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

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

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

n -= k * (n // k)

old = n
mi = n

while True:
  answer = abs(n-k)
  mi = min(mi, answer)
  if old < answer:
    break
  old = answer
  n = answer

print(mi)

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

n = int(nk[0])
k = int(nk[1])

j = n%k

if j > k//2:
	print(abs(k-j))
else:
	print(j)

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

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

print(min(a, k-a))

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

Solution (Python):

from math import ceil
N, K = map(int, input().split())
v = ceil(N / K)
ans = abs(N - v*K)
print(min(N, abs(N%K - K), N%K))

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)
import math
from fractions import gcd
def lcm(a, b): return a * b // gcd(a, b)
from itertools import count, permutations
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok

def get_inv(n, modp):
    return pow(n, modp-2, modp)

def factorials_list(n, modp):    
    fs = [1]
    for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
    return fs

def invs_list(n, fs, modp):     
    invs = [get_inv(fs[-1], modp)]
    for i in range(n, 1-1, -1):
        invs.append(invs[-1] * i % modp)
    invs.reverse()
    return invs

def comb(n, k, modp):
    num = 1
    for i in range(n, n-k, -1):
        num = num * i % modp
    den = 1
    for i in range(2, k+1):
        den = den * i % modp
    return num * get_inv(den, modp) % modp

def comb_from_list(n, k, modp, fs, invs):   
    return fs[n] * invs[n-k] * invs[k] % modp



class UnionFindEx:
    def __init__(self, size):
        
        self.roots = [-1] * size
    def getRootID(self, i):
        r = self.roots[i]
        if r < 0:   
            return i
        else:
            r = self.getRootID(r)
            self.roots[i] = r
            return r
    def getGroupSize(self, i):
        return -self.roots[self.getRootID(i)]
    def connect(self, i, j):
        r1, r2 = self.getRootID(i), self.getRootID(j)
        if r1 == r2:
            return False
        if self.getGroupSize(r1) < self.getGroupSize(r2):
            r1, r2 = r2, r1
        self.roots[r1] += self.roots[r2]    
        self.roots[r2] = r1
        return True

Yes = 'Yes'
No = 'No'

def main():
    N,K=mis()
    N %= K
    print(min(N, abs(K-N)))

main()

Solution (Python):

n, k = map(int, input().split())
x = n
tmp1 = x / k
if tmp1 == 0:
    print(0)
elif tmp1 > 1:
    print(min(abs(x - k * (x//k)), abs(x - k * (x//k + 1))))
else:
    print(min(x, abs(x - k )))

Solution (Python):
def main():
    N,K = map(int,input().split())
    a = N // K
    N = N - a * K
    while True:
        N2 = abs(N - K)
        if N2 < N:
            N = N2
        elif N2 >= N:
            break
    print(int(N))


if __name__ == "__main__":
    main()

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

s=n//k
x=abs(n-(s*k))

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

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

Solution (Python):
n, k=map(int,input().split())
_min = n % k
while (-1):
    __min = abs(_min - k)
    if _min < __min:
        break
    _min = __min

print(_min)

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

if N > (2*K):
    N = N - (N // K - 1) * K
while(True):
    if abs(N-K) > N:
        break
    N = abs(N-K)

print(N)

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

q = n // k
mod = n % k

if mod <= abs(mod - k):
    print(mod)
    exit()
print(abs(mod-k))

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

print(ans)

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

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

Solution (Python):
from sys import stdin
import math
import fractions

n, k = [int(x) for x in stdin.readline().rstrip().split()]

r1 = int(n % k)
r2 = (int(n % k) - k) * -1

if r1 > r2:
    print(r2)
else:
    print(r1)

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

Solution (Python):
import sys
I=input().split()
N=int(I[0])
K=int(I[1])

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

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

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

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

done = []
now = n

while True:
  if now in done:
    break
  done.append(now)
  if now == 0:
    break
  if now%k == 0:
    done.append(0)
    break
  nex = abs(k - (now - (now//k)*k))
  now = nex

print(str(min(done)))

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

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

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

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

Solution (Python):

    
n,m=map(int,input().split())
if(m>n):
    c=m-n
    print(min(c,n))
else:
    if(n%m==0):
        print('0')
    else:
        c=n%m
        print(min(c,m-c))

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

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


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

N=N%K

ans=N
for _ in range(2):
    ans=min(ans,abs(ans-K))
    
print(ans)

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

Solution (Python):
i = list(map(int, input().split())) 
N = i[0]
K = i[1]

amari = N % K
zeta = amari - K
if zeta < 0:
    zeta = zeta * -1
    
if amari < zeta:
    print(amari)
else:
    print(zeta)

Solution (Python):
def min1(x,y):
	if x-y>=0:
		return y
	else:
		return x

str1 = str(input())
l1 = str1.split(' ')
n = int(l1[0])
k = int(l1[1])
print(min1(n%k,k-n%k))

Solution (Python):
def resolve():
    '''
    code here
    '''
    N, K = [int(item) for item in input().split()]

    while N >= K:
        if N >= K:
            N = N % K
        else:
            if N <= K//2:
                pass
            else:
                N = K % N
    
    if N >= K:
        N = N % K
    else:
        if N <= K//2:
            pass
        else:
            N = K % N

    print(N)

if __name__ == "__main__":
    resolve()

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

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

nmin = N

if N < K:
    for i in range(5):
        N = abs(N-K)
        if N < nmin:
            nmin = N
    print(nmin)
elif N == K:
    print(0)
else:
    n = N % K
    print(min(n,K-n))

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

div = int(N/K)
x = abs(x - (div*K))
if x < min:
    min = x
while x >= K:
    x = abs(x-K)
    if x < min:
        min = x
x = abs(x-K)
if x < min:
    min = x
print(min)

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

Solution (Python):


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

tmp = N % K
while tmp > abs(tmp - K):
    tmp = abs(tmp - K)

print(tmp)

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

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

ans = n%k

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

print(ans)

Solution (Python):
i = list(map(int, input().split()))
if i[0]%i[1]<i[1]-i[0]%i[1]:
    print(i[0]%i[1])
else:
    print(i[1]-i[0]%i[1])

Solution (Python):
import math
NK = list(map(int, input().split()))

mod = NK[0] % NK[1]
a = abs(mod - NK[1])

if (mod < a):
    a = mod

if (NK[0] < a):
    print(NK[0])
else:
    print(a)

Solution (Python):
def main():
    n, k = map(int, input().split())
    n = n - (n // k * k)
    ans = n
    cnt = 0
    while cnt < 10:
        n = abs(n - k)
        if ans > n:
            ans = n
        else:
            cnt += 1
    print(ans)


if __name__ == '__main__':
    main()

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

mod = N % K_div
ans = min(mod, K_div - mod)

print(ans)

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

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

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):
import sys
from math import ceil, floor
from collections import deque, Counter, defaultdict
from fractions import gcd
input = lambda: sys.stdin.readline().rstrip()

def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return

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

if __name__ == '__main__':
    main()

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

if n > 2 * k or k > 2 * n:
    n = n % k + k

_set = set()
_set_add = _set.add
_set_add(n)

while True:
    new_n = abs(n - k)
    _set_add(new_n)
    if new_n > n:
        
        break
    else:
        n = new_n

print(min(_set))

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

def division_like(n, k):
  remainder = n%k
  return min(remainder, k-remainder)

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

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

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

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

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

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

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

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

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

if __name__ == '__main__':
    main()

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

n %= k

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

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

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

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

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

X = N % K
while True:
    x = abs(X - K)
    if x >= X:
        print(X)
        break
    else:
        X = x

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

Solution (Python):

x,y = map(int,input().split())
print(min (x%y, y  - x%y ))

Solution (Python):


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


check_set = set()

x = N % K
x_min = x
check_set.add(x)


while True:
  x = abs(x - K)
  x_min = min(x_min, x)
  
  if x in check_set:
    break
  else:
    check_set.add(x)


print(x_min)

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

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

    print(a)


if __name__ == '__main__':
    main()

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

n = n % k

if (k-n) <= n:
    n = k-n

print(n)

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

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

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

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

if __name__ == '__main__':
    resolve()

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

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

print(solve(N, K))

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

def f(x):
    return abs(K-x)

if N > K:
    N %= K

while(True):
    N = f(N)
    if K >=N:
        print(min(N,K-N))
        break

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

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

n1 = abs(n-k)
while n > n1:
    n = n1
    n1 = abs(n-k)

print(n)

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

n = n % k

ans = n+1
while True:
    if abs(k - n) < ans:
        n = abs(k - n)
        ans = n
    else:
        print(n)
        break

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

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

if n < k:
  print(min(n, k-n))
elif n == k:
  print("0")
else:
  mod = n // k
  a = n-(mod * k)
  print(min(a, k-a))

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

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

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

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

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

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

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

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

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

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

ans = 0

if n >= k:
    if n%k != 0:
        ans = abs(k - (n%k))
else:
    if abs(n-k) <= n:
        ans = abs(n-k)
    else:
        ans = n

print(ans)

Solution (Python):

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


if __name__ == "__main__":
    main()

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


if __name__ == '__main__':
    main()

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

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

Solution (Python):
INT_MAX=10**18+7
MOD=10**9+7
def INPUT():return list(int(i) for i in input().split())
def LIST_1D_ARRAY(n):return [0 for _ in range(n)]
def LIST_2D_ARRAY(m,n):return [[0 for _ in range(n)]for _ in range(m)]

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

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

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

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

Solution (Python):
import numpy as np
N,K = map(int,input().split())

cand = N%K

if np.absolute(cand-K)<cand:
    print(np.absolute(cand-K))
else:
    print(cand)

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

print(min(a,b))

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

d = n // k 
mod = n % k

print(min(mod, -1*(n - (d+1)*k)))

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

Solution (Python):
nk=list(map(int, input().split(" ")))
n=nk[0]
k=nk[1]

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

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

Solution (Python):


n, k = [0] * 2

def format_input(filename = None):
	global n, k
	if filename == None:
		n, k = list(map(int, input().split()))

	elif filename == '__random__':
		from random import randint as rng
		n = rng(0, 10**18)
		k = rng(1, 10**18)
		print(n, k)

def get_answer():
	mod = n % k
	answer = min(mod, k - mod)
	return answer

if __name__ == '__main__':
	format_input()

	ans = get_answer()
	print(ans)

Solution (Python):
n = list(map(int,input().split()))
a = n[0]%n[1]
if a < abs(a-n[1]):
    print(a)
else:
    print(abs(a-n[1]))

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

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

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

if __name__ == '__main__':
    main()