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 = input().split()
N = int(N)
K = int(K)
if N>K:
    while N>0:
        N=N-K
        if N<=0:
            print(abs(N))
else:
    print(N)

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

if N > 2*K:
    if (N-2*K) % (2*K) == 0:
        N = N - 2*K*(int((N-2*K)/(2*K)) + 0)
    else:
        N = 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)))
print(min(N, abs(N-K), abs(2*K-N)))

Submission status: Wrong Answer

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

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

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

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


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:
N, K = input().split()
N = int(N)
K = int(K)
k = int(N/K)
a = N-(K*k)
if a > abs(a-K):
    a = abs(a-K)
print(a)

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, t = n - q * k, k - r
    if s > t:
        print(t)
    else:
        print(s)

Submission status: Wrong Answer

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

else:
    print(0)

Submission status: Wrong Answer

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

tmp=N//K
if tmp>0:
    print(tmp%K)
else:
    print(N%K)

Submission status: Wrong Answer

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


if N == 0:
    print(N)

initialK = K
while True:
    K = N%K
    if K <= initialK/2:
        print(K)
        break

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:
        flag = 0
        while(abs(N-K)>=0):
            N = abs(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 = [int(i) for i in input().split()]

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

Submission status: Wrong Answer

Solution code:
import fractions
import sys
N,K = map(int,input().split())
L = []
L.append(N)
i = 0
if N == K:
    print(0)
    sys.exit(0)
if N == 0:
    print(0)
    sys.exit(0)
if K == 1:
    print(0)
    sys.exit(0)
Q = 0
while True:
  L.append(abs(N-K))
  N = abs(N-K)
  i += 1
  if i > 10**5:
    Q = min(L)
    break
if N%K == 0 and N>=K:
    print(0)
else:
    if N < K:
        print(min(N,Q))
    else:
        print(min(N%K,Q,fractions.gcd(N,K)))

Submission status: Wrong Answer

Solution code:
N, K = input().split()
N = int(N)
K = int(K)
k = int(N/K)
a = N-(K*k)
if a > abs(a-k):
    a = abs(a-k)
print(a)

Submission status: Wrong Answer

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

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(abs(K - tmp))

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-(ceil(N/K)*K))
print(min(kari,test))

Submission status: Wrong Answer

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

N = N - (K*k_range)

if ans > abs(N):
    ans = abs(N)
print(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 * (a - 1)))

    print(min(ans1, ans2, ans3))

Submission status: Wrong Answer

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

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

tmp = n
for i in range(int(n**0.5)+1, 1, -1):
    if abs(n-i*k) > tmp:
      break
    tmp = min(abs(n-i*k), tmp)
print(tmp)

Submission status: Wrong Answer

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

result = abs(N - K)

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, m = (int(x) for x in input().split())

a = n % m
b = n / m
if b < 1:
    print(n)
elif b == n:
    print(0)
else:
    min = abs(a - m)
    print(min)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

flag=N

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


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

Submission status: Wrong Answer

Solution code:
import math

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

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

Submission status: Wrong Answer

Solution code:
import fractions
import sys
N,K = map(int,input().split())
L = []
L.append(N)
i = 0
if N == K:
    print(0)
    sys.exit(0)
if N == 0:
    print(0)
    sys.exit(0)
if K == 1:
    print(0)
    sys.exit(0)
Q = 0
while True:
  L.append(abs(N-K))
  N = abs(N-K)
  i += 1
  if i > 10**5:
    Q = min(L)
    break
if N%K == 0 and N>=K:
    print(0)
else:
    if N < K:
        print(min(N,Q))
    else:
        print(min(N%K,Q))

Submission status: Wrong Answer

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

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

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:
    bbb = k % n
    if bbb == 0:
      ret = n
    else:
      ret = min(bbb, abs(bbb - n))

print(ret)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().strip().split())
case = 0
while (n-k)>0:
    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:
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(K - b)
    print(c)

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 N % 2 != 0 and K % 2 == 0:
        print('1')
    elif N % 2 == 0 and K % 2 == 0:
        print('2')
    else:
        print(min(abs(N-K), abs(K-(N % K))))

Submission status: Wrong Answer

Solution code:
import fractions
n, k = [int(i) for i in input().split()]
if n <= k:
    print(n)
    exit()

print(fractions.gcd(n, k))

Submission status: Wrong Answer

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

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:
n,k=map(int,input().split())
t = int(n/k)
tt = t + 1
ans=min([n - t*k,tt*k - n])
print(str(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)))

    print(min(ans1, ans2))

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

Submission status: Wrong Answer

Solution code:
n, m = (int(x) for x in input().split())

a = n % m
b = n / m
if b < 1:
    print(n)
elif b == 0:
    print(0)
