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: Wrong Answer

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

    remainder = n % k
    if remainder == 0:
        print(remainder)
        return

    print(remainder)


minValue()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

    if k == 1 or n == k:
        print(0)
        return

    diff = abs(n - k)
    print(n % diff)


minValue()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

remainder = n % k
print(remainder)

Submission status: Wrong Answer

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

print(N,K)    

x = N

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

Submission status: Wrong Answer

Solution code:
list = input().split(" ")

startNum, standardNum = int(list[0]), int(list[1])

mod = startNum % standardNum
minusMod = abs(mod - standardNum)

result = mod if mod <= standardNum else minusMod

print(result)

Submission status: Wrong Answer

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

first = int(N % K)
second = int((N + K / 2) % K)

if second < first:
  print(second)
else:
  print(first)

Submission status: Wrong Answer

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

first = N % K
second = (N + K / 2) / K

if second < first:
  print(second)
else:
  print(first)

Submission status: Wrong Answer

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

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

bef=-1
x =0
count = 0
Bool=True
if N%K < K:
	bef=N%K
	Bool=False
while Bool:
	if count>0:
		N=bef
	x=abs(N-K)
	count+=1
	if bef>=0 and bef<=x:
		break
	bef=x

print(bef)

Submission status: Wrong Answer

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

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

bef=-1
x =0
count = 0

while True:
	if count>0:
		N=bef
	x=abs(N-K)
	count+=1
	if bef>=x:
		break
	bef=x


print(x)

Submission status: Wrong Answer

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

for i in range(100000):
    x = abs(mini - K)
    if x < mini:
        mini = x
        
print(mini)

Submission status: Wrong Answer

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

for i in range(10000):
    x = abs(mini - K)
    if x < mini:
        mini = x
    

print(mini)

Submission status: Wrong Answer

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

mod = N % K


if K == 1:
    mini = 0
    
elif N >= K:
    mini = mod
    
elif N < K and abs(N-K) > N:
    mini = N
else:
    
    for i in range(1000000):
        x = abs(mini - K)
        if x < mini:
            mini = x
    

print(mini)

Submission status: Wrong Answer

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

mod = N % K


if K == 1:
    mini = 0
    
elif N >= K:
    mini = mod
    
elif N < K and abs(N-K) > N:
    mini = N
else:
    
    for i in range(10000):
        x = abs(mini - K)
        if x < mini:
            mini = x
    

print(mini)

Submission status: Wrong Answer

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

A = N % K

if (0 < A <= K):
    A = A - K
    print(abs(A))
else:
    print(A)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
n,k = map(int,input().split())
print(gcd(n,k))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if (0 > N or 10**18 < N):
    raise ValueError("error!")
if (1 > K or 10**18 < K):
    raise ValueError("error!")

if (N <= K):
    print(N)
    exit()

A = N % K

if (0 < A <= K):
    A = A - K
    print(abs(A))
else:
    print(A)

Submission status: Wrong Answer

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

if (0 >= N and 10**18 <= N):
    raise Exception
if (1 > K and 10**18 <= K):
    raise Exception

if (N <= K):
    print(N)
    exit()

A = N % K

if (0 < A <= K):
    A = A - K
    print(abs(A))
else:
    print(A)

Submission status: Wrong Answer

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

if (N <= K):
    print(N)
    exit()

A = N % K

if (0 < A <= K):
    A = A - K
    print(abs(A))
else:
    print(A)

Submission status: Wrong Answer

Solution code:
str = input().split()
n = int(str[0])
k = int(str[1])
t = n%k
other = abs(k-t)
print(min(t,other))
if t <= other:
    print(t)
else:
    print(other)

Submission status: Wrong Answer

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

rot = n//k
x = n

if n == 1 or n == k or n%k == 0:
  x = 0
elif rot >= 1:
  x = n - k*rot
  
t = x%k

if n == 0:
  print(k)
else:
  print(min(t, abs(t-k)))

Submission status: Wrong Answer

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

rot = n//k
x = n

if n == 1 or n == k or n%k == 0:
  x = 0
elif rot >= 1:
  x = n - k*rot
  
