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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
nk = [int(x) for x in input().split()]
n = nk[0]
k = nk[1]
n %= k
print(min(n, abs(n-k)))

Submission status: Accepted

Solution code:
def main():
    I = [int(_) for _ in input().split()]
    N = I[0]
    K = I[1]

    before = N % K
    after = abs(before - K)
    while before > after:
        before = after
        after = abs(after - K)

    print(before)



if __name__ == "__main__":
    main()

Submission status: Accepted

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

flag = 0
if K == 1:
    print(0)
    quit()
if K == 2:
    if N % 2:
        print(1)
        quit()
    else:
        print(0)
        quit()

i = N // K
N = N - K * i
while True:
    i = N // K
    ab = abs(N - K)
    if N > ab:
        N = ab
    else:
        break

print(N)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
min_n = n
min_1 = n % k
min_2 = abs(min_1 - k)
min_arr = [min_n, min_1, min_2]
print(sorted(min_arr)[0])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    N, K = map(int, rl().split())
    if N % K == 0:
        print(0)
        exit()

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


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

N = N % K

if (N == 0):
    print(N)
elif (N == K):
    print(0)
elif (K == 1):
    print(0)
else:
    while (N >= 0):
        if (N > abs(N - K)):
            N = abs(N - K)
        else:
            print(N)
            break

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections, math

local = False
if local:
    file = open("inputt.txt", "r")

def inp():
    if local:
        return file.readline().rstrip()
    else:
        return input().rstrip()

def ints():
    return [int(_) for _ in inp().split()]


N, K = ints()

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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


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

Submission status: Accepted

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

r = n % k
d = abs(k - r)

ans = r
if r > d:
    ans = d
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
a = N // K
s = N - a*K
t = (a+1)*K - N
if s >= t:
  print(t)
else:
  print(s)

Submission status: Accepted

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

N = N % K

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

print(N)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(n)

Submission status: Accepted

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

if k == 1:
    print(0)
    exit()
    
if n > k:
    n = n%k
    print(min(n, abs(n-k)))