else:
    min = abs(a - m)
    print(min)

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):
            x=N
            while abs(x - K) < x:
                x=abs(x - K)
            print(x)
            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:
    N = N - K*(N//K)

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

Submission status: Wrong Answer

Solution code:
import math

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

num=math.floor(n/k)

if n<k:
  print(n)
  
elif n==k:
  print(0)
  
else:
  be=abs(n-num*k)
  af=abs(be-k)
  print(be if be<=af else af)

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 = int(N / K)
    b = N - (a * K)
    c = abs(K - b)
    print(c)

Submission status: Wrong Answer

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

if N >= 2*K:
    if (N-2*K) % (2*K) == 0:
        N = N - 2*K*(int((N-2*K)/(2*K)) + 0)
    else:
        N = 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)))
print(min(N, abs(N-K), abs(2*K-N)))

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)))):
    k = abs(N-(K*(k+1)))
else:
    k = abs(N-(K*k))
print(k)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
if 0 <= N <= 10^18 and 1 <= K <= 10^18:
    if N>K:
        N = abs(N-K)
        N = abs(N-K)
        print(N)
    else:
        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
    else:
        flag = 0
        while(abs(N-K)>=0):
            N = abs(N-K)
            if(N == 0 ):
                return 0                
            elif (N-K<0):
                return abs(N-K)
        return N
print(replace(N,K))

Submission status: Wrong Answer

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

else:
    print(0)

Submission status: Wrong Answer

Solution code:
from copy import copy

n, k = map(int, input().split())
a = 0
b = 0
c = 0

if k == 1:
	print(0)
	
elif n < k:
	print(n)

else:
	a = n//k
	b = abs(n-k*(a+1))
	c = abs(n-k*a)
	if b < c:
		print(b)
	elif c < b:
		print(c)

Submission status: Wrong Answer

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


if N>K:
    print(N-(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:
import sys

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

tmp = N

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

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:
# coding: utf-8
 
a=list(map(int,input().split()))
#読み込み
N = a[0]
M = a[1]
b = []

if N % M == 0 or M%N ==0:
    ans = 0
else:
    for i in range(pow(10,5)):
        N = abs(N-M)
        b.append(N)
    list.sort(b)
    print(b[0])

Submission status: Wrong Answer

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

left = 0
right = 10**18 + 1
while right > left + 1:
    n = (left + right) // 2
    if abs(N - K*n) < 1000000 :
        left = n
    else:
        right = n
if left == 0:
    print('0')
else:
    ans = 100000000000
    for i in range(left):
        ans = min(ans,abs(N-K*i))
    print(ans)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int,input().split()))
d = int(N/K)
S0 = N - d * K
S1 = S0 - K
Sa = abs(S1)
if S0 <= Sa:
  print(S0)
else:
  print(Sa)

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((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 = N - 2*K*(int((N-2*K)/(2*K)) + 0)
    else:
        N = 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:
N, K = map(int, input().split())

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

left = 0
right = 10**18 + 1
while right > left + 1:
    n = (left + right) // 2
    if abs(N - K*n) < 1000 :
        left = n
    else:
        right = n
if left == 0:
    print('0')
else:
    ans = 100000000000
    for i in range(left):
        ans = min(ans,abs(N-K*i))
    print(ans)

Submission status: Wrong Answer

Solution code:
import math

S =  list(map(int,input().split()))
N = S[0]
K = S[1]

if N < K:
    print(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:
import math

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

Submission status: Wrong Answer

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

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:
    if abs(n-k) <k:
        print(abs(n-k))
    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:
    e = n%k
    a = abs(k-e)
    if a > e:
        a = e
print(a)

Submission status: Wrong Answer

Solution code:
N,K = list(map(int,input().split()))
result = []
if N == 0 :
    print(K)
    exit()
elif K == 0:
    print(N)
    exit()
if N > K:
    N = N % K
else :
    K = K % N
    if(K == 0):
        K = 1
for i in range(4):
    N = abs(N-K)
    result.append(N)
while (1):
    if(result[-1] == result[-3]):
        if(result[-2] == result[-4]):
            break
    N = abs(N-K)
    result.append(N)
print(min(result))

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
if K == 1 or N == 0 or N == K or N%K == 0:
    print(0)
    exit()
elif K == 0:
    print(N)
    exit()
elif N == 1 or N == K-1 or K == N-1 or N%K == 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 = map(int, input().split())
Count = 0
A = [91239,81238]
i=0
while Count == 0:
    N = abs(N-K)
    A.append(N)
    i += 1
    if A[i] == A[i-2]:
        Count += 1
        N=A[-1]
print(N)

Submission status: Wrong Answer

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

if n>k:
  while 1:
    if n-k<0:
      n=abs(n-k)
      break
    n=abs(n-k)
print(n)

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]

# inputs = [10000000000000,999999]
# inputs = [2,6]

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=int(a/b)

while True:
	# print(x)
	if x < a:
		break
	if x > a:
		x= x*b



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

if N % K == 0:
    result = 0
elif N % K != 0 and N // K == 0:
    result = N
else:
    if (N % K) % 2 == 0:
        result = 2
    else:
        result = 1

print(abs(result))

Submission status: Wrong Answer

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

import math
x = math.floor(n/k)

ans = 10**20
for i in range(-100, 101):
    if ans > abs(n-(x-i)*k):
        ans = abs(n-(x-i)*k)
        print(ans)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
ans=[a]
for i in range(10000):
  if ans[i]>=10**18:
    ans.append(abs(ans[i]-b*10**18))
  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)
else:
    a = math.floor(N / K)

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

    print(min(ans1, ans2))

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

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)
if N<K:
    ans = N
