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:
n,k=map(int,input().split());t=n%k;print(min(t,k-t))

Submission status: Accepted

Solution code:
# C Replacing Integer

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

while N >= K:
  N = N % K

frag = 0
while frag == 0:
  cand = abs(N - K)
  if cand < N:
    N = cand
  else:
    frag = 1
print(N)

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=map(int,input().split())
A=N%K
print(min(A,abs(A-K)))

Submission status: Accepted

Solution code:
#!usr/bin/env pypy3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)

def main():
    N, K = LI()
    x = N % K
    y = abs(x - K)
    print(min(x, y))

main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
A=0
for a in range(100000000000000):
    if N>K:
        kot=N%K
        if abs(K-kot)>kot:
            print(kot)
            break
        else:
            print(abs(K-kot))
            break
    if N%K==0:
        print(0)
        break
    if N<K:
        if abs(N-K)<N:
            print(abs(N-K))
            break
        else:
            print(N)
            break
    tugi=abs(N-K)
    N=tugi
    A=tugi
    tugi=abs(N-K)
    B=tugi
    N=tugi
    tugi=abs(N-K)
    if A==tugi:
        if B<A:
            print(B)
            break
        else:
       	    print(A)
            break

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import collections as cl


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 main():
    n, k = MI()

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


main()

Submission status: Accepted

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

Submission status: Accepted

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

tmp = n % k

if tmp <= abs(tmp - k):
  print(tmp)
else:
  print(abs(tmp - k))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

print(n)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def binary_search_int(ok, ng, solve):
    """
    :param ok: solve(x) = True を必ず満たす点
    :param ng: solve(x) = False を必ず満たす点
    """
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if solve(mid):
            ok = mid
        else:
            ng = mid
    return ok, ng

##############################################################

def solve(x):
    """ ある点 x を境にして True と False が二分されるような関数"""
    return N - K*x >= 0

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

a, b = binary_search_int(0, 10**18, solve)
print(min(abs(N-a*K), abs(N-b*K)))

Submission status: Accepted

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

N = N % K
if (K - N) < N:
  N = K - N
print(N)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

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

    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def binary_search_int(ok, ng):
    """
    :param ok: solve(x) = True を必ず満たす点
    :param ng: solve(x) = False を必ず満たす点
    """
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if solve(mid):
            ok = mid
        else:
            ng = mid
    return ok, ng

##############################################################

def solve(x):
    """ ある点 x を境にして True と False が二分されるような関数"""
    return N - K*x >= 0

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

a, b = binary_search_int(0, 10**18)
print(min(abs(N-a*K), abs(N-b*K)))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def binary_search_int(ok, ng, solve):
    """
    :param ok: solve(x) = True を必ず満たす点
    :param ng: solve(x) = False を必ず満たす点
    """
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if solve(mid):
            ok = mid
        else:
            ng = mid
    return ok, ng

##############################################################

def solve(x):
    """ ある点 x を境にして True と False が二分されるような関数"""
    return N - K*x >= 0

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

a, b = binary_search_int(0, 10**18, solve)
print(min(abs(N-a*K), abs(N-b*K)))

Submission status: Accepted

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

def f(x):
    return( N-x*K )

def test(x):
    return( f(x) >= 0 )

left = - 1        # return = right = (取り得る値の最小値) の可能性を排除しないために、-1 が必要
right = 10**18

while right - left > 1:             # 最終的に (right, left, mid) = (最小値, 最小値 - 1, 最小値 - 1) に収束するため、差が 1 になったときに終了すればよい
    mid = (left+right)//2
    if test(mid):
        left = mid
    else:
        right = mid
