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 count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count

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

def input_list_str():
  return list(map(str, input().split()))

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

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

# 2で割り切れる回数
def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c

# 素因数分解
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

def main():
  n,k = input_list()
  if n == k:
    print(0)
    exit()
  if k == 0:
    print(n)
    exit()
  if n > k:
    if n % k == 0:
      print(0)
      exit()
    if n % 2 == 0 and k % 2 == 0:
      if k > 2:
        print(2)
        exit()
      print(0)
      exit()
    if n % 2 != 0 and k % 2 == 0:
      one = abs(n-k)
      two = abs(one-k)
      print(min(one,two))
      exit()
      
    print(1)
  else:
    one = abs(n-k)
    two = abs(one-k)
    print(min(one,two))
  
import math
import fractions
import collections
import itertools
from functools import reduce
main()

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
i = 0
if K == 1:
    print(0)
else:
    results = [N]
    while N > min(results):
        N = N - K
        if N < 0:
            results.append(-N)
            N = -N
        else:
            results.append(N)
        i += 1
    print(min(results))

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(k)
    else:
        if n%k != 1 and n%k != 2 and n%k != 0:
            print(abs(k%n))
        else:
            print(n)

Submission status: Wrong Answer

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

t = input().split()
n = int(t[0])
k = int(t[1])

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if n < k:
  print(n)
else:
  temp = int(n/k)
  remain = n - (k*temp)
  if remain > 0:
    last = abs(remain-k)
    print(min(remain, last))
  else:
  	print(remain)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

result = abs(N - K)

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

Submission status: Wrong Answer

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

a = n / k
if a >= 1:
    b = n % k
    if b == 0:
        print(0)
    else:
        c = abs(b - k)
        if b < c:
            print(b)
        else:
            print(c)
else:
    print(n)

Submission status: Wrong Answer

Solution code:
# C - Replacing Integer
n, k = map((int),(input().split()))


count = (int)(n / k)

print((min((abs(n - k * count)), abs(n - k * (count + 1)), abs(n - k * (count + 2)))))

Submission status: Wrong Answer

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



while True:
    if n < k:
        print(n)
        break

    if n % k == 0:
        print(0)
        break

    n = n % k
    next_ans = n - k
    if abs(next_ans) > n:
        print(n)
        break
    else:
        print(abs(next_ans))
        break

Submission status: Wrong Answer

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

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

x = gcd(max(k, n), min(k, n))
if n % k == 0:
    print(0)
elif n < k:
    print(min(n, abs(n - k)))
else:
    print(x)

Submission status: Wrong Answer

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

if N<K:
    print(N)
elif N%K == 0 :
    print(0)
elif abs(N-K) < K:
    print(abs(N-K))
