Problem:
 Score : 300 points    Problem Statement Given any integer x , Aoki can do the operation below.  Operation: Replace x with the absolute difference of x and K .  You are given the initial value of an integer N . Find the minimum possible value taken by N after Aoki does the operation zero or more times.      Constraints  0 ≤ N ≤ 10^{18}  1 ≤ K ≤ 10^{18}  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  K       Output Print the minimum possible value taken by N after Aoki does the operation zero or more times.        Sample Input 1 7 4      Sample Output 1 1  Initially, N=7 .  After one operation, N becomes |7-4| = 3 .  After two operations, N becomes |3-4| = 1 , which is the minimum value taken by N .       Sample Input 2 2 6      Sample Output 2 2  N=2 after zero operations is the minimum.       Sample Input 3 1000000000000000000 1      Sample Output 3 0

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

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

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

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

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

b = abs(n - k * a)
c = abs(n - k * (a + 1))
print(min(b, c))

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

mo = N % K

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

Solution (Python):
n,k = map(int,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):



def inp():
    return(int(input().rstrip()))
def inlt():
    return(list(map(int,input().rstrip().split())))
def insr():
    s = input().rstrip()
    return(s[:len(s) - 1])
def invr():
    return(map(int,input().rstrip().split()))

n,k = invr()
if k >= 2*n:
    print(n)
elif k >= n:
    print(k - n)
else:
    need = n//k
    remain = n%k
    first = need *k
    print(min(n - first,abs(n - first - k)))

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

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

a = N % K

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

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

Solution (Python):



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


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import os

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

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

NN = -(N - K)

print(min(N, NN))

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

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

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

cand1 = N % K
cand2 = abs(cand1 - K)

if cand1 < cand2:
    print(cand1)
else:
    print(cand2)

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

Solution (Python):
N, K = [int(i) for i in input().split()]
while(True):
    if N > K:
        tmp = N % K
    else:
        tmp = abs(N - K)
    if tmp < N:
        N = tmp
    else:
        break
print(N)

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

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

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

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

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

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

Solution (Python):
n, k = [int(x) for x in input().split()]
def sub(n, k):
    if n < k:
        return min(n, k - n)
    return sub(n % k, k)

print(sub(n, k))

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

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

modK = N % K 

if modK < K - modK:
  Ans = modK
else:
  Ans = K - modK

print(Ans)

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

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

Solution (Python):
def input_int():
    return map(int, input().split())

def one_int():
    return int(input())

def one_str():
    return input()

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

N, K = input_int()

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

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

t = n // k

n -= t*k
print(min(abs(n-k), n))

Solution (Python):

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

tmp = n % k
tmp2 = abs(tmp - k)

print(min(tmp , tmp2))

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

a = n % k
b = abs(a - k)
if a > b:
    print(b)
else:
    print(a)

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

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))

def main():
    N, K = input_nums()
    a = N // K
    print(min(N-K*a, abs(N-K*(a+1))))

if __name__ == '__main__':
    main()

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

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

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

if __name__ == "__main__":
    main()

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

if N>K:
    N = N%K

while True:
    x = abs(N-K)
    if x<N:
        N=x
        continue
    else:
        break
print(N)

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

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

X = N % K
Y = abs(X - K)

if X > Y:
    print(Y)
else:
    print(X)

Solution (Python):
N, K = map(int, input().split())
if N > K:
  m = N //K
  a = N - m*K
else:
  a = N
b = K - a
print(min(a,b))

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


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

print(ans)

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

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

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

b = min(N,K)
for i in range(100000):
    if b > max(N,K)-min(N,K):
        b = max(N,K)-min(N,K)
    else:
        break

print(b)

Solution (Python):



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

    
    

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

    if amari > sabun:
        return sabun

    return amari


if __name__ == '__main__':
    print(main())

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)  
to_index = lambda x: int(x) - 1  
print_list_in_2D = lambda x: print(*x, sep="\n")  


def input_int(): return int(input())

def map_int_input(): return map(int, input())

MII = map_int_input

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

def MII_to_index(): return map(to_index, input())

def MII_split_to_index(): return map(to_index, input().split())


def list_int_inputs(): return list(map(int, input()))

LII = list_int_inputs

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


def LII_2D(rows_number): return [LII() for _ in range(rows_number)]

