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:
str = input().split()
a1 = int(str[0])
a2 = int(str[1])
if a1 >= a2:
    a1 = a2%a1
if a1 < a2-a1:
    print(a1)
else:
    print(a2-a1)

Submission status: Wrong Answer

Solution code:
x, y = map(int, input().split())
tmp = 10**20
k = x // y
while tmp >= x and tmp != x:
    tmp = x
    x = abs(x-y*k)
print(tmp)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
a = N
#操作をしなくてもよい場合
if a < abs(K - N):
    print(a)
    exit()
while True:
    if K >= a:
        print(abs(K - N))
        break
    else:
        a = abs(K - a)

Submission status: Wrong Answer

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

if abs(N-K) > N:
  print(N)
elif N >= 10**18 or K >= 10**18:
  print(0)
else:
  while abs(N-K) < N:
    N = N-K
    if abs(N-K) > N:
      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
    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:
N,K=map(int,input().split())
N = N%K
print(N)

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 = n / k
print(min(n%k,k-s))

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:
def MI(): return map(int, input().split())

if __name__ == "__main__":
    N, K = MI()
    if N < K:
        print(N)
    elif N%K > abs(K - N%K):
        print(abs(K - N%K))
    else:
        print(N%K)

Submission status: Wrong Answer

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

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 K == 1 or N == 0 or N == K or N%K==0:
    print(0)
    exit()
elif K == 0 or K > N:
    print(N)
    exit()
elif N == 1 or N == K-1 or K == N-1:
    print(1)
    exit()
elif N > K:
    ans = (N//K) + 1
    res = abs(N - (K*ans))
    print(res)
    exit()

Submission status: Wrong Answer

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


def x(N, K):
    y = N
    f = 0
    while True:
        N = abs(N - K)

        if y > N:
            y = N

        if y == N:
            f += 1

        if f > 5:
            return y


# if K == 0:
#     print(N)
# elif K == 1:
#     print(0)
# else:
#     print(x(N, K))
print(x(N, K))

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


def x(N, K):
    y = (N)
    f = 0
    while True:
        N = abs(N - K)

        if y > N:
            y = N

        if y == N:
            f += 1

        if f > 2:
            return y


# if K == 0:
#     print(N)
# elif K == 1:
#     print(0)
# else:
#     print(x(N, K))
print(x(N, K))

Submission status: Wrong Answer

Solution code:
import sys
import math

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

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

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

Submission status: Wrong Answer

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


def x(N, K):
    y = (N)
    f = 0
    while True:
        N = abs(N - K)

        if y > N:
            y = N

        if y == N:
            f += 1

        if f > 1:
            return y


# if K == 0:
#     print(N)
# elif K == 1:
#     print(0)
# else:
#     print(x(N, K))
print(x(N, K))

Submission status: Wrong Answer

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

N=N%K

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

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:
# 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 = my_round(n / k)
print(min(n%k,k-s))

Submission status: Wrong Answer

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


def x(N, K):
    y = (N)
    f = 0
    while True:
        N = abs(N - K)

        if y > N:
            y = N

        if y == N:
            f += 1

        if f > 4:
            return y


if K == 0:
    print(N)
elif K == 1:
    print(0)
else:
    print(x(N, K))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding : utf-8

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

flag = 0

alpha = N / K

cad = [N - (alpha-1)*K, N - alpha*K, N - (alpha+1)*K]
cad = list(map(abs, cad))

print(int(min(cad)))

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split(' '))
ans =[]
x = N - int(N/K) * K
x1 = K - x
ans.append(x)
ans.append(x1)
print(min(ans))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
while True:
    if n == 0 or n == k or k == 1 or n == 1:
        print(0)
        exit()
    elif n < k:
        m = k - n
        if n > m:
            n = m
        else:
            print(n)
            exit()
    elif n > k:
        m = n - k
        if n > m:
            n = m
        else:
            print(n)
            exit()
    else:
        print(n)
        exit()

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 or K > N:
    print(N)
    exit()
elif N == 1 or N == K-1 or K == N-1:
    print(1)
    exit()
