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



Sample input:
7 4


Sample output:
1

Submission status: Accepted

Solution code:
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):    # 10**6
    fs = [1]
    for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
    return fs

def invs_list(n, fs, modp):     # 10**6
    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()

Submission status: Accepted

Solution code:
#C
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 )))

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

print(_min)

Submission status: Accepted

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

Submission status: Accepted

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

q = n // k
mod = n % k

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# C - Replacing Integer

N, K = (int(i) for i in input().split())
N -= (N // K) * K
print(min(N, abs(N - K)))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:

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

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8

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

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

print(tmp)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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:
        # Looped:
        break
    else:
        n = new_n

print(min(_set))

Submission status: Accepted

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

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

print(division_like(N,K))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

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

n %= k

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
x,y = map(int,input().split())
print(min (x%y, y  - x%y ))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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)

Submission status: Accepted

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

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

    print(a)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

n = n % k

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

print(n)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

print(min(a,b))

Submission status: Accepted

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

d = n // k 
mod = n % k

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:


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)

Submission status: Accepted

Solution code:
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]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# Problem C - Replacing Integer

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

# initialization
compare_1 = N % K
compare_2 = abs(compare_1 - K)
ans = min(compare_1, compare_2)

# output
print(ans)

Submission status: Accepted

Solution code:
def main():
    from builtins import int, map, list, print, min
    import sys
    sys.setrecursionlimit(10 ** 6)

    input = sys.stdin.readline
    input_list = (lambda: input().rstrip().split())
    input_number = (lambda: int(input()))
    input_number_list = (lambda: list(map(int, input_list())))

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



if __name__ == '__main__':
    main()

Submission status: Accepted

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

while True:
  if N >= K:
    N = N%K
  else:
    if abs(N-K) < N: N = abs(N-K)
    print(N)
    sys.exit()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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



ans1 = n % k
ans2 = abs(ans1-k)

print(min(ans1, ans2))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# N = int(input())
N, K = map(int, input().split())
# S = input()
# X1_list = [int(input()) for i in range(N)]
# X2_list = [list(map(int,input().split())) for i in range(N)]
result = N % K
print(min(result,abs(result-K)))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
input_ints = lambda: list(map(int, input().split()))
N, K = input_ints()

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

Submission status: Accepted

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

m = N % K

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n,k=(int(i) for i in input().split())
print(min(n%k,(((n//k)+1)*k)-n))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

    times = n // k
    ans = n - k * times

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

# else:
    # do nothing

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 2020/04/04
# AtCoder Beginner Contest 161 - C

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

# Calc
rem = n % k
ans = min(abs(rem - k), rem)

# Output
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


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

Submission status: Accepted

Solution code:
def solve():
    n, k = map(int, input().split())
    if n % k == 0:
        print(0)
    else:
        if n - k > 0:
            r = n % k
            d = abs(r - k)
            print(min(r, d))
        else:
            d = abs(n - k)
            print(min(n, d))


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin, stdout


def read():
    x0 = stdin.readline().rstrip()
    return x0


def __main__():

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

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

    print(r)
    
__main__()

Submission status: Accepted

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

ans = n % k

i = False

if ans == 0:
  i = True

while not i:
  if ans > abs(ans - k):
    ans = abs(ans - k)
  else:
    i = True

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
m = N % K
if m > 0:
    _m = abs(m - K)
    if _m < m:
        m = _m
print(m)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def op(x,K):
    return min(abs(x), abs(x-K))


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
x = n%k
ans = []
for i in range(2):
    x = abs(x-k)
    ans.append(x)
print(min(ans))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial
from bisect import bisect_left #bisect_left(list, value)
#from fractions import gcd
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
def pri(x): print('\n'.join(map(str, x)))

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

d = defaultdict(int)
res = N
x = N
while 1:
    x = res%K
    y = abs(res-K)
    #print(res, x, y)
    res = min(res, x, y)
    d[res] += 1
    #print(d)
    if d[res] > 10:
        break
print(res)

Submission status: Accepted

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

if n > k:
  count = n // k
  res1 = n - (k * count)
  res2 = res1 - k
  res2 = max(res2, -res2)
  res = min(res1, res2)
  print(res)
  
else:
  res1 = n
  res2 = res1 - k
  res2 = max(res2, -res2)
  res = min(res1, res2)
  print(res)

Submission status: Accepted

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

v = N

while abs(v - K) < v:
  if v > K:
    v = v % K
    
  v = min(v, abs(v - K))
  
print(v)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)


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

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


main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
while N > K:
  N = N%K

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

print(N)

Submission status: Accepted

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

if N > K:
    N = N % K

# N <= K になっている

print(min(N, K - N))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N, K = map(int, input().split())

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


main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n , k = map(int,input().split())
waru = n//k

aa = abs(n-k*waru)
bb = abs(aa-k)
if aa<bb:
    print(aa)
else:
    print(bb)

Submission status: Accepted

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

d=abs(N-K)
c=N%K

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

Submission status: Accepted

Solution code:
"""
NTC here
"""
import sys
inp = sys.stdin.readline
def input(): return inp().strip()
# flush= sys.stdout.flush
# import threading
# sys.setrecursionlimit(10**6)
# threading.stack_size(2**26)
 
def iin(): return int(input())
 
 
def lin(): return list(map(int, input().split()))
 
 
# range = xrange
# input = raw_input
 
def main():
    T = 1
    while T:
        T-=1
        n, k = lin()
        n = n%k
        ans = n
        done = set()
        while n not in done:
            n = abs(n-k)
            ans = min(n, ans)
            done.add(n)
        print(ans)


 
 
 
 
 
main()
 
# threading.Thread(target=main).start()

Submission status: Accepted

Solution code:
import math
n,k = map(int,input().strip().split())
f = 1
x = n
while True:
    if x>k:
        poss = x//k
        x = x-poss*k
    if abs(x-k)<x:
        x = abs(x-k)
        f = 1
    else:
        break
print(x)

Submission status: Accepted

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

Submission status: Accepted

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

x=N%K

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

Submission status: Accepted

Solution code:
#c

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

NpK = N%K
if 2 * (NpK) < K:
  print(NpK)
else:
  print(K-NpK)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve(n, k):
    mod = n % k
    if mod == 0:
        print(0)
    elif abs(mod - k) < mod:
        print(abs(mod - k))
    else:
        print(mod)

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