def LII_split_2D(rows_number): return [LII_split() for _ in range(rows_number)]

N, K = MII_split()

amari = abs(N % K)

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

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

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

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

    print(ans)


resolve()

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

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

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

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

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

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

t = n // k
print(min([n, abs(n - k * t), abs(n - k * (t + 1))]))

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

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


cand1 = N % K
cand2 = abs(N % K - K)

print(min(cand1, cand2))

Solution (Python):
def main():
    N,K=map(int, input().split())
    ans=N
    for i in range(100000):
        N=N%K
        ans=min(ans,N)
        if N==0:
            break
        N=-N
    print(ans)

if __name__ == "__main__":
    main()

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

a=n%k

b=k-a

print(int(min(n,a,b)))

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

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

Solution (Python):
import sys
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

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

left = N // K
right = left + 1

ans = min(abs(N - K * left), abs(N - K * right))

print(ans)

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

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

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

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

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

print(min(N, K - N))

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

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

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

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

if n > k:
    n = n % k

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

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

Solution (Python):
import math

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

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

answer = min(cand1, cand2)

print(answer)

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

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

Solution (Python):


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

if n > k:
    n = n % k

print (min(n, k - n))

Solution (Python):
def main():
  x, y = map(int, input().split())
  a = x % y
  b = abs((x % y) - y)
  print(min([a,b]))
main()

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

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

Solution (Python):
N, K = [int(x) for x in input().split()]
a = N % K
b = abs(a - K)
print(min(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())
print(min(abs(n%k-k), n%k))

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

Solution (Python):
import sys

input = sys.stdin.readline


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


if __name__ == '__main__':
    main()

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

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

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

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

Solution (Python):

import sys


def solve(N: int, K: int):
    N = N % K
    if N > K // 2:
        print(K - N)
    else:
        print(N)
    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):
import sys
n, k = [int(j) for j in sys.stdin.readline().split()]
print(min(n%k, k-n%k))

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

result = N
if N > K:
    result = min(result, N % K)
for i in range(1000):
    result = min(result, abs(result - K))
print(result)

Solution (Python):
import sys
from collections import defaultdict
n,k=map(int,sys.stdin.readline().split())
if n>k:
    n=n%k
ans=min(n,abs(n-k))
print(ans)

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

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

Solution (Python):

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

mod = N % K


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

Solution (Python):
''' بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ '''

gi = lambda : list(map(int,input().split()))
n, m = gi()
vis = set()
ans = n + m;
while n not in vis:
	vis.add(n)
	ans = min(ans, n);
	n %= m
	vis.add(n)
	ans = min(ans, n);
	n = abs(n - m)
ans = min(ans, n);
print(ans)

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

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())
X = N//K
ans1 = N-K*X
ans2 = abs(N-K*(X+1))
print(min(ans1,ans2))

Solution (Python):
from sys import stdin
N,K = [int(x) for x in stdin.readline().rstrip().split()]
ans = N%K
print(min(ans,abs(ans-K)))

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

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

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

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

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

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

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

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

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

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

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

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

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

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

div = N // K

tmp1 = N - K*div
tmp2 = abs(N-K*(div+1))

print(min(tmp1, tmp2))

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

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

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

a = n%k

print(min(a, k-a))

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

Solution (Python):

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

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

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

Solution (Python):
import bisect as bs

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

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

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

tmp = N // K
x = N - (tmp * K)

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

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

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

Solution (Python):
from fractions import gcd
def main():
    n, k = map(int, input().split())
    if n >= k:
        n = n % k
    ans = min(n, k-n)
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
import sys

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

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

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

n %= k

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

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

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

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

Solution (Python):

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

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

Solution (Python):
import sys

def main():
    N, K = map(int, input().split())
    a = N % K
    b = K - a

    print(min(a,b))

    return 0

if __name__ == '__main__':
    sys.exit(main())

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