t = x%k

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

rot = n//k
x = n
if n == 1 or n == k or n%k == 0:
  x = 0
elif rot >= 1:
  for _ in range(rot):
    x = abs(x - k)
t = x%k

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

Submission status: Wrong Answer

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

rot = n//k
x = n
if n == 1 or k == 1 or n == k:
  x = 0
elif rot >= 1:
  for _ in range(rot):
    x = abs(x - k)
t = x%k

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

Submission status: Wrong Answer

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

rot = n//k
x = n
if n == 1 or k == 1 or n == k:
  x = 0
elif rot >= 1:
  for _ in range(1, rot):
    x = abs(x - k)
t = x%k

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

Submission status: Wrong Answer

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

import sys
sys.setrecursionlimit(300000)


def solve(N: int, K: int):
    def gcd(a, b):
        return a if b == 0 else gcd(b, a % b)
    ret = 0
    if N <= K:
        ret = min(K - N, N)
    else:
        if N % K == 0:
            ret = 0
        else:
            ret = gcd(N, K)
    print(ret)
    return

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: Wrong Answer

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

import sys
sys.setrecursionlimit(300000)


def solve(N: int, K: int):
    def gcd(a, b):
        return a if b == 0 else gcd(b, a % b)
    ret = 0
    if N <= K:
        ret = min(K - N, N)
    else:
        g = gcd(N, K)
        if K == 1:
            ret = 0
        elif g == 1:
            ret = 1
        elif N % g == 0:
            ret = 0
        else:
            ret = g
    print(ret)
    return

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: Wrong Answer

Solution code:
a = input()
a, b = a.split(' ')
a = int(a)
b = int(b)

t = a % b

if a > b:
    print(t)
    exit()

print(t if t < b - t else b)

Submission status: Wrong Answer

Solution code:
a = input()
a, b = a.split(' ')
a = int(a)
b = int(b)

a = a % b

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def replace_abs(x, k):
    return abs(x - k)

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

Submission status: Wrong Answer

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

def replace_abs(x, k):
    return abs(x - k)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

print (n%k)

Submission status: Wrong Answer

Solution code:
import sys
from fractions import gcd


def input():
    return sys.stdin.readline().strip()


N, k = map(int, input().split())
a = gcd(N, k)
if k == 1:
    print(0)
else:

    if a == k:
        print(min(abs(N - k), N))
    else:
        print(a)

Submission status: Wrong Answer

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

# easy cases
if K == 1:
    print(0)
if N == 0:
    print(K)

# find smallest difference with N > K (the value N % K is equivalent to applying N := N-K repeatedly)
N = N % K 

# minimum is either N or N - K (for the case where K > N)
minimum = min(N, abs(N-K))

print(minimum)

Submission status: Wrong Answer

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

if N < K:
    print(N)
    exit(0)

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

Submission status: Wrong Answer

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

if N < K:
    print(N)
    exit(0)

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

ans = n % k
s = n // 2
if s >= ans:
    print(ans)
else:
    print(abs(ans - s))

Submission status: Wrong Answer

Solution code:
def gcd(x, y):
    return x if y == 0 else gcd(y, x % y)

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

Submission status: Wrong Answer

Solution code:
def gcd(x, y):
    return x if y == 0 else gcd(y, x % y)

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

Submission status: Wrong Answer

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

if n <= n%k:
    print(n)
elif dev <= n%k:
    print(dev)
else:
    print(n%k)

Submission status: Wrong Answer

Solution code:
N,K=map(int, input().split()) 
import math
y = math.floor(N/K)
x = N - y*K
ans = min(x, abs(x-K))
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K=map(int, input().split()) 
import math
y = math.floor(N/K)
x = N - y*K
#x = N%K
ans = min(x, abs(x-K))
print(ans)

Submission status: Wrong Answer

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

y = int(N/K)
x = N - y*K

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

Submission status: Wrong Answer

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

if N>K:
  y = int(N/K)
  x = N - y*K
else:
  x = N 

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

Submission status: Wrong Answer

