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 K == 1 or N == 0 or N == K:
    print(0)
    exit()
elif K == 0:
    print(N)
    exit()
elif 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 >= 2:
            if res[i] == res[i-2]:
                print(min(res))
                exit()
    print(min(res))

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

else:
    s = n - q * k
    t = abs(s - k)
    if s < t:
        print(s)
    else:
        print(t)

Submission status: Wrong Answer

Solution code:
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

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

if n == 1 or k == 1:
	print(1)
else:
	print(gcd(n, k))

Submission status: Wrong Answer

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

if n < k:
  last = abs(n - k)
  print(min(n, last))
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())

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

Submission status: Wrong Answer

Solution code:
line = input().split()
N = int(line[0])
K = int(line[1])
A = abs(N-K)
if A % K == 0:
  print(0) 
else:
  print(K % (A % K))

Submission status: Wrong Answer

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

while 1:
    if (N%2==0)and(K%2==0)and(N>=K):
        print(0)
        break
    if K==1:
        print(0)
        break
    if N==0:
        print(0)
        break
    if N==1:
        print(1)
        break
    
    for i in range(2):
        if i==0:
            N = N-((N//K)*K)
        N = abs(N-K)
        a.append(N)
    b = min(a)
    print(b)  
    break

Submission status: Wrong Answer

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

if k == 1 or n == 0 or n == k:
    print('0')
else:
    while n > abs(n - k):
        n = fractions.gcd(n, k)
        # n = abs(n - k)
    print(n)

Submission status: Wrong Answer

Solution code:
import math
N,K= list(map(int,input().split()))
a = int(N/K)
a1 = a*K
a2 = a1+K
if N % K ==0:
    Num = 0
else:
    if abs(N-a1)>=abs(N-a2):
        Num = abs(N-a2)
    else:
        Num = abs(N-a1)
    
print(Num)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
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())
import math
a = n/k
b = math.ceil(a)
if a < 1:
  print(n)
else:  
  x = n-(k*b)
  print(abs(x))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
t = int(n/k)
tt = t + 1
a = n - t*k
b = tt*k - n
if a <= 0:
    a = -a
if b <= 0:
    b = -b
ans=min([a,b])
print(str(ans))

Submission status: Wrong Answer

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

# n, m = map(int, "7 4".split())

# min = -1
# x = n
# 
# output = 0

if n % m  == 0:
    output = 0
elif n >= m:
    if n/2 > m:
        output = n%m   
    else:
        output = n%(n-m)      
else:
    if n < m/2:
        output = n      
    else:
        output = m-n
                          
print(output)

Submission status: Wrong Answer

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

def replace(N,K):
    if(K == 1 or N==K):
        return 0
    if(N<K):
        return N
    else:
        N = N-(K*(N//K))   
        # while(N-K>0):
        if (N-K<=0 and abs(N-K)<K):
            N = abs(N-K)
                       
            
        return N
print(replace(N,K))

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()
    if n % 2 == 0 and k % 2 != 0:
      one = n % k
      two = abs(one - k)
      print(min(one,two))
      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:
N, K = map(int, input().split())

q = N // K
Na = N - q*K
Nb = abs(Na - K)

if Na >= Nb:
  	print(Na)
else:
  	print(Nb)

Submission status: Wrong Answer

Solution code:
import numpy as np
import fractions
N, K = [int(i) for i in input().split(' ')]

if N == 0:
	result = 0
elif N % K == 0:
	result = 0
elif K % N == 0:
	result = K/N if N > K/N else N
elif fractions.gcd(N, K) == 1 and N > K:
	result = 1
elif fractions.gcd(N, K) == 1 and K > N:
	result = K-N if N > K - N else N
elif N > K:
  result = N % K if N % K < K - N % K else K-N%K
elif N < K:
  result = K-N if N > K - N else N
  
print("{}".format(result))

#lis = np.array([int(n) for n in input().split(' ')])
#print("{} {}".format(sum(lis) / 4 / int(M), lis[int(M)-1]))

#count = sum(sum(lis) / 4.0 / int(M) <= lis)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if k !=1 and l == 0:
    print(0)
if l >= abs(l-k):
    print(abs(l-k))
else:
    print(l)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
tmp = n
for i in range(100000):
  tmp = tmp - k
  if (tmp<0):
    tmp = tmp*(-1)
if(k==1):
  print(0)
elif (k==2):
  print(1)
else:
  print(tmp)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if n <= k:
    for i in range(10):
        x = abs(n - k)
        if x <= leastnum:
            leastnum = x
            n = x
        # n = x
        # lst.append(x)
    print(leastnum)

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

Submission status: Wrong Answer

Solution code:
N,K = map(long, raw_input().split())

d = long(N/K)
if d == 0:
    print N
else:
    p = [abs(N - K*(d-1)), abs(N - K*d), abs(N - K*(d+1))]
    print min(p)

Submission status: Wrong Answer

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

min_n = 1000000000000000000000000000
for i in range(1000):
    n %= k
    n = abs(n - k)
    min_n = min(min_n, n)

print(min_n)

Submission status: Wrong Answer

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

def replace(N,K):
    if(K == 1 or N==K):
        return 0
    if(N<K):
        return N
    else:
        while(N-K>0):
            N = N-(K*(N//K))           
            if (N-K<=0 and 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:
    print(0)
elif N < K:
    print(N)
elif N > K:
    if N % K == 0:
        cnt = (N // K)
    else:
        cnt = (N // K) + 1
    print(cnt*K - N)

Submission status: Wrong Answer

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

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

print(abs(result))

Submission status: Wrong Answer

Solution code:
array = list(map(int, input().split()))
tmp = array[0] % array[1]
array[0] = array[0] / array[1]
while (tmp >= array[0]):
  tmp = array[0] % array[1]
  array[0] = array[0] / array[1]
print(tmp)

Submission status: Wrong Answer

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

# A=> N = 2, K =3
if N < K and K < N *2:
    result = abs(N -K)
    print(result)
# B=> N = 2, K =6
elif N < K and N * 2 < K:
    result = N
    print(result)
# C => N =1000000000000000000, K =1
elif N > K and N % K == 0:
    result = 0
    print(result)
elif N > K and N % K != 0:
    n = 0
    while abs(N) // K != 0:
        n +=1
        N = N - (K*n)
    if N < K and K < N *2:
        result = abs(N -K)
        print(result)
    elif N < K and N * 2 < K:
        result = abs(N)
        print(result)

Submission status: Wrong Answer

Solution code:
a,b=input().split()
a=int(a)
b=int(b)
x=b-a
if x==0:
    f=0
    print(0)
elif x<0:
    a=x*(-1)
    y=b-a
    if y==0:
        f=0
    elif y<x:
        f=y
    else:
        f=x
    print(f)
else:
    a=x
    y=b-a
    if y==0:
        f=0
    elif y<x:
        f=y
    else:
        f=x
    print(f)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=[]
for i in range(100):
    N=abs(N-K)
    A.append(N)
print(min(A))

Submission status: Wrong Answer

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

x = n
# 最初の差分だけは繰り返せるので余りを使って短縮
x1 = abs(x-k)
x2 = abs(x1-k)
# print("1:{} 2:{}".format(x1,x2))
if abs(x2-x1) > 0:
  x %= abs(x2-x1)
while True:
  # 今回のループでの結果
  x2 = abs(x-k)
#  print("abs({} - {}) -> {}".format(x,k,x2))
  if x2 > x:
    # 差が大きくなったら終了
    break
  # 計算結果が小さくなっているうちは繰り返す
  x = x2

if n > x:
  print(x)
else:
  print(n)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def replace(N,K):
    if(K == 1 or N==K):
        return 0
    if(N<K):
        return N
    else:
        while(N-K>0):
            N = N-(K*(N//K))           
            if (N-K<=0 and abs(N-K)<K):
                return abs(N-K)

print(replace(N,K))

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:
import math
 
n,k = map(int,input().split())
 
a = math.ceil(n/k)
b = math.floor(n/k)
 
abs_a = abs(n-(a*k))
abs_b = abs(n-(b*k))

print(min(abs_a,abs_b))

Submission status: Wrong Answer

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

def replace(N,K):
    if(K == 1 or N==K):
        return 0
    if(N<K):
        return N
    else:
        while(N-K>0):
            N = N-(K*(N//K))           
            if (N-K<=0 and abs(N-K)>=K):
                return abs(N-K)

print(replace(N,K))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
t = int(n/k)
tt = t + 1
if n-k >=0:
    an = n-k
else:
    an = k-n
ans=min([n - t*k,tt*k - n,an])
print(str(ans))

Submission status: Wrong Answer

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

d = int(N/K)
if d == 0:
    print N
else:
    p = [abs(N - K*(d-1)), abs(N - K*d), abs(N - K*(d+1))]
    print min(p)

Submission status: Wrong Answer

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

print(N%K)

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))
results = set([n])
counter = 0
switch = False

while counter < 50:
    # print(n)

    # test
    if n % k == 0:
        print('0')
        switch = True
        break

    # if n % k < n:
    #     print(n % k)
    #     break

    if n == 0:
        print(n)
        switch = True
        break

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

    # detect loop
    if n in results:
        print(min(results))
        switch = True
        break
    else:
        results.add(n)

    counter += 1


if not switch:
    # print('loop ended n:', n)
    results = list(results)
    print(min(min(results), min(results) % k))

Submission status: Wrong Answer

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

while divflg:
    if n % 10 == 0:
        n = int(n/10)
    else:
        divflg = False

while flg:
    div = n // k
    mod = n % k
    if div == 0:
        flg = False
        diff = abs(mod-k)
    else:
        n = mod

print(min(n,diff))

Submission status: Wrong Answer

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

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

elif N<K:
    if N<=K/2:
        print(N)
    elif N>K/2:
        print(-(N-K))

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
ans=[a]
for i in range(10000):
  if b==1:
    ans.append(abs(ans[i]-b*1000000000))
  else:
    ans.append(abs(ans[i]-b))
print(min(ans))

Submission status: Wrong Answer

Solution code:
import sys
import math

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

if N < K:
    print(N)
elif N == 0:
    print(0)
elif N == K:
    print(0)
else:
    a = math.floor(N / K)

    ans1 = abs(N - (K * a))
    ans2 = abs(N - (K * (a + 1)))
    ans3 = abs(N - K)

    print(min(ans1, ans2, ans3))

Submission status: Wrong Answer

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

if N < K:
    print(N)
elif N%K == 0:
    print(0)
elif N >= K :
    a = abs(N-(K*l))
    b = abs(a-(K*l))
    if a >= b:
        print(b)
    elif a <= b:
        print(a)

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

Submission status: Wrong Answer

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

d = int(N/K)
if d == 0:
    print (N)
else:
    p = [abs(N - K*(d-1)), abs(N - K*d), abs(N - K*(d+1))]
    print (min(p))

Submission status: Wrong Answer

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

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

print(replace(N,K))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
for i in range(10**5):
    if abs(N-K*(i+1)) >= abs(N-K*i) :
        print(abs(N-K*i))
        exit()

Submission status: Wrong Answer

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

else:
    print(0)

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

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

Submission status: Wrong Answer

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

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

print(replace(N,K))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
import math
from decimal import *
getcontext().prec = 70

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

N = 10**18 - 9
K = 10
if N<= K:
    print(min(N,K-N))
else:

    N = N - math.floor(Decimal(N)/Decimal(K))*K 
    print(min(N, K-N))

Submission status: Wrong Answer

Solution code:
n,k = map(float,input().split())
t = k
if n%k==0:
    print(0)
else:
    while(n%10==0 and str(n)[-1] == "0"):
        n=n//10
    while(n%2==0 and k%2==0):
        n=n//2
        k=k//2
    while(n%3==0 and k%3==0):
        n=n//3
        k=k//3
    while(n > abs(n-k)):
        n=abs(n-k)
    print(n)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
"""
青木君は任意の整数 x

に対し、以下の操作を行うことができます。

操作: x
を x と K

の差の絶対値で置き換える。

整数 N
の初期値が与えられます。この整数に上記の操作を 0 回以上好きな回数行った時にとりうる N

の最小値を求めてください。
制約
"""
n,k=map(int,input().split())
x=n
if k==x:
    r=0
elif k>x:
    r=min(x,(k-x))
else:
    if (x-k)>=k:
        r=(x-k)%k
    else:
        r=k%(x-k)
print(r)

Submission status: Wrong Answer

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

if N == K:
    print(0)
elif N < K:
    print(N)
elif N > K:
    cnt = (N // K)
    print(cnt*K - N)

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)
elif tmp < (K - tmp):
    print(tmp)
else:
    print(abs(K - tmp))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
 
if k==1 :
  ln=0
elif k==0 :
  ln=n
elif n>=k:
  a = n%k
  while 1:
    ln = a
    n = abs(n-k)
    if ln<=n:
      break
else:
  while 1:
    ln = n
    n = abs(n-k)
    if ln<=n:
      break
print(ln)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
if K==1 :
    print(0)
    exit()
for i in range(10**5):
    if abs(N-K*(i+1)) >= abs(N-K*i) :
        print(abs(N-K*i))
        exit()

Submission status: Wrong Answer

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

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

elif N<K:
    if N<=K/2:
        print(N)
    elif N>K/2:
        print(-(N-K))

Submission status: Wrong Answer

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

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

A = N - K

while True:
    if (A >= 0):
        A = A - K
        continue
    else:
        print(abs(A))
        exit()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
tmp = n
for i in range(100000):
  tmp = tmp - k
  if (tmp<0):
    tmp = tmp*(-1)
if(k==1):
  print(0)
else:
  print(tmp)

Submission status: Wrong Answer

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

if k == 1 or n == 0 or n == k:
    print('0')
else:
    n = fractions.gcd(n, k)
    while n > abs(n - k):
        n = abs(n - k)
    print(n)

Submission status: Wrong Answer

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

if N < K:
    print(N)
elif N%K == 0:
    print(0)
elif N >= K :
    a = abs(N-(K*l))
    b = abs(a-(K*l))
    if a > b:
        print(b)
    elif a < b:
        print(a)

Submission status: Wrong Answer

Solution code:
import sys

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

tmp = N

if N >= K:
    if N % K == 0:
        print(0)
        sys.exit()
    else:
        K = N / K

while True:
    print(N)
    if abs(N - K) < tmp:
        N = abs(N - K)
    else:
        print(tmp)
        sys.exit()
    tmp = N

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
x = N%K
min = 1001001001
for i in range(10):
  x = abs(x-K)
  if x < min:
    min = x
print(min)

Submission status: Wrong Answer

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

E = N % K

if (E == 0):
  print('0')
else:
  if (E > K):
    print(E)
  else:
    print(abs(E - 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 = N
min_temp = A

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

Submission status: Wrong Answer

Solution code:
n,k=map(int, input().split())
val=n%k
num=abs(n-k)
if num==0:
    print(0)
else:
    val1=min(num%k,k%num)
    print(min(val,val1))

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 
#print(x)
cnt = 0
ans = x
while cnt<10:
  x = abs(x-K)
  ans = min(ans, x)
  cnt += 1
ans = max(ans, 0)
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

else:
    print(0)

Submission status: Wrong Answer

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

if k == 1 or n == 0 or n == k:
    print('0')
else:
    n = fractions.gcd(n, k)
    while n > abs(n - k):
        n = abs(n - k)
    if n == k:
        n = 0
    print(n)

Submission status: Wrong Answer

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

while N >= K:
  	N = N - K

print(N)

Submission status: Wrong Answer

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

def replace(N,K):
    if(K == 1 or N==K):
        return 0
    if(N<K):
        return N
    else:
        while(abs(N-K)>0):
            N = N-(K*(N//K))
            if(N == 0 ):
                return 0                
            elif (N-K<0):
                return abs(N-K)

print(replace(N,K))

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 
#print(x)
cnt = 0
ans = x
while cnt<10:
  x = abs(x-K)
  ans = min(ans, x)
  cnt += 1
ans = min(ans, 0)
print(ans)

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
if b==1:
  a=a-10**str(a).count('0')
ans=[a]
for i in range(10000):
  ans.append(abs(ans[i]-b))
print(min(ans))

Submission status: Wrong Answer

Solution code:
#import sys
#sys.maxint
N, K = map(int, input().split())
A = 0

#if(0 >= N and N >= 1000000000000000000):
    #print(A)
    #exit()

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

A = N - K

while True:
    if (A >= 0):
        A = A - K
        continue
    else:
        print(abs(A))
        exit()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math

S =  list(map(int,input().split()))
N = S[0]
K = S[1]
    
if N < K:
    if N < K-N:
        print(N)
    else:
        print(K-N)
        
if N == K:
    print(0)
if N > K:
    a = math.floor(N/K)
    b = N-K*a
    ba = abs(b-K)

    if b >= ba:
        print(ba)
    else:
        print(b)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
ans = int(N)
k_range = N // K
# print(k_range)

if k_range == 0:
    N = N - K
else:
    N = N - (K * k_range)

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

Submission status: Wrong Answer

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

def replace(N,K):
    if(K == 1 or N==K):
        return 0
    if(N<K):
        return N
    else:
        while(abs(N-K)>=0):
            N = N-(K*(N//K))
            if(N == 0 ):
                return 0                
            elif (N-K<0):
                return abs(N-K)
                
print(replace(N,K))

Submission status: Wrong Answer

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

middle = N % K
answer = 0
if middle < N:
  answer = middle
while True:
  if abs(answer - K) < answer:
    answer = abs(answer-K)
  else:
    break
    
print(str(answer))

Submission status: Wrong Answer

Solution code:
import math
def main(cell):

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

        risk=math.floor(n/k)
        if risk ==0:
            n=n
        else:
            n=n-k*(risk-1)
        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())
#aとnの最大公約数を求める（ユークリッドの互除法）
def gcd(n,a):
	if(n>a): big=n; small=a;
	if(n<a): big=a; small=n;
	
	while(1):
		x=big%small;
		big=small;
		if(x==0): break;
		small=x;
	
	return small;

#aとnが互いに素かどうかの判定
def gcdis1(n,a):
	if(gcd(n,a)==1):
		return 1;  #素だったら1
	else:
		return 0;  #素じゃなかったら0

if(N > K):
    if(N % K == 0):
        print(0)
        sys.exit()
    else:
        if(gcdis1(N,K)==1):
            tmp=N % K
            print(min(tmp,(K - tmp)))
            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:
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()
    if n % 2 == 0 and k % 2 != 0:
      one = 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())
tmpNum = n
subtraction = n
if n % k == 0:
  print(0)
else:
  tmp = int(n/k)
  subtraction = n - (tmp*k)
  while(1):
    subtraction = abs(subtraction-k)
    print(subtraction,tmpNum)
    if subtraction < tmpNum:
      tmpNum = subtraction
    else:
      break
  print(tmpNum)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
lst = []
lst.append(n)
kaisuu = (n // k) + 1
for i in range(kaisuu):
    n = n - k
    lst.append(n)
print(lst)

list2 = []
kazu = len(lst)
for number in lst:
    if number>= 0:
        list2.append(number)
    elif number < 0:
        mainasu = abs(number)
        list2.append(mainasu)
        
print(list2)
print (min(list2))

Submission status: Wrong Answer

Solution code:
from math import ceil
N,K=map(int,input().split())
#print(int(N/K))
kari=N-(int(N/K)*K)
test=abs(N-((int(N/K)+1)*K))
print(min(kari,test))

Submission status: Wrong Answer

Solution code:
def replacing(a, b):
        new = a - b
        new1 = abs(new) - b
        if abs(new1) == new1:
            return 0
        return abs(new1)

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
a=a-10**str(a).count('0')
ans=[a]
for i in range(10000):
  ans.append(abs(ans[i]-b))
print(min(ans))