else:
    print(abs(N-(K*((N//K)+1))))

Submission status: Wrong Answer

Solution code:
min1=0
min2=0
def amb(n,k):
    min1=abs(n%k)
    min2=abs(min1-k)
    if min1==0:
        print(0)
    elif min1==min2:
        print(min1)
    elif min1>min2:
        print(min2)
    elif min1<min2:
        print(min1)

n,k=list(map(int,input().split()))
if n>k:
    amb(n,k)
elif n==k:
    print(0)
else:
    print(n)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
i = 0
if K == 1:
    print(0)
else:
    results = [N]
    while i < K:
        N = N - K
        if N < 0:
            results.append(-N)
            N = -N
        else:
            results.append(N)
        i += 1
    print(min(results))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
a=list(map(int, input().split()))
l=[a[0]]
if a[0]==1:
    print(0)
    exit()
elif a[0]==0:
    print(0)
    exit()
elif a[1]==1:
    print(0)
    exit()
def d(a,b):
    _1=a%b
    _2=b-(a%b)
    if _1<_2:
        print(_1)
    else:
        print(_2)
d(a[0],a[1])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=b=abs(n-k)
ans=n
if n%k==0:
 print("0")
 exit()
while a!=abs(b-k):
 b=abs(b-k)
 if b<ans:
  ans=b
print(ans)

Submission status: Wrong Answer

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

aaa = (n % k)

if aaa == 0:
  ret = 0
else:
  if n > k:
    ret = min(abs(aaa - k), aaa)
  else:
    if k % n == 0:
      ret = n
    else:
      ret = k % n

print(ret)

Submission status: Wrong Answer

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

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(0)
    else:
        if n%k != 1 and n%k != 2 and n%k != 0:
            print(abs(k%n))
        else:
            print(n)

Submission status: Wrong Answer

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


if N>K:
    print(N//K)

else:
    nums=list()
    nums.append(N)
    while abs(N-K)<N:
        N=abs(N-K)
        nums.append(N)
    print(min(nums))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
import fractions
if k == 1 or n == 0:
  print(0)
else:
  print(fractions.gcd(n, k))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
j = 0
i = 1
A = 1000000000000000001
B = 1000000000000000002
C = 1000000000000000003
if N<K:
    print(N)
elif N==K:
    print(0)
elif N%K==0:
    print(0)
else:
    N = N%K
    while j==0:
        #print(N)
        #print("aaa")
        N = N-K
        N = abs(N)
        if i%3==0:
            A = N
        elif i%3==1:
            B = N
        elif i%3==2:
            C = N
        
        if A==C:
            if A<=B:
                print(A)
            else:
                print(B)
            j+=1
        elif B==A:
            if A<=C:
                print(B)
            else:
                print(C)
            j+=1
        elif B==C:
            if B<=A:
                print(C)
            else:
                print(A)
            j+=1
        i+=1

Submission status: Wrong Answer

Solution code:
import sys


n,k= map(int,input().split())
v={}

min = sys.maxsize

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

if n > k :
    x = n%k
    if x > k:print(x);exit()
    else:
        n=x
        v[x] = True

while True:
    diff = abs(n- k)

    if v.get(diff):
        break
    else:
        # print(diff)
        v[diff] = True
        n = diff

        if diff < min:
            min = diff



print(min)

Submission status: Wrong Answer

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

if k == 1:
    print(0)

q, r = divmod(n, k)

if r == 0:
    print(0)
    exit()

if q >= 1:
    if (n - q * k) > (k - r):
        print(q + 1)
    else:
        print(q)
else:
    if n < (k - r):
        print(n)
    else:
        print(k - r)

Submission status: Wrong Answer

Solution code:
import math

if __name__ == "__main__":
    N, K = map(int, input().split())    
    ret =  N - (math.floor(N/K))*K
    if ret > abs(ret-K):
        ret = abs(ret-K)
    print(int(ret))

Submission status: Wrong Answer

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

if k==1:
    print(0)
elif n>k:
    print(k-(n%k))
else:
    print(k-(n%k))

Submission status: Wrong Answer

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

if N % K == 0:
    result = 0
elif N % K != 0 and N // K == 0:
    result = N
else:
    result = N // K

print(abs(result))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().strip().split())
a = 0
case = 0
while (n-k)>0:
    n = n-k
    case = case+1
if case>=1:
    n = abs(n-k)
else:
    n = n
print(n)

Submission status: Wrong Answer

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

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

print(replace(N,K))

Submission status: Wrong Answer

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

if n < k:
    print(min(n,k - n))
else:
    if n % k == 0:
        print(0)
    else:
        print(min(abs(n - k * int(n / k)),abs(abs(n - k * int(n / k))-k)))

Submission status: Wrong Answer

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

if N >= 2*K:
    if (N-2*K) % (2*K) == 0:
        N = abs(N - 2*K*(int((N-2*K)/(2*K)) + 0))
    else:
        N = abs(N - 2*K*(int((N-2*K)/(2*K)) + 1))

if   (N <= K) and (abs(K-N) > N):
    print(N)
elif (N <= K) and (abs(K-N) < N):
    print(abs(K-N))
else:
    print(min(N, abs(N-K), abs(2*K-N)))

Submission status: Wrong Answer

Solution code:
min1=0
min2=0
def amb(n,k):
    min1=abs(n%k)
    min2=abs(min1-k)
    if min1==0 or min2==0:
        print(0)
    elif min1==min2:
        print(min1)
    elif min1>min2:
        print(min2)
    elif min1<min2:
        print(min1)

n,k=list(map(int,input().split()))
if n>k:
    amb(n,k)
elif n==k:
    print(0)
else:
    print(n)

Submission status: Wrong Answer

Solution code:
import math
first_input = list(map(lambda x:int(x), input().split(" ")))
#second_input = list(map(lambda x:int(x), input().split(" ")))

N = first_input[0]
K = first_input[1]

print(math.floor(N // K))
print(K*math.floor(N // K))
if N>K:
    first_min_N = N - K*math.floor(N // K)
else:
    first_min_N = N

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

Submission status: Wrong Answer

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

a = n / k
if a > 1:
    b = n % k
    if b == 0:
        print(0)
    else:
        c = abs(b - k)
        if b < c:
            print(b)
        else:
            print(c)
else:
    print(n)

Submission status: Wrong Answer

Solution code:
import sys

N,K=map(int,input().split())
ZET=N
b=(10**18)
c=N
b1=N
for i in range(int(b)):
  if K==1:
    b1=0
    break
  if N>10**17 and K<10**9:
    b1=N%K
    break
  c=abs(b1-K)
  if int(b)<=c:
    break
  b1=c
  b=c
print(b1)

Submission status: Wrong Answer

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

ans_1 = n % k
ans_2 = abs(n-k)
ans = 0
if (ans_1 < ans_2):
    ans = ans_1
else:
    ans = ans_2

print(ans)

Submission status: Wrong Answer

Solution code:
def main(cell):

    n=cell[0]
    k=cell[1]
    if k==0:
        return n
    else:

        risk=int(abs(n/k))
        n=n-k*(risk-2)
        pren=n
        while 2>0:
            n=abs(n-k)
            if n >pren:
                break
            else:
                pren=n

        return pren
    


cell=list(map(int,input().split()))
pren=main(cell)
print(pren)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if N < K:
    print(N)

else:
    N = N % K
    for i in range(10):
        N = abs(N-K)

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

Submission status: Wrong Answer

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

num = N % K

if num == 0:
  print(0)
elif N < K:
  print(1)
else:
  min_num = abs(num - K)
  print(min_num)

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 or K%N == 0:
    print(0)
    exit()
elif K == 0:
    print(N)
    exit()
elif N == 1 or N == K-1 or K == N-1 or N%K == 1 or K%N == 1:
    print(1)
    exit()
else:
    res = []
    ans = N
    for i in range(max(N,K)):
        ans = ans - K
        res.append(ans)
        if i >= 5:
            if res[0] == res[2] or res[1] == res[3]:
                print(min(res))
                exit()
    print(min(res))

Submission status: Wrong Answer

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

if N==0:
    print(N)
elif N>=K:
    #print("1")
    x1=N%K
    x2=abs(N%K-K)
    print(min(x1,x2))
else:
    #print("2")
    print(N)

Submission status: Wrong Answer

Solution code:
# C - Replacing Integer
n, k = map((int),(input().split()))


count = (int)(n / k)

print((int)(min(abs(n - k * (count - 1)), abs(n - k * count), abs(n - k * (count + 1)))))

Submission status: Wrong Answer

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

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(0)
    else:
        if n%k != 1 and n%k != 2 and n%k != 0:
            print(abs(n-k))
        else:
            print(n)

Submission status: Wrong Answer

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


if N>K:
    print(N//K)

else:
    while abs(N-K)<N:
        N=abs(N-K)
        nums.append(N)
    print(min(nums))

Submission status: Wrong Answer

Solution code:
n,k = map(str, input().split(' '))
k = int(k)
n = int(list(n)[-1])
lis = []
if n==0: n=10
for i in range(10):
  lis.append(n)
  n-=k
  n=abs(n)
print(min(lis))

Submission status: Wrong Answer

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

if N<K:
    print(N)
elif N%K == 0 :
    print(0)
else:
    print(abs(N-(K*((N//K)+1))))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split(' '))
if n < k:
    r = n
else:
    if n % k == 0:
        r = 0
    else:
        r = n % k
        r1 = abs(r - k)
        if r > r1:
            r, r1 = r1, r
print(r)

Submission status: Wrong Answer

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

N = int(NK[0])
K = int(NK[1])

if N == K or K==1:
    print(0)

elif N > K:
    A = N % K
    if A > K-A:
        print(K-A)
    #elif A == K:
        #print(A)
    else:
        print(A)

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

Submission status: Wrong Answer

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

r = n%k
w = r%k
print(min(r,w))

Submission status: Wrong Answer

Solution code:

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

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

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

Submission status: Wrong Answer

Solution code:
min1=0
min2=0
def amb(n,k):
    min1=abs(n%k)
    min2=abs(min1-k)
    if min1==0 or min2==0:
        print(0)
    elif min1==min2:
        print(min1)
    elif min1>min2:
        print(min2)
    elif min1<min2:
        print(min1)

n,k=list(map(int,input().split()))
if n<k:
    print(n)
elif n==k:
    print(0)
else:
    amb(n,k)

Submission status: Wrong Answer

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

tmp = abs(N % K)

if tmp == 0:
    print(0)
elif N <= tmp:
    print(N)
else:
    print(K - tmp)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
if 0 <= N <= 10^18 and 1 <= K <= 10^18:
    N = abs(N-K)
    N = abs(N-K)
    print(N)
else:
    print("0")

Submission status: Wrong Answer

Solution code:
import sys

N,K=map(int,input().split())
ZET=N
b=(10**18)
c=N
b1=N
for i in range(int(b)):
  if K==1:
    b1=0
    break
  if N>10**17:
    b1=N%K
    break
  c=abs(b1-K)
  if int(b)<=c:
    break
  b1=c
  b=c
print(b1)

Submission status: Wrong Answer

Solution code:
import fractions

n, k = list(map(int, input().split(" ")))
if k == 1 or n == 1:
  print(0)
else:
  print(fractions.gcd(n, k))

Submission status: Wrong Answer

Solution code:
from fractions import gcd


def main():
    n, k = map(int, input().split())
    if k == 1 or n == 0 or n == k:
        print(0)
    elif n < k:
        print(min(n, k - n))
    elif n % k == 0:
        print(0)
    else:
        print(gcd(n, k))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

if N>=K:
    #print("1")
    x1=N%K
    x2=abs(N%K-K)
    print(min(x1,x2))
else:
    #print("2")
    print(N)

Submission status: Wrong Answer

Solution code:
k = input().split()
s = int(k[0])
j = int(k[1])
v = s + 1
while s > 10000 * j:
    s = s - 1000000000 * j
while v > s:
    v = s
    s = abs(s - j)

v = s
s = abs(s-j)
if v > s:
  print(s)
else:
  print(v)

Submission status: Wrong Answer

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

N = a[0]
K = a[1]

if K >= 2*N:
    print(N)
elif N >= K:
    print(N%K)
else:
    while 1:
        cul_result1 = abs(N-K)
        cul_result2 = abs(cul_result1-K)
        if cul_result1 < N and cul_result2 >=N:
            print(cul_result1)
            break
        elif cul_result1 < N and cul_result2 < cul_result1:
            N = cul_result1
            continue

Submission status: Wrong Answer

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

if N % K == 0:
    print('0')
elif K % N == 0:
    print(N)
elif N <= K:
    print(min(N, N-K))
elif N > K:
    if K % 2 == 0:
        print('1')
    else:
        base = K
        while N > K:
            K += base
        print(K % N)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if k==1:
    print(0)
elif n>k:
    print(k-(n%k))
else:
    print(k-n)

Submission status: Wrong Answer

Solution code:
def main(cell):

    n=cell[0]
    k=cell[1]
    risk=int(abs(n/k))
    n=n-k*(risk-2)
    pren=n
    while 2>0:
        n=abs(n-k)
        if n >pren:
            break
        else:
            pren=n

    return pren
    


cell=list(map(int,input().split()))
pren=main(cell)
print(pren)

Submission status: Wrong Answer

Solution code:
import math
first_input = list(map(lambda x:int(x), input().split(" ")))
#second_input = list(map(lambda x:int(x), input().split(" ")))

N = first_input[0]
K = first_input[1]

print(math.floor(N//K))
print(K*math.floor(N//K))
if N>K:
    first_min_N = N - K*math.floor(N//K)
else:
    first_min_N = N

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

Submission status: Wrong Answer

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

tmp = N % K

if tmp == 0:
    print(0)
elif N <= tmp:
    print(N)
else:
    print(K - tmp)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split(' '))
if 2 * N < K:
    print(N)
else:
    a = N // K
    mm = min(abs(N - a * K), abs(N - a * (K + 1)))
    print(mm)

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
import math
a = n/k
b = math.ceil(a)
if b <= 1:
  print(n)
else:  
  x = n-(k*b)
  print(abs(x))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
min_num = n
new_min = n % k
while True:
  new_min = new_min - k
  if new_min < 0:
    new_min *= -1
  if min_num > new_min:
    min_num = new_min
  else:
    break
print(min_num)

Submission status: Wrong Answer

Solution code:
import itertools
import math
import fractions

n,k = map(int,input().split())
ans = n
if k == 1 or n == 0 or n==k :
    print("0")
    exit()
elif n < k:
    ans = abs(n-k)
    print(min(ans,n))

elif n > k:
    ans = n%k



while(1):
    
    ans = abs(ans-k)
    if ans == n:
        break
    if ans == 1 or 0:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
n_k = input()
n = int(n_k.split(' ')[0])
k = int(n_k.split(' ')[1])
mini = 10**18
if (n < 10**5):
	while mini > abs(n-k):
		mini = abs(n-k)
		n = abs(n-k)

	print(mini)
else:
	print('0')

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if n < k:
    print(n)
else:
    n = n%k
    while True:
        tmp = n
        a = n-k
        n = abs(a)
        if a < 0:
            print(min(tmp, n))
            break

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
min1=0
min2=0
def amb(n,k):
    min1=abs(n%k)
    min2=abs(min1-k)
    if min1==0 or min2==0:
        print(0)
    elif min1==min2:
        print(min1)
    elif min1>min2:
        print(min2)
    elif min1<min2:
        print(min1)

n,k=list(map(int,input().split()))
if n<k :
    print(n)
else:
    amb(n,k)

Submission status: Wrong Answer

Solution code:
def abstract(a, b):
    return abs(a - b)

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

A = abstract(N, K)
min_temp = A

if N == 1 or K == 1:
    print(0)
elif N == 2 or K == 2:
    print(2)
else:
    while 1:
        A = abstract(A, K)
        if A > min_temp:
            break
        elif A == min_temp:
            break

Submission status: Wrong Answer

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

flag=N

if N/2>=K and N%K==0:
    print(0)
elif N/2>=K and N%K!=0:
    N=N-(int(N/K))*K
    if -(N-K)>=N:
        print(N)
    elif N-K<0 and -(N-K)<N:
        print(-(N-K))
    else:
        print(-(N-K-K))
elif N/2<K:
    if -(N-K)>=N:
        print(N)
    elif N-K<0 and -(N-K)<N:
        print(-(N-K))
    else:
        print(-(N-K-K))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
i = 1
lis = []
if K==1:
    print(0)
while True:
    N = abs(N-K)
    lis.append(N)
    if N < 10:
        break
print(min(lis))

Submission status: Wrong Answer

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

if N % K == 0:
    result = 0
elif N % K != 0 and N // K == 0:
    result = N
else:
    while N > 0:
        N = N - K
    result = abs(N)

print(abs(result))

Submission status: Wrong Answer

Solution code:
# C - Replacing Integer
n, k = map((int),(input().split()))

count = (int)(n / k)
print((int)(min(abs(n - k * count), abs(n - k * (count + 1)))))

Submission status: Wrong Answer

Solution code:
k = input().split()
s = int(k[0])
j = int(k[1])
v = s + 1
if s > 100 * j:
    s = s - 100000 * j
while v > s:
    v = s
    s = abs(s - 10000 * j)
while v > s:
    v = s
    s = abs(s - j)

v = s
s = abs(s-j)
if v > s:
  print(s)
else:
  print(v)

Submission status: Wrong Answer

Solution code:
import math
N,K = list(map(int, input().split()))
 
 
i=int(math.modf(N/K)[1])
#i=int(N/K)
N_i=N-K*i
N_i_p1=N_i-K
 
if abs(N_i_p1)>N_i:
  print(int(N_i))
if abs(N_i_p1)<N_i:
  print(int(abs(N_i_p1)))
if abs(N_i_p1)==N_i:
  print(int(N_i))

Submission status: Wrong Answer

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

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

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())
x = N
min_x = x
i = 0
for i in range(1,N):
    x = abs(x - K)
    i = i + 1
    if x < min_x:
        min_x = x
    if i == N + 1:
        break
print(min_x)

Submission status: Wrong Answer

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

if n % k == 0:
  ret = 0
else:
  if n > k:
    ret = abs((n % k) - k)
  else:
    if k % n == 0:
      ret = n
    else:
      ret = abs((k % n) - n)

print(ret)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if n < k:
    print(min(n, abs(n-k)))
elif k == 1 or n == 0 or (k % 2 == 0 and n % 2 == 0 and n%k == 0):
    print(0)
elif k % 2 == 0 and n % 2 == 0 and n%k != 0:
    print(n%k)
elif n%k == 0:
    print(0)
elif n%k != 0:
    print(min(abs(n%k-k), n%k))

Submission status: Wrong Answer

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

min_list = []

if num_list[0] < num_list[1]:
    print(num_list[0])
elif num_list[0] == num_list[1]:
    print(0)
elif num_list[0] % num_list[1] == 0:
    print(0)
else:
    kake_num = num_list[0] // num_list[1]
    check_num = num_list[0] - (num_list[1]*kake_num)
    min_list.append(check_num)
    min_list.append((num_list[1] - check_num))
    print(min(min_list))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if N < K:
    print(N)
elif N % K == 0:
    print(0)
else:
    a = N // K
    b = N - (a * K)
    c = abs(N - a)
    print(c)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
min1=0
min2=0
def amb(n,k):
    min1=n%k
    min2=abs(min1-k)
    if min1==0 or min2==0:
        print(0)
    elif min1==min2:
        print(min1)
    elif min1>min2:
        print(min2)
    elif min1<min2:
        print(min1)

n,k=list(map(int,input().split()))
if n<k:
    print(n)
else:
    amb(n,k)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
if k==1 or n%k==0:
    ans=0
else:

    if k<n/2:
        if k%2==n%2:
            ans=2
        if k%2!=n%2:
            ans=1
    else:
        x=abs(n-k)
        y=abs(x-k)
        ans=min(n,min(x,y))
print(ans)

Submission status: Wrong Answer

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

flag=N

if N/2>=K and N%K==0:
    print(0)
elif N/2>=K and N%K!=0:
    N=N-(int(N/K))*K
    if -(N-K)>N:
        print(N)
    elif N-K<0 and -(N-K)<N:
        print(-(N-K))
    else:
        print(-(N-K-K))
elif N/2<K:
    if -(N-K)>N:
        print(N)
    elif N-K<0 and -(N-K)<N:
        print(-(N-K))
    else:
        print(-(N-K-K))

Submission status: Wrong Answer

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

now_N = 10 ** 19
while True:
  N = min( abs( N % K ), now_N )
  if now_N == N:
    break
  now_N = N
print( now_N )

Submission status: Wrong Answer

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

#桁が大きい時の処理
if(K == 1):
    while(int(len(str(N))) > 2):
        N = int(N - K * (N / 2));
else:
    while(int(len(str(N))) > 2):
        N = int(N / K);
        
flag = True;
while flag == True:
    N = abs(N - K);
    if(N <= min):
        min = N;
    else:
        flag = False;
    
print(min);

Submission status: Wrong Answer

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

if N % K == 0:
    result = 0
elif N % K != 0 and N // K == 0:
    result = N
else:
    while N < 0:
        N = N - K
    result = abs(N)

print(abs(result))

Submission status: Wrong Answer

Solution code:
list1=[]
a,b=map(int,input().split())
d=a
e=a
if b==1:
    print(0)
else:
    for i in range(d):
        a=a-b
        if a<0:
            if abs(a)<e:
                print(abs(a))
                break
            else:
                print(e)
                break

Submission status: Wrong Answer

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

k = int(N/K)
if (N - K*k) > abs(N - K*(k+1)):
    print(abs(N - K*(k+1)))
else:
    print(N - K*k)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

x = gcd(max(k, n), min(k, n))
if n % k == 0:
    print(0)
elif n < k:
    print(min(n, abs(n - k)))
elif (n + k) % 2 == 0:
    print(2)
else:
    print(1)

Submission status: Wrong Answer

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

if N >= 2*K:
    if (N-2*K) % (2*K) == 0:
        N = abs(N - 2*K*(int((N-2*K)/(2*K)) + 0))
    else:
        N = abs(N - 2*K*(int((N-2*K)/(2*K)) + 1))

if   (N <= K) and (abs(K-N) > N):
    print(N)
elif (N <= K) and (abs(K-N) < N):
    print(abs(K-N))
else:
    if   abs(2*K-N) > N:
        print(N)
    elif abs(2*K-N) <= N:
        print(abs(2*K-N))