Solution code:
l = list(map(float,input().split()))
c = abs(l[0]-l[1])
x = l[0]
set_a = set()
if c>l[0]:
    print(int(l[0]))
else:
    while x>0:
        c = abs(c-l[1])
        set_a.add(c) 
        x -= 1
    print(int(min(list(set_a))))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

x = abs(N - K*(int(N/K)-1))
ans = N

for i in range(0,5):
    x = abs(x-K)
    ans = min(ans,x)

print(ans)

Submission status: Wrong Answer

Solution code:
import math
N,K = map(int, input().split())
ans = N
for i in range(int(N/K)-2,math.ceil(N/K)+2):
    ans = min(ans,abs(N-K*i))

print(ans)

Submission status: Wrong Answer

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

print(min(abs(N-K*int(N/K)),abs(N-K*math.ceil(N/K)),min(abs(N-(1+K*int(N/K))),abs(N-K*(1+math.ceil(N/K))),N)))

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
import math
n, k = map(int, input().split())
if n <= k: print(min(n, k - n))
else:
    t = math.ceil((n - k) / k - 10 ** -13)
    n = n - t * k
    print(min(n, k - n))

Submission status: Wrong Answer

Solution code:
import math
n, k = map(int, input().split())
if n <= k: print(min(n, k - n))
else:
    t = math.ceil((n - k) / k - 10 ** -11)
    n = n - t * k
    print(min(n, k - n))

Submission status: Wrong Answer

Solution code:
import math
n, k = map(int, input().split())
if n <= k: print(min(n, k - n))
else:
    t = math.ceil((n - k) / k - 10 ** -9)
    n = n - t * k
    print(min(n, k - n))

Submission status: Wrong Answer

Solution code:
import math
n, k = map(int, input().split())
if n <= k: print(min(n, k - n))
else:
    t = math.ceil((n - k) / k - 10 ** -7)
    n = n - t * k
    print(min(n, k - n))

Submission status: Wrong Answer

Solution code:
import math
n, k = map(int, input().split())
if n <= k: print(min(n, k - n))
else:
    t = math.ceil((n - k) / k - 10 ** -6)
    n = n - t * k
    print(min(n, k - n))

Submission status: Wrong Answer

Solution code:
import math
n, k = map(int, input().split())
if n <= k: print(min(n, k - n))
else:
    t = math.ceil((n - k) / k - 10 ** -5)
    n = n - t * k
    print(min(n, k - n))

Submission status: Wrong Answer

Solution code:
import math
n, k = map(int, input().split())
if n <= k: print(min(n, k - n))
else:
    t = math.ceil((n - k) / k - 10 ** -1)
    n = n - t * k
    print(min(n, k - n))

Submission status: Wrong Answer

Solution code:
import math
n, k = map(int, input().split())
if n <= k: print(min(n, k - n))
else:
    t = math.ceil((n - k) / k - 10 ** -2)
    n = n - t * k
    print(min(n, k - n))

Submission status: Wrong Answer

Solution code:
import math
n, k = map(int, input().split())
if n <= k: print(min(n, k - n))
else:
    t = math.ceil((n - k) / k - 10 ** -3)
    n = n - t * k
    print(min(n, k - n))

Submission status: Wrong Answer

Solution code:
def main():
    import math
    N,K = map(int,input().split())
    r,l = math.modf(N/K)
    if l == 0:
        result = N
    else:
        result = int(min(N-l*K, K*(l+1)-N))
    print (result)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
if N >1000000000:
    N = N - int(N/1000000)*1000000
syou = int(N/K)
min_1 = N - syou*K
min_2 = -(N - (syou+1)*K)
if min_1 < min_2:
    print(min_1)
else :
    print(min_2)

Submission status: Wrong Answer

Solution code:
import math
n, k = map(int, input().split())
if n <= k: print(min(n, k - n))
else:
    t = math.ceil((n - k) / k - 10 ** -8)
    n = n - t * k
    print(min(n, k - n))

Submission status: Wrong Answer

Solution code:
import math
N,K = map(int,input().split())
r,l = math.modf(N/K)
if l == 0:
    result = N
else:
    result = int(min(N-l*K, K*(l+1)-N))