elif N > K:
    ans = (N//K) + 1
    res = abs(N - (K*ans))
    print(res)
    exit()

Submission status: Wrong Answer

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


def x(N, K):
    y = (N)
    f = 0
    while True:
        N = abs(N - K)
        print(y)

        if y > N:
            y = N

        if y == N:
            f += 1

        if f > 4:
            return y


if K == 0:
    print(N)
elif K == 1:
    print(0)
else:
    print(x(N, K))

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

a = N % K 

if N < K:
        print(N)
else:
        m = 1000000000000000000
        for i in range(100):
                a = abs(a - K)
                if a < m:
                        m = a 
        print(m)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


def x(N, K):
    y = (N)
    f = 0
    while True:
        N = abs(N - K)
        print(y)

        if y > N:
            y = N

        if y == N:
            f += 1

        if f > 1:
            return y


if K == 0:
    print(N)
elif K == 1:
    print(0)
else:
    print(x(N, K))

Submission status: Wrong Answer

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

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

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:
    ans = (N//K) + 1
    res = abs(N - (K*ans))
    print(res)

Submission status: Wrong Answer

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


count = (int)(n / k)

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

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
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%2!=0:
          m=1
        else:
          m=min(2,abs(K-2))
      else:
        if K%2==0:
          m=min(1,abs(K-1))
        else:
          m=min(2,abs(K-1))
print(m)

Submission status: Wrong Answer

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

a = a%10
b = b%10

if a > b:
    min = b
else:
    min = a

while True:
    if min > abs(a-b):
        min = abs(a-b)
    else:
        break

    if a > b:
        a = min
    else:
        b = min

    

print(min)

Submission status: Wrong Answer

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

if(N == K):
    print(0)
elif(K>N):
    if(n<N):
        print(n)
    else:
        print(N)
elif(K<N):
    mod = N % K
    print(mod)

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())
ans = n - k
if k == 1 or n == 1:
    print(1)
    quit()
if n % k == 0:
    print(0)
    quit()
if n < k:
    print(n)
    quit()

print(min(ans, k - n%k))

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

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

Submission status: Wrong Answer

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

if N > 1000000000000000*K:
    while x > K:
        x = abs(x - 1000000000000000*K)
        if x < K:
            break

while x > K:
    x = abs(x - K)
    if x < K:
        break

A = []
for i in range(5):
    A.append(abs(x - K))     
print(min(A))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
Boundary = N // K
if Boundary == 0:
  print(N)
elif abs(N-K) >= N:
  print(0)
elif abs(N-Boundary*K) >= abs(N-(Boundary+1)*K):
  print(abs(N-Boundary*K))
else:
  print(abs(N-(Boundary+1)*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 == 0:
    print(0)
elif N == 1 or K == 1:
    print(K)
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())

if K == 1:
    print(0)

elif N == K:
    print(0)

elif N == 0:
    print(0)

elif N % 2 == 0 and K % 2 == 0 and N > K and K % N == 0:
    print(0)
elif N % 2 == 0 and K % 2 == 0 and N > K and K % N != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10 ** 8):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break


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


elif N % 2 == 1 and K % 2 == 1 and N > K and K % N == 0:
    print(0)
elif N % 2 == 1 and K % 2 == 1 and N > K and K % N != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10**8):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break


elif N % 2 == 0 and K % 2 == 0 and N < K and K % N == 0:
    print(N)
elif N % 2 == 0 and K % 2 == 0 and N < K and K % N != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10 ** 8):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break


elif N % 2 == 0 and K % 2 == 1 and N > K and N % K != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10 ** 8):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break
elif N % 2 == 0 and K % 2 == 1 and N > K and N % K == 0:
    print(0)


elif N % 2 == 0 and K % 2 == 1 and N < K and N % K != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10 ** 8):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break
elif N % 2 == 0 and K % 2 == 1 and N < K and N % K == 0:
    print(0)


elif N % 2 == 1 and K % 2 == 0 and N > K and N % K != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10 ** 8):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break
elif N % 2 == 1 and K % 2 == 0 and N > K and N % K == 0:
    print(0)


elif N % 2 == 1 and K % 2 == 0 and N < K and N % K != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10 ** 8):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break
elif N % 2 == 1 and K % 2 == 0 and N < K and N % K == 0:
    print(N)

else:
    print("NG")

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
A = []
A.append(N)
sa = N - K
N = abs(sa)
A.append(N)
if N in A:
    print(min(A))