Solution (Python):
n, k = [int(c) for c in input().split()]
n = n - k*(n//k)
print(min(n, k-n))

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

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N, K = LI()
    if N < K:
        ans = min(abs(K-N), N)
    else:
        N = N % K
        ans = min(abs(K-N), N)
    print(ans)

main()

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

a = N % K
b = K - a

print(min(a, b))

Solution (Python):


import sys

DEBUG = False

def read(t):
    return t(sys.stdin.readline().rstrip())


def read_list(t, sep = " "):
    return [t(s) for s in sys.stdin.readline().rstrip().split(sep)]


def dprint(*args, **kwargs):
    if DEBUG:
        print(*args, **kwargs)
    return


def main():
    n, k = read_list(int)
    print(min(n, n - (n // k) * k, abs(n - (n // k + 1) * k)))


if __name__ == "__main__":
    main()

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

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

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

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

Solution (Python):


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

     n_d = n % k
     ans = n_d
     while(ans > abs(ans - k)):
          ans = abs(ans - k)
     
     print(ans)

if __name__ == "__main__":
    main()

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

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 = [int(x) for x in input().split()]

if n > k:
  n = abs(n%k)

while True:
  n_ = abs(n-k)
  if n_ >= n:
    print(n)
    break
  else:
    n = n_

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

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

Solution (Python):


import sys
stdin = sys.stdin

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

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

if __name__ == "__main__":
    main()

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

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

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

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

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

Solution (Python):
import math

from sys import exit, setrecursionlimit
setrecursionlimit(10**6)

def array2D(row, col ,val=0):
    return [[val]*col for i in range(row)]

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

a1 = x//k
ans = 10**30
for i in range(-10, 11):
    ans = min(ans, abs(x-(a1+i)*k))
print(ans)

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

ans = n
q, r = divmod(n, k)
ans = min(ans, abs(r-k))
if r == 0:
    ans = 0
print(ans)

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

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

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

a = N % K

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

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

visited = set()
x = n
while x not in visited:
    visited.add(x)
    if x > k:
        x = x % k
    else:
        x = k - x
print(min(visited))

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

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

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

q = n //k

ans = n - q*k

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

Solution (Python):
import collections
import sys
import numpy as np
sys.setrecursionlimit(1000000000)

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

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

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

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

x1 = n % k
x2 = k - x1
ans = min(x1, x2)

print(ans)

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

c = n % b

d = abs(c - b)

print(min(c,d))

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

Solution (Python):
def solve(n, k):
    if n > k:
        n = n % k
    return min(n, k - n)


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


def test():
    assert solve(7, 4) == 1
    assert solve(2, 6) == 2
    assert solve(1000000000000000000, 1) == 0


if __name__ == "__main__":
    test()
    main()

Solution (Python):
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

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

c = N // K

print(min(N - c * K, (c + 1) * K - N))

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

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

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

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

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


else:
  a=n//k
  q=n-a*k
  w=k-q
  s=min(w,q)
  print(s)

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

a = N//K

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

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

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

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

Solution (Python):
import sys
from math import ceil
RI = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
 

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

Solution (Python):
import sys
import heapq
from operator import itemgetter
 
MAX_INT = int(10e15)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N,K = IL()
p = N - N//K*K

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

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

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

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

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

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

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

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

if __name__ == '__main__':
    main()

Solution (Python):

import sys
sys.setrecursionlimit(10**8)
INF = float("inf")


def solve(N: int, K: int):

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

    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):
InputIntList = lambda : list(map(int, input().split()))
InputStrList = lambda : list(input().split())


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

    N = N % K

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

main()

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

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

print(min(a,b))

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

Solution (Python):

from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip


def main():
    n, k = [int(s) for s in input().strip().split()]
    n %= k
    result = min(n, k - n)
    print(result)




BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")



if __name__ == "__main__":
    main()

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


if __name__ == "__main__":
    N, K = tuple(map(int, input().split(" ")))
    M = N % K
    print(min(abs(K - M), M))

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

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

    n = N % K
    return min(n, abs(n - K))

print(c_replacing_integer())

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

pos = n % k

neg = pos - k

print(min(pos, abs(neg)))

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

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

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

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

def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())

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

def LLI(rows_number): return [LI() for _ in range(rows_number)]

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

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

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

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

a = N // K
b = N // K + 1

A = abs(N - K * a)
B = abs(N - K * b)

if A < B:
  print(A)
else:
  print(B)

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

a=N%K

print(min(a,K-a))

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

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

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

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

mod = N % K

mod_minus = mod - K

ans = min(mod, abs(mod_minus))

print(ans)

Solution (Python):
def main():
    x, K = list(map(int, input().split()))
    t = x // K

    return min(x, abs(K * t - x), abs(K * (t + 1) - x))

print(main())

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

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

mod_x = n%k

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

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

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