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: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
x = n
r = 0
pre_x = x
while True:
	if k == 1 or n == 0:
		break
	pre_x = x
	x = abs(x - k)
	if pre_x < x:
		r = pre_x
		break
print(r)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
lis = []
while lis.count(abs(n-k)) == 0:
    lis.append(abs(n-k))
    n = abs(n-k)
print(min(lis))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys

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

if n%k==0:
  print(0)
  
else:
  while True:
    if abs(n-k)<=be:
      be=abs(n-k)
    else:
      print(be)
      sys.exit()
    n=abs(n-k)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

saitei = N
for i in range(N//K+2):
    N = abs(N-K)
    saitei = min(saitei, N)

print(saitei)

Submission status: Time Limit Exceeded

Solution code:
nk=list(map(int,input().split(' ')))
while(True):
    if abs(nk[0]-nk[1])<nk[0]:
        nk[0]=abs(nk[0]-nk[1])
    else:
        break
print(nk[0])

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

N, K = map(int, input().split())
diff = abs(K - N)

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#C
N,K = map(int,input().split())
old = N
while True:
    new = abs(K-old)
    if old < new:
        break
    else:
        old = new
print(old)

Submission status: Time Limit Exceeded

Solution code:
#!python3

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


def main():
    x = N
    while True:
        y = max(x - K, K - x)
        if y < x:
            x = y
        else:
            break
    print(x)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
# K > 1 かつ　N > Kの場合は、N - N // K * K をスタートにできる
if K > 1:
  N = N - N // K * K
else:
  N = 0
minimum = N
count = 0
#print(N)
while count <= N / K:
  N = abs(N - K)
  minimum = min(N, minimum)
  count += 1
print(minimum)

Submission status: Accepted

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

print(min(n-k*(n//k),-n+k*(n//k+1)))

Submission status: Accepted

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

if a > 0:
    ans = N - K * a
    a = ans // K
    b = ans % K

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
a,b=[int(i) for i in input().split()]
c=a//b
e=abs(a-(b*c))
f=abs(a-(b*(c+1)))
print(min(e,f))

Submission status: Accepted

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

if a <= abs(a-K):
    print(a)
else:
    print(abs(a-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())

a = n % k
if (a <= int(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:
n,k = map(int,input().split())
check = []
check.append(n%k)
check.append(k-check[0])
print(min(check))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

N, Y = map(int, input().split())
ans = min(N%Y, Y-(N%Y))
print(ans)

Submission status: Accepted

Solution code:
def sep():
    return map(int,input().strip().split(" "))
def lis():
    return list(sep())
import sys
sys.setrecursionlimit(10**6)

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8
a,b = map(int,input().split())
yon = 100000000000000000000000
a = a%b
yon = min(a,abs(a-b))
print(yon)

Submission status: Accepted

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


def solve(N: int, K: int):
    ans = N
    N = abs(N - K * (N // K))
    if ans > N:
        ans = N
    N = abs(N -K)
    if ans > N:
        ans = N


    print(ans)
    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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:
#!/usr/bin/env python3
import sys


def solve(N: int, K: int):
    ans = N
    N =  abs(N - K * (N // K))
    if ans > N:
        ans = N
    for i in range(3):
        N = abs(N - K)
        if ans > N:
            ans = N


    print(ans)
    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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())

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# import math
# import statistics
# import itertools
# a=int(input())
# b=input()
# c=[]
# for i in a:
#     c.append(int(i))
N,K= map(int,input().split())
# f = list(map(int,input().split()))
# g = [int(input()) for _ in range(N)]
# h = []
# for i in range(a):
#     h.append(list(map(int,input().split())))
# a = [[0] for _ in range(H)]#nizigen

if N%K==0:
    print(0)
else:
    et=N//K
    a1=et*K
    a2=(et+1)*K
    mi = min(abs(a1-N),abs(a2-N))
    print(mi)

Submission status: Accepted

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

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())
count = N//K
N = abs( N - count*K )
N_2 = abs( N - K )
print( N if N <= N_2 else N_2 )

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

mod1 = N % K
mod2 = (K - mod1)

print(mod1 if mod1 < mod2 else mod2)

Submission status: Accepted

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

if N==K:
  print(0)

elif N<K:
  lis1=[N,abs(N-K)]
  print(min(lis1))
  
else:
  lis2=[N%K,abs(N%K-K)]
  print(min(lis2))

Submission status: Accepted

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

y = N%K
z = abs(K-y)

ans = min(y,z)

print(ans)

Submission status: Accepted

Solution code:
def N():
    return int(input())
def L():
    return list(map(int,input().split()))
def NL(n):
    return [list(map(int,input().split())) for _ in range(n)]
import math

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

Submission status: Accepted

Solution code:
a,b=[int(s) for s in input().split()]
print(min([a%b,(b-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:
a,b=[int(s) for s in input().split()]
print(min([a%b,(b-a)%b]))

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

N = N % K
nN = abs(N - K)
while(nN < N):
    N = nN
    nN = abs(N-K)

print(N)

Submission status: Accepted

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

Submission status: Accepted

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

L = []



for i in range(10**5):
    M = N // K
    if i == 0:
        N = abs(N - K)
        L.append(N)
    else:
        if N > K:
            N = N - (K * M)
            L.append(N)
        else:
            N = abs(L[i-1] - K)
            L.append(N)


print(min(L))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

x = N
pre_x = x

if x // K > 0:
    x = abs(x - K * (x // K))
    pre_x = x

while True:
    pre_x = x
    x = abs(x - K)

    if pre_x < x:
        print(pre_x)
        exit()

Submission status: Accepted

Solution code:
import numpy

n, m = input().split()
n = numpy.int64(n)
m = numpy.int64(m)
n = n % m
print(min(n, m-n))

Submission status: Accepted

Solution code:
import numpy

n, m = input().split()
n = numpy.int64(n)
m = numpy.int64(m)
n = n % m
print(min(n, m-n))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(n)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8

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

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

n,k = map(int,input().split())
x = k+(n%k)
min = sys.maxsize

while True:
    if x%k == 0:
        min = 0
        break

    x = abs(x-k)
    if x == min :break
    elif x < min : min = x

print(min)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
import copy
a = [int(c) for c in input().split()]
N=a[0]
K=a[1]

#まずK*10*i<Nとなる最大のiを探す

while True:
    i = 0
    while K*10**(i+1)<N:
        i+=1
    N = abs(N-K*10**i)
    if K*10 >= N:
        break
Nmin = 10**18
while True:
    N = abs(N-K)
    if Nmin>N:
        Nmin=N
    else:
        print(Nmin)
        sys.exit(0)

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

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

print (min)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k =map(int,input().split())
if n//k != 0:
        n -= (n//k)*k
else:
        pass
def p(n):
        if n > abs(n-k):
                return True
        else:
                return False
while p(n):
        n = abs(n-k)

print(n)

Submission status: Accepted

Solution code:
# C
N, K = map(int, input().split())
ans = 10**10
mod = N % K
ans = min(mod, abs(mod-K))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
INF = int(10e20)


def main():
    N, K = tuple([int(_x) for _x in input().split()])

    minval = N
    sub = N // K - 5
    if sub > 0:
        N = N - K * sub

    for i in range(20):
        N = abs(N - K)
        minval = min(N, minval)

    print(minval)


main()

Submission status: Accepted

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

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 = N % K
if a > K / 2:
  print(K - a)
else:
  print(a)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline

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

if __name__=="__main__":
    main()

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

Submission status: Accepted

Solution code:
# アルゴリズムは回答を見て作成
n,k = map(int,input().split())
x = n
t = n%k
ans = min(t, k-t)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = [int(x) for x in input().split()]
a = n // k
n -= a * k
print(min(n,abs(n-k)))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

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

Submission status: Accepted

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

if n < k:
    rival_n = k - n
    if n >= rival_n:
      print(rival_n)
    else:
      print(n)

elif n == k:
    print(0)

else:

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(solve(N,K))

Submission status: Accepted

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

Submission status: Accepted

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# C - Replacing Integer

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

m = n % k
l = map(abs,[m-k,m,m+k])
print(min(l))

Submission status: Accepted

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

if k <= n:
    if n%k == 0:
        ans = 0
    else:
        tmp = n//k
        ans = min(abs((n-k*tmp)), abs((n-(k*(tmp+1)))))
else:
    ans = min(abs(n), abs(n-k))

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:

x,y = map(int,input().split())
d = x % y
d_ = y - d
if d > d_:
  print(d_)
else:
  print(d)

Submission status: Accepted

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

Submission status: Accepted

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

top = n - n//k*k
low = n - ((n//k)+1)*k

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

if N > K:
  N = N % K

min_N = float("inf")
while min_N != N:
  min_N = min(N,min_N)
  if N == K or N == 0 or K == 0:
    print(0)
    exit(0)
  
  N = abs(N - K)
  
print(min_N)

Submission status: Accepted

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

Submission status: Accepted

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

t = n // k
re = n - t * k


if abs(re - k) < re:
    re = abs(re - k)
    i = True

print(re)

Submission status: Accepted

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

t = n // k
re = n - t * k

i = True

while i == True:
    if abs(re - k) < re:
        re = abs(re - k)
        i = True
    else:
        i = False

print(re)

Submission status: Accepted

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

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

Submission status: Accepted

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

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())
ans = N % K

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

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

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,K = map(int,input().split())
i = N // K
N = N - K * i

ans = N
while True:
  N = abs(N - K)
  if ans > N:
    ans = N
  else:
    break
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
def input():
    return sys.stdin.readline()[:-1]

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

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

    print(ans)

if __name__ == '__main__':
    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 = map(int, input().split())

ans = n
if abs(n-k) > k:
    x = abs(n-k)//k
    n = abs(n-k) - x*k
ans = min(ans, n, abs(n-k), abs(abs(n-k)-k))
print(ans)

Submission status: Accepted

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

a=N//K

ans=min(abs(N-a*K), abs(N-(a+1)*K))
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())

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

Submission status: Accepted

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

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 or K-t
t = N % K
ans = min(t, K-t)
 
print(ans)

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

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

print(ans)

Submission status: Accepted

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

n = n % k

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

Submission status: Accepted

Solution code:
def manipulate(x, k):
	return abs(x - k)

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

while n > k / 2:	
	n = manipulate(n, k)
	
print(n)

Submission status: Accepted

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

cand_1 = N % K
cand_2 = K - cand_1

min = min(cand_1, cand_2)

print(min)

Submission status: Accepted

Solution code:
#C
N, K = map(int,input().split())
if N%K==0:
    print(0)
else:
    print(min(N%K,-(N%(K)-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())

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

    if n == k:
        ans = 0
    elif n > k:
        a = n // k
        ans = min(n - a * k, abs(n - (a + 1) * k))
    elif n < k:
        ans = min(n, k - n)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

n = x[0]
k = x[1]

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

Submission status: Accepted

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

valueA = N % K
valueB = K - valueA

if valueA > valueB:
  print(valueB)
else:
  print(valueA)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n, k = map(int, input().split())
    if n - k > k:
        n_k = (n-k) // k
        minv = n - k * n_k
    else:
        minv = n

    def min_x(x):
        minv = x
        for _ in range(2):
            if abs(minv - k) < abs(minv):
                minv = abs(minv - k)
            else:
                print(minv)
                exit()
        print(minv)

    min_x(minv)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    n, k = map(int, input().split())
    if n - k > k:
        n_k = (n-k) // k
        minv = n - k * n_k
    else:
        minv = n

    def min_x(x):
        minv = x
        for _ in range(3):
            if abs(minv - k) < abs(minv):
                minv = abs(minv - k)
            else:
                print(minv)
                exit()
        print(minv)
        
    min_x(minv)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys
from collections import deque
import numpy as np
import math
sys.setrecursionlimit(10**6)
def S(): return sys.stdin.readline().rstrip()
def SL(): return map(str,sys.stdin.readline().rstrip().split())
def I(): return int(sys.stdin.readline().rstrip())
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def solve():
    x = n%k
    ans = min(n,abs(x-k),x)
    print(ans)
    return

if __name__=='__main__':
    n,k = IL()
    solve()

Submission status: Accepted

Solution code:
def main():
    N,K = map(int,input().split())
    syo = N//K
    amari = N%K
    N = abs(amari-K)
    ans = min(N,amari)
    return ans
    

print(main())

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
list1 = []
if N <= abs(N - K):
  print(N)
  exit()
else:
  N = N % K
  while N > abs(N-K):
    N = abs(N - K)
    list1.append(N)
  
if list1 ==[]:
  print(0)
else:
  print(min(list1))

Submission status: Accepted

Solution code:
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=[int(x) for x in input().split()]
if n%k==0:
  print(0)
else:
  m=n//k
  a=abs(n-m*k)
  b=abs(a-k)
  ans=min(a,b)
  print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

abc161c_replacing_integer()

Submission status: Accepted

Solution code:
def resolve():
    N, K = list(map(int, input().split()))
    boundleft = N%K
    boundright = K-N%K
    print(min(boundleft, boundright))

if '__main__' == __name__:
    resolve()

Submission status: Accepted

Solution code:
import sys
import numpy as np
import numba
from numba import jit

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

@jit
def main(n, k):
    n = n%k
    return min(n, abs(n-k))


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

print(main(n, k))

Submission status: Accepted

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

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

Submission status: Accepted

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

if ans>k//2:
    print(abs(ans-k))
elif ans<=k//2:
    print(ans)

Submission status: Accepted

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

ans = 0

ans_1 = N//K
ans_2 = ans_1 + 1

ans = min(abs(N-ans_1*K), abs(N-ans_2*K))
print(ans)

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

if n%k==0:
  print(0)
elif n<=k:
  print(min(n,k-n))
else:
  num=n%k
  print(k-num)