else:
    sa = N - K
    N = abs(sa)
    A.append(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:
    if abs(N % K) < abs(K - (N % K)):
        result = N % K
    else:
        result = K - (N % K)

print(abs(result))

Submission status: Wrong Answer

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

ans = n
if n == 0 or n == 1 or k == 1 or n == k:
  ans = 0
elif n >= 2*k or k >= 2*n:
  ans = n
else:
  ans = abs(n-k)
  
print(ans)

Submission status: Wrong Answer

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

result = abs(N - K)

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

if N == K:
  print(0)
else:
  a = int(N / K) * K
  a = N - a
  if int(N / K)*K == 0:
    print(min(N, abs(N-K)))
  else:
    print(min(a, abs(a-K)))

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)+2, 1, -1):
    tmp = min(abs(n-i*k), tmp)
    if abs(n-(i-1)*k) > tmp:
      break
print(tmp)

Submission status: Wrong Answer

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

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:
    ans = (N//K) + 1
    res = abs(N - (2**ans))
    print(res)

Submission status: Wrong Answer

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

if(x>=n):
    print(max(n,x-n))
else:
    dict1={}

    flag=0
    val=n//x

    n-=val*x
    ans=0
    while(flag==0):
        n=abs(n-x)
        try:
            dict1[n]+=1
            print(ans)
            exit(0)
        except:
            KeyError
            dict1[n]=1

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
if n==k:
    print(0)
elif k == 1:
    print(0)
elif n < k :
    print(min(n,k-n))
elif n%2!=0 or k%2!=0:
    print(1)
elif n%2==0 and k%2==0:
    print(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if n%k < n and abs(n%k-k) < n%k:
    print(int(abs(n%k-k)))
elif n%k < n and abs(n%k-k) >= n%k:
    print(int(n%k))
else:
    print(int(n))

Submission status: Wrong Answer

Solution code:
i1 = input()

list1 = [int(s) for s in i1.split()]

n = list1[0]
k = list1[1]


def do(x):
    return abs(x - k)

def next_search(last_index):
    if (last_index%2) == 1:
        # 奇数
        return (last_index-1) / 2
    else:
        return last_index /2

def truemin(value):
    list1 = []
    list1.append(value)
    list1.append(do(value))
    list1.append(value)
    list1.append(do(value))
    return int(min(list1))

past = do(n)

index = 1000000000000000000

for a in range(100):
    # print("index" + index.__str__())

    if index == 0.0:
        # print("indexがゼロ")
        print(truemin(n))
        break

    future = n - index*k
    if future < 0:
        index = next_search(index)
        # print("future:" + future.__str__())
    elif future < k:
        # print("ここで収束!")
        # print("future:" + future.__str__())
        print(truemin(future))
        break
else:
    print(n)

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

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 or K == 0 or N % K == 0:
    print("0")
    c += 1

if N > K:
    M = N % K
    N = M

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

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

Submission status: Wrong Answer

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

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

old = max(NK)
new = 0

while old > new:
    old = new
    for i in range(10):
        if i == 0:
            temp = []
            temp.append(abs(N - K))
            temp.append(abs(K - N))
        else:
            temp1 = []
            temp1.append(abs(temp - K))
            temp1.append(abs(temp - N))
            temp = temp1
        temp = min(temp)
    new = temp


print(temp)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
def abs_result(x,y):
    tmp = abs(x-y)
    return tmp

def main():
    line = list(map(int,input().split()))
    n,k = line[0],line[1]
    
    # nを、nとkの差の絶対値で置き換える
    if n == 0 or k == 1:
        print(0)
        return
    
    if n % 2 == 0 and k % 2 == 0 and n < k:
        print(2)
        return
    
    if n % 2 == 0 and k % 2 == 0 and n > k:
        print(0)
        return
        
    if n % 2 == 1 and k % 2 == 0 and n > k:
        print(1)
        return
        
    
    com = 2
    for i in range(0,10**18):
        ans = abs_result(n,k)
        if ans == 0:
            print(0)
            return
        elif ans == 1:
            print(1)
            return
        if ans <= com:
            print(ans)
            return
        n = ans

main()

Submission status: Wrong Answer

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

if K == 1:
    print(0)

elif N == K:
    print(0)

elif N == 0:
    print(0)

elif N % 2 == 0 and K % 2 == 0 and N > K and K % N == 0:
    print(0)
elif N % 2 == 0 and K % 2 == 0 and N > K and K % N != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10 ** 18):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break


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


elif N % 2 == 1 and K % 2 == 1 and N > K and K % N == 0:
    print(0)
elif N % 2 == 1 and K % 2 == 1 and N > K and K % N != 0:
    if abs(N - K) > N:
        print(N)
    else:
        a = N % K
        print(a)


elif N % 2 == 0 and K % 2 == 0 and N < K and K % N == 0:
    print(N)
elif N % 2 == 0 and K % 2 == 0 and N < K and K % N != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10 ** 18):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break