print(min(abs(N-left*K), abs(N-right*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:
def main():
    N, K = map(int, input().split())
    v = N // K
    print(min([N - K * v, abs(N - K * (v + 1))]))


if __name__ == '__main__':
    main()

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, m = list(map(int, input().split()))
print(min(n%m, m-n%m))

Submission status: Accepted

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

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 = (int(x) for x in input().split())
N%=K
dif=N-K
if dif<0:
  dif*=-1
print(min(N, dif))

Submission status: Accepted

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

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 = map(int,input().split(" "))
a = n%k
b= abs(k-a)
if a <= b:
  print(a)
else:
  print(b)

Submission status: Accepted

Solution code:
import sys


def main():
    input = sys.stdin.buffer.readline
    n, k = map(int, input().split())
    n %= k
    while True:
        if abs(n - k) >= n:
            break
        else:
            n = abs(n - k)
    print(n)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

r = n % k

ans = min(r, k - r)

print(ans)

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=map(int,input().split())

if n>=k:
  ans=abs(n-((n+k//2)//k)*k)

else:
  ans=min(n,abs(n-k))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

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=map(int, input().split())
am = n % k
print(min(am,(k-am)))

Submission status: Accepted

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

x = abs((N%K) - K)
if x > K/2:
    x = abs(x - K)
print(x)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

(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())
if K>=N:
  print(min(N, K-N))
else:
  res = N % K
  print(min(res, K-res))

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=map(int,input().split())
print(min(n%k,abs(n%k-k)))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#12
import sys
N,K = map(int,input().split())
num = float("inf")
n = 0

for i in reversed(range(1,18)):
    n = (10**i)*K
    for j in range(9):
        N -= n
        if N < 0:
            N+=n
            break

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

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

if abs(N-K) > num:
    print(num)
else:
    print(abs(N-K))

Submission status: Accepted

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

# while k<n:
#   n-=k
if k<n:
  n-=n//k*k

# while k-n<n:
#   n=k-n
if k-n<n:
  n=k-n
  
print(n)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

if N%K == 0:
    print(0)
if N%K != 0 and N > K :
    a = N%K
    b = K%a
    if a >= b :
        print(b)
    if a < b:
        print(a)
if N%K != 0 and N < K :
    c = K-N
    if c > N:
        print(N)
    if c < N:
        print(c)

Submission status: Accepted

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

a, b = divmod(N, K)

if K == 1:
    print('0')
else:
    result = min(N, b, abs(K - b))
    print(result)

Submission status: Accepted

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

Submission status: Accepted

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

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

tmp = n%k

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

Submission status: Accepted

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

i = N // K
N = N - K * i
A = N
B = -(A-K)
ans = min(A,B)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = list(map(int, input().split()))
if N == K:
    print(0)
    exit()
elif N < K:
    ans = min(N, K - N)
    print(ans)
    exit()
elif N > K:
    temp1 = N - N // K * K
    temp2 = abs(temp1 - K)
    ans = min(temp1, temp2)
    print(ans)
    exit()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
if  N % K == 0:
    print(0)
    exit()
m = N // K + 1
print(min(m * K - N, N - (m - 1) * K))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
n = n % k
while n >= k / 2:
    n = abs(n - k)
print(n)

Submission status: Accepted

Solution code:
import sys
n,k = map(int,input().split())
if n%k == 0:
	print(0)
	sys.exit()
num = n - k*(n//k)
abnum = abs(num - k)
print(min(num,abnum))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N, K = input().split(' ')
NA = int(N)
K = int(K)
if NA - K > K:
  NA = NA - K*(NA//K - 1)
N1 = abs(NA-K)

while N1 < NA:
  NA = N1
  N1 = abs(N1-K)
else:
  print(NA)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#n=int(input())
n,k=map(int,input().split())
#al=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]

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

Submission status: Accepted

Solution code:
import math  # noqa: F401
from typing import Dict, List, Optional, Tuple, Union  # noqa: F401


def main(N: int, K: int):
    if N == K:
        print(0)
        return

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

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


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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:

n,k=map(int,input().split())
min_s=10**18

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

if N % K == 0:
  answer = 0
else:
  if abs(N % K - K) >= N % K:
    answer = N % K
  else:
    answer = abs(N % K - K)

  
print(answer)

Submission status: Accepted

Solution code:

from math import gcd
n,k=map(int,input().split())
print(min(n%k,k-n%k))

Submission status: Accepted

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

# 先にギリギリまで数を減らしておく
x = N - (N // K) * K

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

print(x)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = map(int, open(0).read().split())

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def abc161_C(N,K):
    if K == 1:
        return print(0)
    Min = 0
    N = N%K
    diff_list = [N]
    while 1:
        diff = abs(N - K)
        N = diff
        if diff in diff_list:
            return print(min(diff_list))
        elif diff < K/2:
            diff_list.append(diff)
            return print(min(diff_list))
        else:
            diff_list.append(diff)
    
N,K = map(int,input().split())
abc161_C(N,K)

Submission status: Accepted

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

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

print(N)

Submission status: Accepted

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

print(min(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:
N, K = map(int, input().split())

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


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


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


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

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


def main():
    solve()


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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 = map(int, input().split())
x = N % K

while x > K/2:
    x = abs(K-x)

print(x)

Submission status: Accepted

Solution code:
# n, m, l = map(int, input().split())

# list_n = list(map(int, input().split()))

# n = input()
# list = [input() for i in range(N)

# list = [[i for i in range(N)] for _ in range(M)]

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

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

Submission status: Accepted

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

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

s, a = divmod(n, k)

t = k - a

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

Submission status: Accepted

Solution code:
N,K =map(int,input().split())
if(N%K==0):
    print(0)
else:
    count = N//K
    print(min(abs(N-K*(count+1)),abs(N-K*(count))))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int,input().split());
i = 0;
if n == k:
    print(0);
elif n < k:
    print(min(n,k-n));
else:
    v = (n - k)%k;
    if v*2 >= k:
        print(k-v);
    else:
        print(v);

Submission status: Accepted

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

Submission status: Accepted

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

t = N % K

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

Submission status: Accepted

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

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


print(ans)

Submission status: Accepted

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

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 = map(int,input().split())
m = N % K
print(min(m,K - m))

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=map(int,input().split())

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math
N, K = map(int, input().split())
if N == K:
    ans = 0
elif N < K:
    ans=min(N,K-N)
else:
    ansmax = N%K
    ansmin = K-ansmax
    ans = min(ansmin, ansmax)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k=map(int, input().split())
# nがkよりも大きい場合毎回kで減っていって、kよりも小さくなってからは２つの値を行ったり来たりするよ
#となると、まず、kで割った場合の余りを出して、

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

Submission status: Accepted

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

ans = 0
while True:
    if n >= k:
        n = n % k
        if n == 0:
            break
    else:
        if abs(n-k) < n:
            n = abs(n-k)
        break

print(n)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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=map(int,input().split())
print(min(abs(N-(K*(N//K))),abs(N-(K*((N//K)+1)))))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(min(x, y))

Submission status: Accepted

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

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

Submission status: Accepted

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

x = n // k
n -= (x-1) * k
nmin = n
for _ in range(3):
   n = abs(n - k)
   nmin = min(n, nmin)
print(nmin)

Submission status: Accepted

Solution code:
import math

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

if n > k:
   n = n % k
    
if n*2 <= k:
  print(n)
else:
  print(k-n)

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 = map(int, input().split())

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

Submission status: Accepted

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

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

Submission status: Accepted

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

n = abs(n - k)

if k == n:
    print(0)
    sys.exit()
elif k > n:
    print(min(n, k-n))
    sys.exit()
else:
    while n > k:
        n %= k
    print(min(n%k, abs(n-k)))

Submission status: Accepted

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

Submission status: Accepted

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

a = N % K
b = K - a

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

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 = map(int,input().split())

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

Submission status: Accepted

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

amari=N%K
tmp=abs(amari-K)

Ans=min(amari,tmp)
print(Ans)

Submission status: Accepted

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

seen = {N}
while True:
    if N >= K:
        N = N % K
    else:
        N = K - N
    if N in seen:
        break
    seen.add(N)
print(min(seen))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n, k = map(int, input().split())
    x = n // k
    x2 = x + 1
    xr = abs(n - x * k)
    x2r = abs(n - x2 * k)
    r = min(xr, x2r)
    print(r)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

while True:
  if n > k:
    n = n - (n//k)*k
  num = abs(n-k)
  if num > n:
    print(n)
    exit(0)
  else:
    n = num

Submission status: Accepted

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

if N >= K:
  A = N//K
  B = N - A*K
  print(min([B, abs(B-K)]))
else:
  if K >= 2*N:
    print(N)
  else:
    print(abs(N-K))

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

Submission status: Accepted

Solution code:
n,K=map(int,open(0).read().split())

if n%K==0:
  print(0)
elif n%K!=0:
  a = n%K
  if a>abs(a-K):
    a = abs(a-K)
    print(a)
  elif a<=abs(a-K):
      print(a)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
'''
while True:
  tar = abs(n - k)
  if (tar)
'''

diff = n - k
div = n // k

if (diff == 0):
    ans = 0
elif (diff < 0):
    ans = min(n, abs(n - k))
else:
    temp = n - div * k
    ans = min(temp, abs(temp - k))


print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
n=n%k
flag=n
li=[n]
for i in range(10**10):
  n=abs(n-k)
  li.append(n)
  if n==flag:
    break
  
print(min(li))

Submission status: Accepted

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

if n % k == 0:
  print(0)
elif n / k != 0:
  min = n % k
  if min > abs(min - k):
    min = abs(min - k)
  print(min)
elif n > abs(n - k):
  print(abs(n - k))
else:
  print(n)

Submission status: Accepted

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

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

Submission status: Accepted

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

m = N
t = -1
s = -1
N = N%K
if N < m:
    m = N
while(t!=N):
    t = s
    s = N
    N = abs(N-K)
    if N < m:
        m = N
print(m)

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=list(map(int,input().split()))
n=n%k
m=k-n
print(min(m,n))

Submission status: Accepted

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

ans1=abs(N-N//K*K)
ans2=abs(N-(N//K+1)*K)

print(min(ans1,ans2))

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())
ans = 0

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

print(ans)

Submission status: Accepted

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

print(t)

Submission status: Accepted

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

print(min(ans1, ans2))

Submission status: Accepted

Solution code:
import math
def action(x,K):
    x=abs(x-K)
    return x
N,K=map(int,input().split())
if N>K:
    N=N-math.floor((N-K)/K)*K
n=N
while True:
    N=action(n,K)
    if N>n:
        print(n)
        break
    n=N

Submission status: Accepted

Solution code:
def resolve():
    N, K = map(int, input().split())
    if N <= K/2:
        print(N)
    elif N <= K:
        print(abs(N-K))
    else:
        N = N%K
        if N <= K/2:
            print(N)
        elif N <= K:
            print(abs(N-K))
resolve()

Submission status: Accepted

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

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

Submission status: Accepted

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