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:
num=input().rstrip().split(" ")
num1=int(num[0])
num2=int(num[1])
if num1%num2==0:
  print(0)
else:
  date0=num1%num2
  date1=num2-date0
  if date0>date1:
    print(date1)
  else:
    print(date0)

Submission status: Accepted

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

Submission status: Accepted

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


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

Submission status: Accepted

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

N = N % K

if K - N > N:
  print(N)
  exit()
print(K - N)

Submission status: Accepted

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

amari = N % K  

if amari > (K-amari):
  answer = K-amari
else:
  answer = amari
  
print(answer)

Submission status: Accepted

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

print(min(N%K,cal))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
data = input().split()
n,k = int(data[0]),int(data[1])
print(min(n%k,k-n%k))

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n, k = [int(i) for i in input().split(" ")]
    while abs(n - k) < n and n != 0:
        n = n%k
        if n != 0:
            n = abs(n - k)
    print(n)

Submission status: Accepted

Solution code:
input_line = input().split(" ")
N,K,sol=int(input_line[0])%int(input_line[1])+int(input_line[1]),int(input_line[1]),10**18+1
for i in range(10):
    if sol>N:
        sol=N
    N=abs(N-K)
print(sol)

Submission status: Accepted

Solution code:
input_line = input().split(" ")
N,K=int(input_line[0]),int(input_line[1])
sol=10**18+1
N=N%K+K
for i in range(10):
    if sol>N:
        sol=N
    N=abs(N-K)
print(sol)

Submission status: Accepted

Solution code:
def CalAbs(a, b):
    sol=a-b
    if sol<0:
        sol=sol*(-1)
    return sol
    
input_line = input().split(" ")
N=int(input_line[0])
K=int(input_line[1])

sol=10**18+1
N=N%K+K
i=0
count=0
while i<=10:
    if sol>N:
        sol=N
    N=CalAbs(N,K)
    i=i+1
print(sol)

Submission status: Accepted

Solution code:
#絶対値を求める関数
def CalAbs(a, b):
    sol=a-b
    if sol<0:
        sol=sol*(-1)
    return sol
    
#桁数を求める関数    
def CalDigi(a):    
    digi=1
    flag=0
    while flag==0:
        a=a/10
        if a>=1:
            digi=digi+1
        else:
            flag=1
    return digi
    
#標準入力
input_line = input().split(" ")
N=int(input_line[0])
K=int(input_line[1])

rec=10**18+1

#Nが大きすぎる時、Kの倍数でそぎ落とす
N=N%K+K


i=0
times=100
count=0
while i<=times:
    if rec>N:
        rec=N
    N=CalAbs(N,K)
    if i==times:
     lastN=N
    i=i+1
if rec>N:
    rec=N
    

    

print(rec)

Submission status: Accepted

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

N = N % K

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

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:
def main():
    args = input().rstrip().split(' ')
    N, K = int(args[0]), int(args[1])
    last_point = N % K
    print(min(last_point, abs(last_point-K)))
    
if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a = list(map(int, input().split()))
b = a[0]%a[1]

if b > a[1]/2 :
  print (a[1]-b)
else:
  print (b)

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

Submission status: Accepted

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


if __name__ == "__main__":
    main()

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
inv_amari = abs(amari - k)
if amari <= inv_amari:
  print(int(amari))
else:
  print(int(inv_amari))

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=[int(x) for x in input().split()]
#Sequence decreases first then starts increasing.This point has to be identified.
prev,cur=n+1,n
if k<=0:
  print(n)
else:
	rem = n%k
	print(min(rem,abs(rem-k)))

Submission status: Accepted

Solution code:
def main():
    n,k = map(int,input().split())
    print(min(n%k,abs(n%k-k)))
if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n, k = list(map(int, readline().split()))
    n = divmod(n, k)[1]
    print(min(n, abs(n - k)))


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n, k = list(map(int, readline().split()))
    while n > divmod(n, k)[1]:
        n = divmod(n, k)[1]
    print(min(n, abs(n - k)))


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

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

print(n)

Submission status: Accepted

Solution code:
#スペース区切りの整数の入力
N, K = map(int, input().split())
if(N%K<K-N%K):
    print(N%K)
else:
    print(K-N%K)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())    
if N % K == 0:                      ##N/Kの余りが0だった時
    print(0)