print (result)

Submission status: Wrong Answer

Solution code:
import math
n, k = map(int, input().split())
if n <= k: print(min(n, k - n))
else:
    t = math.ceil((n - k) / k - 10 ** -4)
    n = n - t * k
    print(min(n, k - n))

Submission status: Wrong Answer

Solution code:
import math
n, k = map(int, input().split())
if n <= k: print(min(n, k - n))
else:
    t = math.ceil((n - k) / k + 10 ** -4)
    n = n - t * k
    print(min(n, k - n))

Submission status: Wrong Answer

Solution code:
import math
n, k = map(int, input().split())
if n <= k: print(min(n, k - n))
else:
    t = math.ceil((n - k) / k + 10 ** -10)
    n = n - t * k
    print(min(n, k - n))

Submission status: Wrong Answer

Solution code:
import math
n, k = map(int, input().split())
if n <= k: print(min(n, k - n))
else:
    t = math.ceil((n - k) / k - 10 ** -10)
    n = n - t * k
    print(min(n, k - n))

Submission status: Wrong Answer

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

a = N%K
b = K-N%K

if K > N:
  print(N)
elif a > b:
  print(a)
elif N >= 10**18 or K >= 10**18:
  print(0)
else:
  print(b)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
p = 1
if N > 100000000:
    N /= 10000000
    K /= 10000000
    p = 0
syou = int(N/K)
min_1 = N - syou*K
min_2 = -(N - (syou+1)*K)
if p == 0:
    if min_1 < min_2:
        print(int(min_1*10000000+0.5))
    else :
        print(int(min_2*10000000+0.5))
else :
    if min_1 < min_2:
        print(min_1)
    else :
        print(min_2)

Submission status: Wrong Answer

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

if n>=k:
    if n%k != 1 and n%k != 2 and n%k != 0:
        print(abs(k-n%k))
    else:
        print(n%k)
elif n<k:
    print(n)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# coding: utf-8
# import pprint
# import numpy as np
# import code
# import functools

# if __debug__:
#     def debug(arg):
#         pass
# else:
#     import sys
#     def debug(arg):
#         debug = lambda *x: None
#         print(arg, file=sys.stderr)