elif N % 2 == 0 and K % 2 == 1 and N > K and N % K != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10 ** 18):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break
elif N % 2 == 0 and K % 2 == 1 and N > K and N % K == 0:
    print(0)


elif N % 2 == 0 and K % 2 == 1 and N < K and N % K != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10 ** 18):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break
elif N % 2 == 0 and K % 2 == 1 and N < K and N % K == 0:
    print(0)


elif N % 2 == 1 and K % 2 == 0 and N > K and N % K != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10 ** 18):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break
elif N % 2 == 1 and K % 2 == 0 and N > K and N % K == 0:
    print(0)


elif N % 2 == 1 and K % 2 == 0 and N < K and N % K != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10 ** 18):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break
elif N % 2 == 1 and K % 2 == 0 and N < K and N % K == 0:
    print(N)

else:
    print("NG")

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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



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

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

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

Submission status: Wrong Answer

Solution code:
a,b=input().split()
a=int(a)
b=int(b)  
if a==0:
    f=b
    print(f)
elif b==0:
    f=a
    print(f)
elif b==1 and a==1:
    f=0
    print(f)
elif b==1 :
    f=1
    print(f)
elif a==1 :
    f=1
    print(f)

else:
    if b>a:
        x=b-a
    else:
        x=a-b
    if x==0:
        f=0
        print(0)
    else:
        a=x
        if b>a:
            y=b-a
        else:
            y=a-b
        if y==0:
            f=0
        elif y<x:
            f=y
        else:
            f=x
        print(f)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import fractions
n, k = map(int, input().split())
if (n > abs(n - k)):
    n = fractions.gcd(n, k)
while n > abs(n - k):
    n = abs(n - k)
if n == k:
    print('0')
else:
    print(n)

Submission status: Wrong Answer

Solution code:
import math

if __name__ == '__main__':
    s = input().rstrip().split(' ')
    N,K = int(s[0]), int(s[1])
    if(N>=K):
        cand1 = int(abs(N - int(K*math.ceil(N/float(K)))))
        cand2 = int(abs(N - int(K*math.floor(N/float(K)))))
        print(min(cand1, cand2))
    else:
        print(min(int(abs(N-K)),N))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math

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

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)
        print(x)
        if x <= leastnum:
            leastnum = x
            n = x
        # n = x
        # lst.append(x)
    print(leastnum)

elif k == 1:
    print(0)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def abs_result(x,y):
    tmp = abs(x-y)
    return tmp

def main():
    line = list(map(int,input().split()))
    n,k = line[0],line[1]
    
    # nを、nとkの差の絶対値で置き換える
    if n == 0 or k == 1:
        print(0)
        return
    
    if n % 2 == 0 and k % 2 == 0 and n < k:
        print(2)
        return
    
    if n % 2 == 0 and k % 2 == 0 and n > k:
        print(0)
        
    if n % 2 == 1 and k % 2 == 0 and n > k:
        print(1)
        return
        
    
    com = 2
    for i in range(0,10**18):
        ans = abs_result(n,k)
        if ans == 0:
            print(0)
            return
        elif ans == 1:
            print(1)
            return
        if ans <= com:
            print(ans)
            return
        n = ans

main()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=[]
for i in range(1000):
    N=abs(N-K)
    A.append(N)
    if N==0 or N==1 or N==2:
        break
print(min(A))

Submission status: Wrong Answer

Solution code:
n, k = list(map(int,input().split()))
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

if n > k:
    N = n
    K = k
    A = []
    a = 100
    while a != 0:    
        a = N - K*(N//K)
        N = K
        K = a
        A.append(a)
    if len(A) ==  1:
        print("0")
    elif A[-2] == 1:
        print("1")
    else:
        print(A[-2])
else:
    print(gcd(n,k))

Submission status: Wrong Answer

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

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