else:                               ##それ以外は
    N = N % K                       ##NにN/Kの余りを代入
    if abs(N - K) < N:           ##N-Kの絶対値がNより小さいなら
        N = abs(N - K)              ##NはN-Kの絶対値
        print(N)
    else:
        print(N)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
if N == K:
    print(0)
else:
    N = N % K
    a, b, c = N, abs(N - K), abs(abs(N - K) - K)
    while not (a == b or a == c or b == c):
        a, b, c = b, c, abs(c - K)
    print(min(a, b, c))

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(x) for x in input().split(' '))
tmp = N % K
if tmp == 0:
  print(0)
elif N > tmp or N > abs(tmp-K):
  print(abs(tmp-K))
else:
  print(N)

Submission status: Accepted

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

Submission status: Accepted

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

N %= K

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
nk = input()
N = int(nk[:nk.index(" ")])
K = int(nk[nk.index(" ")+1:])
ans = N - K

if ans<0:
	ans*=-1
	if ans>N:
		ans = N
else:
	ans = N - K*(N//K)
	if (ans-K)*-1<ans:
		ans = (ans-K)*-1
print(ans)

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())
print(min([N, N%K, abs(K-N%K)]))

Submission status: Accepted

Solution code:
def main():
  n,k = [int(x) for x in input().split()]
  return min(n%k, k-n%k)