else:
    print(min(n, abs(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:
nn, kk = input().split(" ")
n = int(nn)
k = int(kk)
 
remain = n % k
if( kk=="1" ):
  print(0)
elif( remain == 0 ):
  print(remain)
else:
  print( min(abs( remain - k), abs(abs(remain-k)-k) ))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

amari = N % K

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

a=n%k
for i in range(10):
    s=a
    a=abs(a-k)
    if s<a:
        print(s)
        break

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:
li = list(map(int, input().split()))
n = li[0]
k = li[1]
a = n % k
b = abs(a - k)
if a >= b:
    print(b)
else:
    print(a)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:

import collections


class Solution:
    def solve(self, n, k):
        rem = n % k
        return min(rem, abs(rem-k))


sol = Solution()

[n, k] = list(map(int, input().strip().split()))
print(sol.solve(n, k))

Submission status: Accepted

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

ans = 0
if N!=K and K>1:
    if N < K:
        ans = min(N,abs(N-K))
    else:
        ans = min(N % K,abs((N % K)-K))
if N==K:
    ans = 0
print(ans)

Submission status: Accepted

Solution code:

# スペース区切りの整数の入力
N,K= map(int, input().split())
''' a1=abs(N)
a2=abs(N-K)
da=a1-a2
N=a2 '''
''' while da>=0:
    a1=abs(N)
    a2=abs(N-K)
    da=a1-a2
    N=a2 '''
ansN=N//K
ans=min(N-ansN*K,-N+(ansN+1)*K)

print(ans)

Submission status: Accepted

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

x = N

if x > K:
    x %= K

if K - x < x:
    x = K - x

print(x)

Submission status: Accepted

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

N = N % K
a = 0
while a == 0:
    b = N
    if N >= K:
        N = N - K
    else:
        N = K - N
    if N > b:
        a = 1

print(b)

Submission status: Accepted

Solution code:
# coding:utf-8
import math
import fractions

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = list(map(int, input().split(" ")))
x = n % k
y = abs(x - k)

if x > y:
    print(y)
else:
    print(x)

Submission status: Accepted

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

start = 0
end = N

ans = float('inf')
while start <= end:
	mid = start + (end - start) //2
	ans = min(abs(mid*K-N),ans)
	if mid*K == N:
		break
	elif mid*K>N:
		end = mid-1
	else:
		start = mid+1
		
print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
q, mod = divmod(N, K)
if mod == 0:
    print(0)
else:
    if mod*2 > K:
        print(abs(mod-K))
    else:
        print(mod)

Submission status: Accepted

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

a = n//k
c = abs(n-a*k)
d = abs(n-(a+1)*k)

if c >= d:
    s = d
else:
    s = c
    
print(s)

Submission status: Accepted

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

for i in range(2):
  if N >= K:
    N = N % K
  
  else:
    if K < N*2:
      N = K % N
    
print(N)

Submission status: Accepted

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

if n > m:
    n %= m

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

Submission status: Accepted

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

key = n % k


while True:
  d = key - k
  if d < 0:
    d *= -1
  if d >= key:
    break
  else:
    key = d
print(key)

Submission status: Accepted

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

Submission status: Accepted

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

if n <= k // 2:
    print(n)

else:
    sho = n // k
    n -= k * sho
        
    for i in range(1000000000000000000000):
        n = abs(n - k)
        if n <= k // 2:
            break

    print(n)

Submission status: Accepted

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

num1 = N%K
num2 = abs(num1-K)

if num1>=num2:
    print(num2)
else:
    print(num1)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from fractions import gcd

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

if N > K:
    N %= K

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
inputted = input().split()
x = int(inputted[0])
k = int(inputted[1])
buf = 0

p = x % k
q = k - p

if p > q:
    print(q)
else:
    print(p)

Submission status: Accepted

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

Submission status: Accepted

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

if N<=K:
    while K<2*N:
        N=abs(N-K)
    print(N)
else:
    R=N%K
    print(min(R,K-R))

Submission status: Accepted

Solution code:
[n, k] = [int(i) for i in input().split()]
min = n%k
tmp = -1 * (min - k)
if min <= tmp:
    print(min)
else:
    print(tmp)

Submission status: Accepted

Solution code:
#n=int(input())
n,k=map(int,input().split())
#l=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]
ans=0
import fractions
newval=n%k
if n>=k:
    if n%k==0:
        ans=0
    else:
        #ans=fractions.gcd(n,k)
        ans=n%k
        templ=[]
        ans=min(ans,abs(ans-k))
else:
    #ans=fractions.gcd(n,k)
    ans=n%k
    ans=min(ans,abs(ans-k))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main(n,k):
    val, mod = divmod(n,k)
    x = n - (k*val)
    y = abs(x - k)
    return min(x,y)

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

Submission status: Accepted

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

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


##########
if __name__ == "__main__":
    c()

Submission status: Accepted

Solution code:
# -*- coding utf-8 -*-
n, k = map(int, input().split())
if k==1:
    print(0)
    exit()
n = n%k
if n <=k/2:
    print(n)
else:
    print(-1*(n-k))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n, k = tuple(map(int, input().split()))
if n > k:
    n = n%k
while(True):
    n = abs(n-k)
    if(n < abs(n-k)):
        break
print(n)

Submission status: Accepted

Solution code:
import sys

N,K = map(int, input().split())
tmp = N
while(1):
    if(N%K<N):
        N = N%K
    elif(N%K == N):
        if(abs(N-K) <= N):
            N = abs(N-K)
        else:
            print(N)
            sys.exit()

Submission status: Accepted

Solution code:
import math

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

amari = n % k
n = amari

ab = abs(n - k)

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

print(n)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

amari=nk[0]%nk[1]

ab=abs(nk[1]-amari)

if ab<amari:
    print(ab)
else:
    print(amari)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

# line = ' 7 4'
# N, K = [int(x) for x in line.split()]
N, K = [int(x) for x in input().split()]


v1 = N % K
v2 = v1 - K

ans = min([v1, -v2])
print(ans)

Submission status: Accepted

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

ans = N % K
ans2 = (ans - K) * -1

if ans2 < ans:
    print(ans2)
else:   #ans2 <= ans1
    print(ans)

Submission status: Accepted

Solution code:

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

seen = set()

while N not in seen:
    seen.add(N)

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

print(min(seen))

Submission status: Accepted

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

Submission status: Accepted

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

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

n %= k
m = abs(n - k)

print(min(m, n))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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


if N == K or K == 1:
    ans = 0
else:
    a = N % K
    ans = abs(a - K)
    if a <= ans:
        ans = a

print(str(ans))

Submission status: Accepted

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

r=0

if(N<K):
  if(abs(N-K)<N):
    r=abs(N-K)
  else:
    r=N

elif(K==1):
   r=0

elif(K<N):
  a=N//K
  N=N-K*a
  while(0<N):
    r=abs(N-K)
    N=N-K

print(r)

Submission status: Accepted

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

if n < abs(n-k):
    pass

elif n > k:
    a = n // k
    n = n - a*k
    while abs(n-k) <= n:
        n = abs(n-k)

else:
    while abs(n-k) <= n:
        n = abs(n-k)


print(n)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
x = n % k
while x ** 2 >= (x - k) ** 2:
    x -= k
print(x if x >= 0 else x * (-1))

Submission status: Accepted

Solution code:
n, k = [int(i) for i in input().split()]
if n % k  == 0:
    print(0)
elif n < k:
    print(min(n, k-n))
else:
    n = n - int(n//k) * k
    n = min(abs(n-k), n)
    n = min(abs(n-k), n)
    print(n)

Submission status: Accepted

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

min_num = N % K if N % K < abs(N % K - K) else abs(N % K - K)

print(str(min_num))

Submission status: Accepted

Solution code:
N,K = map(int, input().split())
b = N // K
min = N - (K*b)
while True:
  if abs(min - K) > min:
    print(min)
    break
  min = abs(min-K)

Submission status: Accepted

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

def solve(init: int, K: int, acc: int):
    while abs(init - K) < init:
        init = abs(init - K)
    return init

    # temp = abs(init - K)
    # if temp > acc:
    #     return init
    # else:
    #     return solve(temp, K, temp)



def main():
    N, K = map(int, input().rstrip().split())
    N -= K * (N  // K)
    res = solve(N, K, N)
    print(res)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

a = n
if n<=k:
    a = min([n,k-n])
else:
    a = min([n%k,k-n%k])
print(a)

Submission status: Accepted

Solution code:

(n, k) = [int(s) for s in input().split()]

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

ans = 0

if r == 0:
    ans = 0
elif r >= b:
    ans = b
else:
    ans = r

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n, m = map(int, input().split())
    n = abs(n - (n // m) * m)
    n = min(n, abs(n - m))
    print(n)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,k=map(int, input().split())
d=abs(n-k)
t=n//k
if n%k==0:
    print(0)
elif n>d:
    a=n-t*k
    if a>abs(a-k):
        print(abs(a-k))
    else:
        print(a)
else:
    print(n)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

s = N % K

t=abs(K-s)

a=[s,t,N]
ans = min(a)

if t>N:
  ans=N
else:
  ans=t

#print(s,t,N)

if s==0 or t==0 or N==0:
  ans=0

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
import math
if N > K:
    s = N // K
    print(min(abs(N - s * K), abs(N - (s + 1) * K)))
elif N == K:
    print(0)
else:
    print(min(N, abs(N - K)))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

for i in range(N//K+1):
    if min_num >= abs(min_num - K):
        min_num = min_num - K
    else:
        break

print(abs(min_num))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

if k >= 2 * n:
    print(n)
    quit()

if k >= n and k < 2*n:
    print(abs(n-k))
    quit()

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

Submission status: Accepted

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

r1= n%k;

r2=k-r1;

print(min(r1,r2))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
    n,k = map(int,input().split(' '))
    n = n % k
    key = 0
    data = []
    while(key == 0):
        check = abs(n - k)
        for per in data:
            if(check == per):
                key = 1
        if(key == 0):
            data.append(check)
            n = check
        else:
            key = 1
    print(min(data))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
input_line = [int(i) for i in input().split()]
n = input_line[0]
k = input_line[1]

x = (n // k) - 1
ans = [abs(n - ((x + i) * k)) for i in range(3)]

print(min(ans))

Submission status: Accepted

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

if N%K == 0:
    print(0)
elif K%N==0:
    print(N)
else:
    a = N%K
    b = K*(N//K+1)-N
    print(min(a,b))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

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

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
absn = 0
checker = True
while checker:
    if n > k:
        n = n % k
    else:
        absn = abs(n - k)
        if n > absn:
            n = absn
        else:
            checker = False
print(n)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def func(a,b):
    if a <= b:
        return a
    else:
        return func(b,abs(b-K))

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

Submission status: Accepted

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

Submission status: Accepted

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

cal = n % k
if (k - cal) < cal:
    cal = k - cal
print(cal)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
ans=0
if n==k:
    print(0)
    exit()
elif n>k:
    if n%k==0:
        print(0)
        exit()
    else:
        ans+=n%k
else:
    ans+=n


while True:
    a=abs(ans-k)
    if a>=ans:
        print(ans)
        exit()
    else:
        ans=a

Submission status: Accepted

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

amari = n % k

diff = abs(amari - k)

print(min(amari, diff))

Submission status: Accepted

Solution code:
from math import ceil,floor
N,K = map(int,input().split())
print(min(N%K,K-N%K))

Submission status: Accepted

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

div_p = N//K
div_m = N//K+1


print(min(abs(N-K*div_p), abs(N-K*div_m)))

Submission status: Accepted

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

while True:
    n %= k
    dif = abs(n-k)
    old_n = n
    if dif >= n:
        break
    n = dif
    if n==old_n:
        break

print(n)

Submission status: Accepted

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

t = []
n = n%k

while n != 0:
    n = abs(n-k)
    if n in t or n == 0:
        print(min(t))
        exit()
    t.append(abs(n))
print(0)

Submission status: Accepted

Solution code:
def main():
    n,k = map(int,input().split())
    div = n//k
    A = abs(n-k*div)
    B = abs(n-k*(div+1))
    if A < B:
        x = A
    else:
        x = B
    print(x)
if __name__ == '__main__':
    main()

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(N%K)
    else:
        print(abs(K - N%K))

Submission status: Accepted

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

if K == 1:
    print(0)
    exit()
elif N == 0:
    print(0)
    exit()    
else:
    a = N-K*(N//K)
    b =abs(N-K*(N//K +1))
    print(min(a,b))

Submission status: Accepted

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

s = {x}
if 0 in s:
    ans = 0
else:
    while True:
        x = abs(x - K)
        if x == 0:
            ans = 0
            break
        if x in s:
            ans = min(s)
            break
        s.add(x)

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

q, r = n // k, n % k

print(min(abs(n - (q + 1) * k), r))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    N, K = list(map(int, input().split()))
    r = N%K
    if r < abs(r-K):
        print(r)  
    else:
        print(abs(r-K))

Submission status: Accepted

Solution code:

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

flag = "No"
if k == 1:
  n = 0
else:
  n = n % k

for i in range(1, 1000000000000000000):
  if abs(n - k) > n:
    print(n)
    flag = "Yes"
    break
  else:
    n = abs(n - k)
if flag == "No":
  print(n)

Submission status: Accepted

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

t=n%k

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

Submission status: Accepted

Solution code:
import sys,math,copy,queue,itertools,bisect
from fractions import gcd
sys.setrecursionlimit(10**7)
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
_LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
INF = 10**18
MOD = 10**9 + 7
DD = ((1,0),(0,1),(-1,0),(0,-1))
N,K = LI()
if K == 1:
    print (0)
    exit (0)
if N == 0:
    print (0)
    exit(0)

if N == 1:
    print (1)
    exit(0)
N = N % K
while True:
    NN = abs(N - K)
    if NN >= N:
        print (N)
        exit (0)
    N = NN

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

n = N % K
# print(n)

r = K - n
# print(r)

if n <= r:
  print(n)
else:
  print(r)

Submission status: Accepted

Solution code:
def fun(N, K):
    if N%K > K:
        return K
    return fun(K, N%K)

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

Submission status: Accepted

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

def solve(N: int, K: int):
    if K<N:
        ans=min(N%K, abs(N%K-K))
    elif K<2*N:
        ans=K-N
    else:
        ans=N
    print(ans)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    solve(N, K)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

x=n%k
y=k-x
if(x<y):
  print(x)
else:
  print(y)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
if N == K:
    print(0)
    exit()

tmp1 = 0
if N > K:
    tmp, m = divmod(N, K)
    tmp1 = m
    tmp1 = min(tmp1, abs(m - K))
else:
    tmp = abs(N - K)
    tmp1 = min(N % K, tmp)

print(tmp1)

Submission status: Accepted

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

j = i[0] % i[1]
k = i[1] -j

print(min(j,k))