n, k = (int(x) for x in input().split())
n = n - (n // k) * k
print(n)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
if N > 100000000:
    N /= 10000000
syou = int(N/K)
min_1 = int(N - syou*K + 0.5)
min_2 = -int(N - (syou+1)*K - 0.5)
if min_1 < min_2:
    print(min_1)
else :
    print(min_2)

Submission status: Wrong Answer

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
arr = list(map(int, input().rstrip().split()))
a = arr[0]
b = arr[1]
c = a - b
c = abs(a - b)
d = c - b
if d <= 0:
    d = abs(c - b)
else:
    if d > 0:
        d = 0
print(min(c, d))

Submission status: Wrong Answer

Solution code:
arr = list(map(int, input().rstrip().split()))
a = arr[0]
b = arr[1]
c = a - b
c = abs(a - b)
d = c - b
if d < 0:
    d = abs(c - b)
else:
    if d >= 0:
        d = 0
print(min(c, d))

Submission status: Wrong Answer

Solution code:
arr = list(map(int, input().rstrip().split()))
a = arr[0]
b = arr[1]
c = a - b
c = abs(a - b)
d = c - b
if d < 0:
    d = abs(c - b)
else:
    if d > 0:
        d = 0
print(min(c, d))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
syou = int(N/K)
min_1 = N - syou*K
min_2 = -(N - (syou+1)*K)
if min_1 < min_2:
    print(min_1)
else :
    print(min_2)

Submission status: Wrong Answer

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from fractions import gcd
N, K = [int(x) for x in input().split()]
if N % K == 0:
    print(0)
else:
    print(gcd(N, K))

Submission status: Wrong Answer

Solution code:
N,K=(int(x) for x in input().split())
x=N
if(x >= K):
    t=N%K
    x=min(t,abs(t-K))

print(x)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
N, K = [int(x) for x in input().split()]
print(gcd(N, K))

Submission status: Wrong Answer

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

if n>=k:
    if n%k != 1 and n%k != 2 and n%k != 0:
        print(abs(k-n%k))
    else:
        print(n%k)
elif n<k:
    if n == 0:
        print(n)
    else:
        if k%n != 1 and k%n != 2 and k%n != 0:
            print(abs(n-k%n))
        else:
            print(n)

Submission status: Wrong Answer

Solution code:
def absolute(N,K):
    return abs(N-K)
    
N,K=map(int,input().split())
#N=N%K
valuelist = [N%K]

for i in range (0,3):
    i+=1
    N=absolute(N,K)
    valuelist.append(absolute(N,K))
    
    
print(min(valuelist))

Submission status: Wrong Answer

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

if n>=k:
    if n%k != 1 and n%k != 2 and n%k != 0:
        print(abs(k-n%k))
    else:
        print(n%k)
elif n<k:
    if n == 0:
        print(n)
    else:
        if k%n != 1 and k%n != 2 and k%n != 0:
            print(abs(k%n))
        else:
            print(n)

Submission status: Wrong Answer

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

N = NK[0]
K = NK[1]

old = max(NK)
new = max(NK) - 1

answer = []

temp = []
temp.append(N)

kouho = N // K

while old > new:
    temp.append(abs(temp[-1] - K * kouho))
    old = new
    new = min(temp)
    answer.append(new)

while old > new:
    temp.append(abs(temp[-1] - K))
    old = new
    new = min(temp)
    answer.append(new)

if N == K:
    print(N)
else:
    print(min(answer))

Submission status: Wrong Answer

Solution code:
def absolute(N,K):
    return abs(N-K)
    
N,K=map(int,input().split())
N=N%K
valuelist = [N%K]

for i in range (0,1):
    i+=1
    N=absolute(N,K)
    valuelist.append(absolute(N,K))
    
   
print(min(valuelist))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(x)

Submission status: Wrong Answer

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

def replace(N,K):
    if(N<K):
        return N
    else:
        a = N%K
        if (abs(a-K)<a):
            a = abs(a-K)
    return a

print(replace(N,K))

Submission status: Wrong Answer

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

def replace(N,K):
    if(N<K):
        return N
    else:
        a = N%K
        if (abs(a-K)<K and abs(a-K)<a):
            a = abs(a-K)
    return a

print(replace(N,K))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(x)

Submission status: Wrong Answer

Solution code:
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

from math import ceil

n, k = na()
print(min(n % k, abs(ceil(n / k) * k - n)))

Submission status: Wrong Answer

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

def replace(N,K):
    if(N<K):
        return N
    else:
        a = N%K
        if (abs(a-K)<K):
            a = abs(a-K)
    return a

print(replace(N,K))

Submission status: Wrong Answer

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

def replace(N,K):
    if(N<K):
        return N
    else:
        a = N%K
        if (abs(a-K)<K):
            a = abs(N-K)
    return a

print(replace(N,K))

Submission status: Wrong Answer

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

def replace(N,K):
    if(N<K):
        return N
    else:
        a = N%K
        if (abs(a-K)<K):
            a = abs(N-K)
        else:
            a = abs(N-K) + abs(N-a)  
    return a

print(replace(N,K))

Submission status: Wrong Answer

Solution code:
import sys
import math

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

pre=n-math.floor(n/k)*k

while 1:
    if pre <= abs(pre-k):
        print(pre)
        sys.exit()
    pre = abs(pre - k)

Submission status: Wrong Answer

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

def replace(N,K):
    if(N<K):
        return N
    else:
        N = N%K
        if (abs(N-K)<K):
            N = abs(N-K)
    return N

print(replace(N,K))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def replace(N,K):
    if(K == 1):
        return 0
    elif(N<K):
        return N
    else:
        N = N%K
        if (abs(N-K)<K):
            N = abs(N-K)
        else:
            return N
    return N

print(replace(N,K))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def replace(N,K):
    if(K == 1):
        return 0
    elif(N<K):
        return N
    else:
        N = N%K
        if (abs(N-K)<K):
            N = abs(N-K)
        else:
            return 0
    return N

print(replace(N,K))

Submission status: Wrong Answer

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

per = math.floor(num/k)
count = 0

for i in range(num):
    if math.fabs(num-(per+i)*k) > math.fabs(num-(per+(i+1))*k):
        count += 1
    else:
        break

print(math.floor(math.fabs(num-(per+count)*k)))

Submission status: Wrong Answer

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

def replace(N,K):
    if(K == 1):
        return 0
    elif(N<K):
        return N
    else:
        N = N%K
        if (abs(N-K)<K):
            N = abs(N-K)
    return N

print(replace(N,K))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def replace(N,K):
    if(K == 1):
        return 0
    elif(N<K):
        return N
    else:
        N = N%K
        if (abs(N-K)<=K):
            N = abs(N-K)
    return N

print(replace(N,K))

Submission status: Wrong Answer

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

def replace(N,K):
    if(K == 1):
        return 0
    elif(N<K):
        return N
    else:
        N = N%K
        if (abs(N-K)<=K):
            N = abs(N-K)
    return

print(replace(N,K))

Submission status: Wrong Answer

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

def replace(N,K):
    if(K == 1):
        return 0
    else:
        N = N%K
        if(N==0):
            return N
        if (abs(N-K)<K):
            N = abs(N-K)
    return N

print(replace(N,K))

Submission status: Wrong Answer

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

per = math.floor(num/k)
count = 0


if num-per*k > math.fabs(num-(per+1)*k):
    print(math.fabs(num-(per+1)*k))
else:
    print(num-per*k)

Submission status: Wrong Answer

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

def replace(N,K):
    if(K == 1):
        return 0
    else:
        N = N%K 
        if (abs(N-K)<K):
            N = abs(N-K)
    return N

print(replace(N,K))

Submission status: Wrong Answer

Solution code:
import math
n,k=map(int,input().split())
num=math.floor(n/k)
n=n-num*k
ans=min(abs(k-n),n)
print(ans)

Submission status: Wrong Answer

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

def replace(N,K):
    if(K == 1):
        return 0
    elif(N<K):
        return N
    else:
        N = N%K 
        if (abs(N-K)<K):
            N = abs(N-K)
    return N

print(replace(N,K))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def replace(N,K):
    if(K == 1):
        return 0
    elif(N<K):
        return N
    else:
        N = N%K 
        if (abs(N-K)<K):
            N = abs(N-K)
        return N

print(replace(N,K))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

K = N%X

if K >= X:
  print(X)
else:
  print(K)

Submission status: Wrong Answer

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

def replace(N,K):
    if(K == 1):
        return 0
    elif(N<=K):
        return N
    else:
        N = N%K 
        if (abs(N-K)<K):
            N = abs(N-K)
        return N

print(replace(N,K))

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
if K == 1 or N == 0 or N == K or N%K==0:
    print(0)
elif K == 0 or K > N:
    print(N)
elif N == 1 or N == K-1 or K == N-1 or N%K==1:
    print(1)
elif N > K:
    res = abs(N - (K*(N//K)))
    if abs(res - K) < res:
        print(abs(res-K))
    else:
        print(res)

Submission status: Wrong Answer

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

def replace(N,K):
    if(K == 1 or N==K):
        return 0
    elif(N<K):
        return N
    else:
        N = N%K 
        if (abs(N-K)<K):
            N = abs(N-K)
        return N

print(replace(N,K))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
if n<k:
    for i in range(n):
        num=int(abs(n-k))
        if num<n:
            n=num
        else:
            break
else:
    t=n%k
    n=min(t,k)
print(n)

Submission status: Wrong Answer

Solution code:
nums = list(map(int, input().split()))
n = nums[0]
k = nums[1]

x  = n % k

print(min(n, k - n))

Submission status: Wrong Answer

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

print(N % K)

Submission status: Wrong Answer

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

def replace(N,K):
    if(K == 1 or N==K):
        return 0
    elif(N<K):
        return N
    else:
        N = N%K 
        if (N-K<0 and abs(N-K)<K):
            N = abs(N-K)
        return N

print(replace(N,K))