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

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

Submission status: Wrong Answer

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


i=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(N_i)
if abs(N_i_p1)<N_i:
  print(abs(N_i_p1))
if abs(N_i_p1)==N_i:
  print(N_i)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
N = abs(N - K)
if N // 10 == 0:
    while N > 1000:
        N = N / 1000
while N > 0:
    N = (N - K) 
print(abs(N))

Submission status: Wrong Answer

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

while 1:
    if (N%2==0)and(K%2==0)and(N>=K):
        a = 0
        break
    if K==1:
        a = 0
        break
    if N==0:
        a = 0
        break
    if N==(K+1):
        a = 1

    a = N
    N = abs(N-K)

    if c==0:
        c += 1
        continue
    if a<=N:
        break
print(a)

Submission status: Wrong Answer

Solution code:
def main(cell):

    n=cell[0]
    k=cell[1]
    risk=n/k
    n=n-k*int((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 sys

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

if(N > K):
    if(N % K == 0):
        print(0)
        sys.exit()
    else:
        print(1)
        sys.exit()
else:
    tmp = abs(N - K)
    if(tmp <= N):
        print(tmp)
    else:
        print(N)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
cl1 = input().split()
cl1 = [int(s) for s in cl1]
x = cl1[0]
k = cl1[1]
if(x < k):
    print(x)
else:
    a = x % k
    b = -(x % k - k)
    print(min(a,b))

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-(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:
import math
N, K = map(int, input().split())

var = N % K
if var == 0:
        print(0)
elif var == K-1 or var == 1:
        print(1)
else:
        if N > math.ceil(K / 2):
                N = abs(N-K*(N//K))
        print(N)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
m=N
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)
elif N % K ==0:
    print(0)
else:
    a = N // K
    b = N - (a * K)
    c = abs(N - b)
    print(c)

Submission status: Wrong Answer

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

ans = n % k

print(ans)

Submission status: Wrong Answer

Solution code:
min1=0
min2=0
def amb(n,k):
    min1=n%k
    min2=abs((n%k)%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())
N = abs(N - K)
while N > 0:
    N = (N - K)
print(abs(N))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
ans = int(N)
# while (N > 0):
#    N = N - K
#    # print(N)
k_range = N // K
for i in range(k_range+1):
    N -= K
    print(N)
    if N <= 0:
        break
    else:
        pass

if ans > abs(N):
    ans = abs(N)
print(ans)

Submission status: Wrong Answer

Solution code:
#https://atcoder.jp/contests/abc161/tasks/abc161_c
# -*- coding: utf-8 -*-
N,K = map(int,input().split())
syou = N // K
A = N - syou*K
B = N - (syou+1)*K
if abs(A) >= abs(B):
  print(B)
else:
  print(A)

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

Submission status: Wrong Answer

Solution code:
import sys
n,k = map(int,input().split())
a = abs(n-k)
if n < a:
    print(n)
    sys.exit()
while True:
    a = abs(a-k)
    if n > a:
        print(a)
        break
    else:
        a = abs(a-k)

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:
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:
        print(A-K)
    elif A == K:
        print(A)
    else:
        print(K-A)

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

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()
    one = abs(n-k)
    two = abs(one-k)
    print(min(one,two))
    exit()
  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:
#C
import math
def my_round(val, digit=0):
    p = 10 ** digit
    return (val * p * 2 + 1) // 2 / p
n, k = map(int, input().split())
s = int(my_round(n/k))
print(abs(n - (s * k)))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
N = abs(N - K)
if N // 10 == 0:
    while N > 100:
        N = N / 100
while N > 0:
    N = (N - K) 
print(abs(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):
        return abs(N-K*round(N/K))
    else:
        return N

print(replace(N,K))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if N < K:
    print(N)
elif N%K == 0:
    print(0)
elif N >= K:
    for i in range(P):
        N = abs(N-K)
    print(N)

Submission status: Wrong Answer

Solution code:
N, K= (map(int, input().split()))
if N > K:
    N = N%K
    number_list = [N]
else:
    number_list = [N]

print(min(number_list))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

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

print(abs(result))

Submission status: Wrong Answer

Solution code:
import numpy as np
N,K=map(int,input().split())
print(N%K)

Submission status: Wrong Answer

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


count = (int)(n / k)

if count >= 1:
    print((int)(min(abs(n - k * count), abs(n - k * (count + 1)))))
else:
    print(min(n, abs(n - k)))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
s = int(str(N)[-1])
x = abs(s-K)
l = [x]
for i in range(s+K):
    x -= K
    l.append(abs(x))
print(min(l))

Submission status: Wrong Answer

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

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

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

        else:
            print(abs(N % K))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
m=N
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 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 ans in res:
                print(min(res))
                exit()
    print(min(res))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
i = 1
if 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())

if N == K:
    print(0)

elif N > K:
    if N % K == 0:
        print(0)
    else:
        print(min(N % K, N % (N-K)))
else:
    SN = N
    PN = float('inf')
    while PN > N:
        PN = N
        N = abs(N-K)
    print(min(SN, PN))

Submission status: Wrong Answer

Solution code:
min1=0
min2=0
def amb(n,k):
    min1=n%k
    min2=abs((n%k)%k)
    if min1>min2:
        print(min2)
    elif min1<min2:
        print(min1)
    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())
c = 0

while 1:
    if (N%2==0)and(K%2==0)and(N>=K):
        a = 0
        break
    if K==1:
        a = 0
        break
    if N==0:
        a = 0
        break
    a = N
    N = abs(N-K)

    if c==0:
        c += 1
        continue
    if a<=N:
        break
print(a)

Submission status: Wrong Answer

Solution code:
N,K = list(map(int, input().split()))
i=int(N/K)
N_i=N-K*i
N_i_p1=N_i-K

if abs(N_i_p1)>N_i:
  print(N_i)
if abs(N_i_p1)<N_i:
  print(abs(N_i_p1))
if abs(N_i_p1)==N_i:
  print(N_i)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
N = abs(N - K)
if N // 10 == 0:
    while N > 100:
        N = N / 10
while N > 0:
    N = (N - K) 
print(abs(N))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
if K == 0:
    print(N)
elif N > K:
    c = N - (int(N/K)-1)*K
    print(min(c, abs(c-K)))
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(int(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())
if n//k>=10000:
    n=0
dif = abs(n-k)
while dif<n:
  n = dif
  dif = abs(n-k)
print(n)

Submission status: Wrong Answer

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

if k == 1:
    print(0)

q, r = divmod(n, k)

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

Submission status: Wrong Answer

Solution code:
i = list(map(int, input().split()))
N = i[0]
K = i[1]
 
for i in range(1000000):
    if i == 0:
        min = abs(N-K)
    min = abs(min-K)
    if (min-K) < 0:
        break
if min > 5:
    print('0')
else:
    print(min)

Submission status: Wrong Answer

Solution code:
import math

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

ans = 0
if N is K:
    ans = 0
elif N < K:
    ans = min(N, abs(N - K))
else:
    xx = N / K
    c = math.ceil(xx) * K
    f = math.floor(xx) * K
    ans = min(abs(N - c), abs(N - f))

print(ans)

Submission status: Wrong Answer

Solution code:
import fractions
N,K = list(map(int,input().split()))
if N%K == 0 or K%N == 0:
  ans = 0
  print(ans)
else: 
  ans = fractions.gcd(N,K)
  if N == 1 or K == 1:
    ans = 0
  print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
L = []
if n > k:
  n = n - k*(n//k)
 
if n*2 <= k:
  print(n)
else:
  print(k)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
if n==0 or n%k==0:
    print(0)
else:
    a = n%2
    b = k%2
    if a==0 and b==0:
        print(2)
    elif a==1 and b==1:
        print(abs(n-k))
    else:
        while n>=k:
            n = abs(n-k)
        print(abs(n-k))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import fractions
N, K = map(int,input().split())
N = fractions.gcd(N,K)
min = N
for i in range(10**7):
    N = abs(N-K)
    if min > N:
        min = N
    if N == 0:
        min = 0
        break
print(min)

Submission status: Wrong Answer

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


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

Submission status: Wrong Answer

Solution code:
[n, k] = map(int, input().split())
 
n -= n//k
if n > k//2:
  n = k -n
print(n)

Submission status: Wrong Answer

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

if k == 1:
    print(0)
    sys.exit()

while True:
    if ((n-k) > 100000):
        n = n - (k * 100000) 
    else:
        an = abs(n-k)
        if n <= an:
            break
        else:
            n = an
print(n)

Submission status: Wrong Answer

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

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:
        print(n)

Submission status: Wrong Answer

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

if N ==0 or K==1 or N == K:
  print(0)
else:
  while N>abs(N-K):
    N = abs(N%K-K%N)
  print(N)

Submission status: Wrong Answer

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

result = abs(N - K)

if N % K == 0:
    print(0)
elif N < K:
    if N < result:
        print(N)
    else:
        print(result)
elif result % 2 == 1:
    print(1)
else:
    print(2)

Submission status: Wrong Answer

Solution code:
inputs = input().split(' ')
# # inputs2 = input().split(' ')
inputs=[int(a) for a in inputs]
# # inp2=[int(a) for a in inputs2]

def move(a,b):
	c=a-b
	if c < 0:
		c*=-1
	return c


a=inputs[0]
b=inputs[1]
t_f = True
slist=[a]
x=b
# print(a)
while True:
	if x > a or x==a:
		break
	else:
		x=x*10
		# print(x)
	
while t_f==True:
	s = move(a,x)
	a = s
	slist.append(a)	
	
	if slist[-1] > slist[-2] or slist[-1] == slist[-2]:
		t_f = False
	# print(slist)
	# print(s)

print(min(slist))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:

N,K = list(map(int, input().split()))
i=int(N/K)
N_i=N-K*i
N_i_p1=N_i-K

if abs(N_i_p1)>N_i:
  print(N_i)
else:
  print(abs(N_i_p1))

Submission status: Wrong Answer

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

for i in range(10000000):
    if i == 0:
        min = abs(N-K)
    min = abs(min-K)
    if (min-K) < 0:
        break
if min > 5:
    print('0')
else:
    print(min)

Submission status: Wrong Answer

Solution code:
m=10**18
N,K=map(int,input().split())
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)

elif n%k==0:
    print(0)
else:
    t=n-k
    p=k//t
    r=k-p*t
    print(r)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,m = map(int, input().split(' '))
re = 0
if n > m:
    while re >= 0:
        re = n - m
        n = m
        m = re
else: 
    print(n)

Submission status: Wrong Answer

Solution code:
import sys
a,b=map(int,input().split())
i=1
z=[0]*2
saisyo=abs(a-b)
if a==0:
  print(a)
  sys.exit()
if a<b:
  if a<saisyo:
    saisyo=a
  print(saisyo)
  sys.exit()


  
u=a%b
w=(a-u)/b
z[0]=a-(w*b)
z[1]=abs(a-(w+1)*b)
r=int(min(z))


c=[saisyo,r]
print(min(c))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

while True:
    if ((n-k) > 10000):
        n = n - (k * 100000000)
    else:
        an = abs(n-k)
        if n <= an:
            break
        else:
            n = an
print(n)

Submission status: Wrong Answer

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

num = N % K

if num == 0:
  print(0)
else:
  min_num = abs(num - K)
  print(min_num)

Submission status: Wrong Answer

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

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) < k:
        print(abs(rem-k))
    else:
        print(rem)

Submission status: Wrong Answer

Solution code:
import sys
import math

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

if k == 1:
    print(0)
    sys.exit()

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

flag=N

if N/2>=K and N%K==0:
    print(0)
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:
inputs = input().split(' ')
# # inputs2 = input().split(' ')
inputs=[int(a) for a in inputs]
# # inp2=[int(a) for a in inputs2]

def move(a,b):
	c=a-b
	if c < 0:
		c*=-1
	return c


a=inputs[0]
b=inputs[1]
t_f = True
slist=[a]
x=b
print(a)
while True:
	if x > a or x==a:
		break
	else:
		x=x*10
		print(x)
	
while t_f==True:
	s = move(a,x)
	a = s
	slist.append(a)	
	
	if slist[-1] > slist[-2] or slist[-1] == slist[-2]:
		t_f = False
	print(slist)
	print(s)

print(min(slist))

Submission status: Wrong Answer

Solution code:
line = input().rstrip().split(" ")

N = int(line[0])
K = int(line[1])
rec = 0
c = 0


calc1 = N - K
# print(str(calc1))
rec += calc1
if calc1 > N:
    print(str(N))
    c += 1
    # print("終了")


if N % K == 0:
    print("0")
    c += 1
if N > K:
    N = N % K








if calc1 < 0:
    calc1 *= -1
    # print(str(rec))


while c == 0:
    calc = rec - K
    if calc < 0:
        calc *= -1
    # print(str(calc) + "結果")
    if rec < calc:
        print(str(rec))
        # print("終了")
        c += 1
    rec = calc
    # print(str(rec))

Submission status: Wrong Answer

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


def solve(N: int, K: int):
    l=abs(N//K)
    p=K*l
    if N//K==2:
        print(0)
        exit()
    for i in range(1,10000):
        if abs(N-(K*(l+1)))<abs(N-p):
            p=p+1
    print(abs(N-p))
    return


# Generated by 1.1.6 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: Wrong Answer

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

if n < k:
    print(n)
    sys.exit()
elif n == k or k == 1:
    print(0)
    sys.exit()
    
def min_N(n, k):
    a = []
    a.append(n)
    i = 0
    while True:
        a.append(abs(a[i]-k))
        if a[i] < a[i+1]:
            break
        i += 1
    print(a[i])
    sys.exit()

min_N(n, k)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

elif n%k==0:
    print(0)
else:
    t=n-k
    p=k//t
    r=k-p*t
    print(r)

Submission status: Wrong Answer

Solution code:
def str_count(string, letter):
      count = 0
      if len(string) > 0:
            splits = sorted(string)
            for i in splits:
                  if letter in i:
                        count +=1
      return count

Submission status: Wrong Answer

Solution code:
# 初期入力
N,K = (int(x) for x in input().split(" "))

#
zeroika = N//K
N1 =abs(N-K*zeroika) #N1がマイナスになって初めてABS、そこまではKの倍数単調に減る
while N1<N:
    N =N1
    N1 =abs(N-K)
    #print(N1,end=" ")
print(N)

Submission status: Wrong Answer

Solution code:
import numpy as np

N,K = list(map(int, input().split()))
i=int(N/K)
N_i=N-K*i
N_i_p1=N_i-K

if np.abs(N_i_p1)>N_i:
  print(N_i)
else:
  print(np.abs(N_i_p1))

Submission status: Wrong Answer

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

if K > N:
    print(N)
    exit()
if K == N:
    print(0)
    exit()

a = abs(N - K)
ans1 = N % a

b = abs((N % a) - a)
ans2 = N % b

if K == 1:
    print(0)
else:
    print(min(ans1, ans2))

Submission status: Wrong Answer

Solution code:
n,k=map(int, input().split())
for i in range(n//k+2):
    if n < k:
        print(n)
        break
    tmp = n
    n = abs(n-k)
    #print(n)
    if n < 0:
        print(tmp)
        break

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

Submission status: Wrong Answer

Solution code:
k = input().split()
s = int(k[0])
j = int(k[1])
v = s + 1
if s > 10 * j:
    s = s + 10 * 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 fractions
N, K = map(int, input().split())

k = fractions.gcd(N, K)

if K == 1:
    print(0)
else:
    print(k)

Submission status: Wrong Answer

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

k = int(N/K)
print(N - K*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(abs(n-k))
    else:
        print(n)

Submission status: Wrong Answer

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

V = 1
num = 0

while V == 1:
  num = abs(N - M)
  if N == num:
    V = 0
  if N+num == M:
    V = 0
  else:
    N = num
    
print(N)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
c = 0
 
while 1:
    if (N%2==0)and(K%2==0)and(N>=K):
        a = 0
        break
    if K==1:
        a = 0
        break
    if N==0:
        a = K
        break
    a = N
    N = abs(N-K)
 
    if c==0:
        c += 1
        continue
    if a<=N:
        break
print(a)

Submission status: Wrong Answer

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

if N ==0 or K==1:
  print(0)
else:
  while N>abs(N-K):
    N = abs(N%K-K%N)
  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(1)

Submission status: Wrong Answer

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

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

    elif k % n == 0:
        print(n)
        break
    else:
        print((n%k-k) * -1)
        break

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import fractions
N, K = map(int,input().split())
N = fractions.gcd(N,K)
min = N
for i in range(10**6):
    N = abs(N-K)
    if min > N:
        min = N
    if N == 0:
        min = 0
        break
print(min)

Submission status: Wrong Answer

Solution code:
import sys
a,b=map(int,input().split())
i=1
z=[0]*2
saisyo=abs(a-b)
if a==0:
  print(a)
  sys.exit()
if a<b:
  print(saisyo)
  sys.exit()


  
u=a%b
w=(a-u)/b
z[0]=a-(w*b)
z[1]=abs(a-(w+1)*b)
r=int(min(z))


c=[saisyo,r]
print(min(c))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

if N < K:
    print(N)
elif N > K:
    print(N%K)

Submission status: Wrong Answer

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

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

#A = max(N,K)
#B = min(N,K)

#sre = (1/(4*M))*sum(A)

def hoge(k):
    init_i = math.floor(N/k)

    p1 = abs(N - (init_i-1) * k)
    p = abs(N - init_i * k)
    m = abs((init_i+1) * k - N)

    return min(p1,p,m)

ks = []
tmp_K = K

while(1):
    tmp_K = hoge(tmp_K)
    ks.append(tmp_K)
    if tmp_K == 0 or tmp_K in ks:
        break

print(min(ks))

Submission status: Wrong Answer

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


if N == 0:
    print(N)
elif K == 0:
    print(K)
elif N < K:
    k = K % N
    ans = abs(N - k)
    if N * 2 <= K:
        print(N)
    else:
        print(ans)
elif N > K:
    k = N % K
    ans = abs(K - k)
    if k == 0:
        print("0")
    elif k * 2 <= K:
        print(k)
    else:
        print(ans)
else:
    print("0")

Submission status: Wrong Answer

Solution code:
X = list(map(int, input().split()))
N = X[0]
K = X[1]
if K == 1:
  print(0)
if K == 2:
  if N % 2 == 0:
    print(0)
  else:
    print(1)
else:  
  while True:
    A = abs(N - K)
    if N > A:
      N = A
    else:
      break
  print(N)

Submission status: Wrong Answer

Solution code:
min1=0

n,k=list(map(int,input().split()))
min1=n%k
min2=abs((n%k)%k)
if min1>min2:
    print(min2)
elif min1<min2:
    print(min1)
elif min1==min2:
    print(min1)