print(main())

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
print(min(n, min(n % k, abs(n % k - 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())
num1=N%K
num2=abs((N%K)-K)
ans=min(num1,num2)
print(ans)

Submission status: Accepted

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

c1 = n % k
c2 = -1 * (c1 - k)

print(min([c1, c2]))

Submission status: Accepted

Solution code:
n, k = map(int,input().split())
ans = 0
ans = min(n%k,abs(n%k-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:
import math
import string
import collections
from collections import Counter
from collections import deque


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


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]

# n=int(input())
# h=readints()
n, k = map(int, input().split())
if n % k < k-n % k:
    print(n % k)
else:
    print(k-n % k)

Submission status: Accepted

Solution code:
n, k = input().split()
n=int(n)
k=int(k)
if n % k > abs((n % k) - k):
    print(abs((n % k) - k))
else:
    print(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:
n,k=map(int,input().split())
x=n%k
while x>=0:
    x-=k
if x<0:
    x*=-1
y=x-k
if y <0:
    y*=-1
if x<y:
    print(x)
if x>y:
    print(y)
if x==y:
    print(x)

Submission status: Accepted

Solution code:
nk = input().split(' ')
n = int(nk[0])
k = int(nk[1])
n = n % k

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

Submission status: Accepted

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

Submission status: Accepted

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

n = N % K

if n != 0 and N > K:
    n = K % n
else:
    n = min(n, K - n)

print(n)

Submission status: Accepted

Solution code:
# abc161 c
n, k = [int(i) for i in input().split()]
temp1 = n % k
temp2 = abs(temp1 - k)
print(min(temp1, temp2))

Submission status: Accepted

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

import numpy as np
N = N % K
past = N

while(True):
    N = np.abs(N - K)
    if N >= past:
        break;
    past = N
print(past)

Submission status: Accepted

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

if k >= 2 * n:
    print(n)
    quit()
elif k >= n and k < 2*n:
    print(abs(n-k))
    quit()
else:
    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())

if k >= 2 * n:
    print(n)
    quit()
elif k >= n and k < 2*n:
    print(abs(n-k))
    quit()
elif 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())
X=N%K
if abs(K-X)<X:
    print(abs(K-X))
else:
    print(X)

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, K - N % K))

Submission status: Accepted

Solution code:
#入力受け取り
N, K=map(int, input().split())

#N=aK+bとした時のbを求める
b=N%K

#bとK-bの小さい方を出力
print(min(b, K-b))

Submission status: Accepted

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

d = n//k

n = n - d*k

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

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

Submission status: Accepted

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

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

if ans > abs(ans - K):
    print(abs(ans - K))
else:
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

N = N % K

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

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:
a,b=map(int, input().split())
a=a%b
if a==0:
    print(0)
else:
    arr=[]
    arr.append(a)
    while True:
        a=abs(a-b)
        if sum(x == a for x in arr)==1:
            break
        else:
            arr.append(a)

    print(min(arr))

Submission status: Accepted

Solution code:
def C():
    nk=list(map(int,input().split()))
    
    abs_val=abs(nk[0]-nk[1])
    
    while True:
        if abs_val>nk[1]:
            abs_val=abs_val%nk[1]
        elif abs_val<nk[1]:
            abs_val=min(abs_val,abs(abs_val-nk[1]))
            break
        else :
            # 0
            abs_val=abs_val-nk[1]
            break
    
    print(min(abs_val,nk[0]))
    
    
    
if __name__=='__main__':
    C()

Submission status: Accepted

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

Nn = N%M
Nm = abs(Nn - M)

if(Nn >= Nm):
    print(Nm)
else:
    print(Nn)

Submission status: Accepted

Solution code:
N,K = (int(i) for i in input().split())
T = N//K
N = N - (K * T)

N_1 = abs(N-K)

while N_1 < N :
    N = N_1
    N_1 = abs(N-K)
print(N)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

if amari <= amari_h:
        print(amari)
else:
        print(amari_h)

Submission status: Accepted

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

if N > K:
    amari  = N % K
    amari_h = abs(amari - K)

    if amari <= amari_h:
        print(amari)
    else:
        print(amari_h)
if N <= K:
    sub = abs(N - K)
    if N <= sub:
        print(N)
    else:
        print(sub)

Submission status: Accepted

Solution code:
N,K = list(map(int,input().split()))
divisor = N//K
if abs(N-(K*divisor)) < abs(N-(K*(divisor+1))):
	print(abs(N-(K*divisor)))
else:
	print(abs(N-(K*(divisor+1))))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve(n, k):
    return min(n % k, abs(n % k - k))

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

Submission status: Accepted

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


minV = (n%k) if (n%k) < (k-n%k) else (k-n%k)
print(minV)

Submission status: Accepted

Solution code:
inp = input().split()
inp = [int(i) for i in inp]

print(min(inp[1] - (inp[0] % inp[1]), inp[0] % inp[1]))

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

if n >= k:
    result = min(n % k, k - (n % k))
elif 2 * n >= k > n:
    n = abs(k - n)
    result = n
else:
    result = n

print(result)

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

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

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

Submission status: Accepted

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

ans1 = abs(N - (K * math.floor(N / K)))# 余り
ans2 = abs(N - (K * (math.floor(N / K)+1)))# 余り

ans1 = N % K
ans2 = K - N % K 
print(min(ans1,ans2))

Submission status: Accepted

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

N = N % K

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

Submission status: Accepted

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

t = n % k

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

Submission status: Accepted

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

i = n % k 
l.append(abs(i-k))
l.append(abs(l[0]-k))
print(min(l))

Submission status: Accepted

Solution code:
# from math import factorial,sqrt
# from itertools import permutations as permus
# from fractions import gcd
# from collections import deque,Counter
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

# import numpy as np
# from scipy.sparse.csgraph import shortest_path, dijkstra, floyd_warshall, bellman_ford, johnson
# from scipy.sparse import csr_matrix

# strlist = "abcdefghijklmnopqrstuvwxyz"
n,k = map(int,input().split())

val1 = n % k
val2 = abs(val1-k)
ans = min(val1,val2)

print(ans)
# print("{:.10f}".format(ans))
# print(*ans)   # unpackして出力。間にスペースが入る
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a = list(map(int,input().split()))
if a[0]<a[1]:
  print(str(min(a[0],a[1]-a[0])))
else:
  r=a[0]%a[1]
  print(str(min(r,a[1]-r)))

Submission status: Accepted

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

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

main()

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
if (n==k or k==1 or n==0):
    print(0)
    exit()
if (n<k):
    if (n <= k/2):
        print(n)
        exit()
    print(k-n)
    exit()
n=n%k
if (n <= k/2):
    print(n)
    exit()
print(k-n)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s=input()
ss=s.split()
n=int(ss[0])
k=int(ss[1])
print(min(n%k,k-n%k))

Submission status: Accepted

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

print(_min)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
arc = []

def tool(N, K):
    if N == K:
        arc.append(0)
        return True

    elif N < K:
        if K-N in arc:
            return True
        else:
            arc.append(K-N)
            return tool(K-N, K)

    else:
        if N % K in arc:
            return True
        else:
            arc.append(N % K)
            return tool(N % K, K)


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

    print min(arc)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

if N == K:
  print(0)
elif N > K:
  if N%K == 0:
    print(0)
  else:
    print(abs(K-N%K))
else:
  if N < K/2:
  	print(N)
  else:
    print(abs(N-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())
n=n%k
if n<=k//2:
    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:
def readInts():
    return list(map(int, input().split()))


n, m = readInts()
ans = min(n % m, abs(n % m - m))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

n=n%k
while n>=k/2:
  n=abs(n-k)
  
print(int(n))

Submission status: Accepted

Solution code:
import numpy as np
N, K = list(map(int,input().split()))
tmp = np.zeros(2)
tmp[0] = N % K
tmp[1] = -N % K
print(int(tmp.min()))

Submission status: Accepted

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

Submission status: Accepted

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

#main
t = N % K
ans = min(t, K - t)

#output
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline

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


'''
def DIF(N):
  global K
  return abs(N-K)

if N < DIF(N):
  print(N)
else:
  if N % K == 0:
    print(0)
  else:
    while N >= DIF(N):
      N = DIF(N)
    print(N)
'''

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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:
#!/usr/bin/env python3
import sys


def main():

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

    if n < k:
        print(min(n,abs(n-k)))
        sys.exit(0)
    if n % k == 0:
        print(0)
        sys.exit(0)
    
    print(min(abs(n%k-k),n%k))


main()

Submission status: Accepted

Solution code:
# C - Replacing Integer

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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:
import math
if __name__ == "__main__":
    N, K = map(int, input().split())
    if N == 0:
        ret = 0
        print(int(ret))
        exit(0)
    ret = N - N // K * K
    while ret > abs(ret-K):
        ret = abs(ret-K)
    print(int(ret))

Submission status: Accepted

Solution code:
import math
if __name__ == "__main__":
    N, K = map(int, input().split())
    if N == 0:
        ret = 0
        print(int(ret))
        exit(0)
    ret =  N % K
    while ret > abs(ret-K):
        ret = abs(ret-K)
    print(int(ret))

Submission status: Accepted

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

Submission status: Accepted

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

a = n % k
b = (a - k) * -1

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

Submission status: Accepted

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

if n > k :
    A = n % k
    B = abs(A - k)
    ans = min(A,B)
else :
    ans = min(n,abs(n-k))
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=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:
    ans=min(n%k,abs(k-n%k))
else:
    ans=min(k-n,n)
print(ans)

Submission status: Accepted

Solution code:
#ABC161C
import sys
N, M = map(int, sys.stdin.readline().split())
tmp = N%M
print(min(tmp, -(tmp-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())
r=n%k
print(min([r,k-r]))

Submission status: Accepted

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

#output
r = N//K
print(min(abs(N - K*(r+1)), abs(N-K*r)))

Submission status: Accepted

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

l = n // k

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

if n > k:
     n = n % k

ans = k - n
n = ans

while k - n != ans:
    ans = min(ans, k - n)
    n = k - n

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#
# Written by NoKnowledgeGG @YlePhan
# ('ω')
#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7
"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

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

def main():
  """
  例えば x = 100で、k=7みたいなとき
  最初はずっと x >= kの場合になる
  x = 2のときに、 2 - 7より、7 - 2となる
  最終値になり得るものは、最終的に
  2 - 5 - 2 - 5 - 2のどっちかの数になる
  
  2 というのは　N % K
  そうすると、 N % K = a と置くと、
  a, k - aをminすればよい
  
  """
  n,k = readInts()
  a = n % k
  ans = min(a, k - a)
  print(ans)
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
n, k = map(int, input().split())

print(n%k) if n%k < k-n%k else print(k-n%k)

Submission status: Accepted

Solution code:
from sys import setrecursionlimit, exit
setrecursionlimit(1000000000)

from heapq import heapify, heappush, heappop, heappushpop, heapreplace
from bisect import bisect_left, bisect_right

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

Submission status: Accepted

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

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

Submission status: Accepted

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

cnt = n // k;

n -= cnt * k;
best = n;
for i in range(5):
    best = min(best, abs(n - k));
    n = abs(n - k);

print(best);

Submission status: Accepted

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

print(N%K if N%K < abs(N%K-K) else abs(N%K-K))