print(ans)

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(n % 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]

# inputs = [10000000000000,999999]
inputs = [2,6]

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=int(a/b)

while True:
	# print(x)
	if x < a:
		break
	if x > a:
		x= x*b



# 	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, m = (int(x) for x in input().split())

a = n%m
if a < 0:
    print(n)
else:
    min = abs(a - m)
    print(min)

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:
        if K==99999999999999999:
          m=1
        else:
          m=min(2,abs(K-2))
      else:
        m=min(1,abs(K-1))
print(m)

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)))):
    k = abs(N-(K*(k+1)))
else:
    k = abs(N-(K*k))
print(k)

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 N // K != 0:
        n +=1
        N = N - (K*n)
    if N < K and K < N *2:
        result = abs(N -K)
        print(result)
    else:
        result = N
        print(result)

Submission status: Wrong Answer

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

import math
x = math.floor(n/k)

ans = 10**20
for i in range(-100, 101, 1):
    if ans > abs(n-(x-i)*k):
        ans = abs(n-(x-i)*k)
        print(ans)

print(ans)

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(abs(n/k))
        n=n-k*(risk)
        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

S =  list(map(int,input().split()))
N = S[0]
K = S[1]

if K == 1:
    print('0')
if N < K:
    print(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:
    c = N - (int(N/K)-1)*K
    print(min(c, abs(c-K)))
else:
    print(min(N,abs(N-K)))

Submission status: Wrong Answer

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

result = abs(N - K)

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

Submission status: Wrong Answer

Solution code:

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import time
N,K=(int(x) for x in input().split())
x=N
if K!=1:
     while True:
        x = abs(x-K)
        y=abs(x-K)
        if y>x or time.time()>1.99999:
            break
     print(x)
else:
    print(0)

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()
s, t = n - q * k, k - r
if q >= 1:
    if s > t:
        print(t)
    else:
        print(s)
else:
    if n < (k - r):
        print(n)
    else:
        print(k - r)

Submission status: Wrong Answer

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


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

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

Submission status: Wrong Answer

Solution code:
import math

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

num=math.floor(n/k)

n=n-num*k
print(abs(n) if abs(n)<=abs(n-k) else abs(n-k))

Submission status: Wrong Answer

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

if N == K:
    print(0)

elif N > K:
    gap = N - K
    if K > N // 2:
        if gap > K:
            print(N % gap)
        else:
            print(N % K)
    else:
        if gap > K:
            print(N % K)
        else:
            print(N % gap)
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:
N,K = map(int,input().split())
n = int((N/K + 0.5)//1)

print(min(abs(n*K-N),abs((n+1)*K-N),abs((n-1)*K-N),abs((n-2)*K-N),abs((n+2)*K-N)))

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

Submission status: Wrong Answer

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

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

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 - abs(k * int(n / k))),abs(abs(n - abs(k * int(n / k)))-k)))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split(' '))
ans_list = [N]
for i in range(N//K+1):
    ans = abs(N-K)
    ans_list.append(ans)
    
print(min(ans_list))

Submission status: Wrong Answer

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

flag=N

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:
        print(N-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().strip().split())

print(n % k)

Submission status: Wrong Answer

Solution code:
import fractions
import sys
N,K = map(int,input().split())
L = []
L.append(N)
i = 0
if N == K:
    print(0)
    sys.exit(0)
if N == 0:
    print(0)
    sys.exit(0)
if K == 1:
    print(0)
    sys.exit(0)
Q = 0
while True:
  L.append(abs(N-K))
  N = abs(N-K)
  i += 1
  if i > 10**5:
    Q = min(L)
    break
if N%K == 0 and N>=K:
    print(0)
else:
    if N < K:
        print(min(N,Q))
    else:
        print(N%K)

Submission status: Wrong Answer

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

import math
x = math.floor(n/k)

ans = 10**9
for i in range(-100, 101):
    if ans > abs(n-(x-i)*k):
        ans = abs(n-(x-i)*k)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
n = int((N/K + 0.5)//1)

print(min(abs(n*K-N),abs((n+1)*K-N),abs((n-1)*K-N)))

Submission status: Wrong Answer

Solution code:
array = list(map(int, input().split()))
tmp = array[0]
array[0] = abs(array[0] - array[1])
while (tmp == array[0]):
  tmp = array[0]
  array[0] = abs(array[0] - array[1])

print(array[0])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
Count = 0
A = [91239,81238]
i=0
while Count == 0:
    N = abs(N-K)
    A.append(N)
    i += 1
    if A[i] == A[i-2]:
        Count += 1
print(N)