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:
line = input().rstrip().split(" ")

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

rec = 0
c = 0
calc1 = N - K
# print(str(calc1))
rec += calc1

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

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

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

print(result)

Submission status: Wrong Answer

Solution code:
def min(a, b):
    if a >= b:
        res = a - b
    elif a < b:
        res = b - a
    return res

n,k = map(int, input().split())
if n%k == 0:
    print(0)
    quit()

min_num = float('inf')


print(min_num)

Submission status: Wrong Answer

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


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

Submission status: Wrong Answer

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

if N == 1 or K == 1:
    print(0)
elif N == 2 or K == 2:
    print(2)
else:
    print(1)

Submission status: Wrong Answer

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

def process(a,b):
	if a%b == 0:
		return 0
	a = a%b

	for i in range(1000):
		a = min(a,abs(a-b))
	return a
if n == 0:
	print(0)
if n==1 and k == 0:
	print(0)
elif k == 1 or n == 1:
	print(0)
else:
	print(process(n,k))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def get_abs(N, K):
  return abs(N-K)

N, K = map(int, input().split())
if K == 1:
  print(0)
  sys.exit()
  
if N/K > 1:
  N = N - (N / K) * K
a = abs(N-K)
while abs(a - K) < a:
  a = abs(a-K)

print(int(a))

Submission status: Wrong Answer

Solution code:
import sys
a,b=map(int,input().split())
i=1
z=[0]*2
saisyo=abs(a-b)
if a<=0:
  print(a)
  sys.exit()
if saisyo>a:
    saisyo=a
if a<b:
  print(saisyo)
  sys.exit()
  
u=a%b
w=(a-u)/b
z[0]=a-(w*b)
z[1]=abs(a-(w+1)*b)
r=int(min(z))


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

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

N, K = map(int, input().split())
diff = abs(K - N)

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

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

Submission status: Wrong Answer

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

n = str(n)

if(len(n) > 1):
    n = int(n[0]+n[1])
else:
    n = int(n)
    
if k == 1:
    print(0)
    exit()

while True:
    N = abs(n - k)
    if N > n:
        break
    else:
        n = abs(n - k)

print(n)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import fractions

a=list(map(int,input().split()))
n=a[0]
k=a[1]
if n==0:
     an=k
if k==1:
     an=0
else:
     an=fractions.gcd(n,k)
print(an)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
#AtCoder
N, K = map(int, input().split())
if K == 1 or (N-K) == 0:
    print(0)
    exit()
elif N < K:
    print(N)
    exit()

q, mod = divmod(N, K)
if mod == N:
    print(N)
elif q < mod:
    print(q)
else:
    print(mod)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
x = n
i = 0
if min(n, k) == 1:
    print("0")
else:
    while i < n:
        x = abs(x - k)
        i += 1
        print(min(n, x))

Submission status: Wrong Answer

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

n = inp[0]
k = inp[1]

if n % k == 0:
    print(0)
else :
    i = int(n / k)
    while(1):
        if n - (i * k) < 0:
            break
        else :
            i += 1

    if (i * k) - n > n - ((i - 1) * k):
        print(n - ((i - 1) * k))
    else :
        print((i * k) - n)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
from fractions import gcd
n,k=map(int,input().split())
m=n%k
if m==0:
    print(0)
else:
    ans=10**10
    for i in range(10):
        ans=min(ans,m)
        m=abs(m-k)
    print(ans)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
a = round(N/K)
result = abs(N - K * a)
print(result)

Submission status: Wrong Answer

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

def get_abs(N, K):
  return abs(N-K)

N, K = map(int, input().split())
if K == 1:
  print(0)
  sys.exit()
  
if N/K > 1:
  N = N - (N / K) * 2
a = abs(N-K)
while abs(a - K) < a:
  a = abs(a-K)

print(int(a))

Submission status: Wrong Answer

Solution code:
import sys

table = list(map(int, input().split()))
min = table[0]

if not table[0] == 0 or table[1] == 0:
    if table[0] > table[1]:
        if table[0] % table[1] == 0:
            print(0)
            sys.exit()

        table[0] = table[0] % table[1]
    else:
        if table[1] % table[0] == 0:
            print(0)
            sys.exit()
        table[0] = table[1] % table[0]

while True:
    a = abs(table[0] - table[1])
    if min <= a:
        break
    min = abs(table[0] - table[1])
    table[0] = min
print(min)

Submission status: Wrong Answer

Solution code:
import fractions

N, K = [int(n) for n in input().split()]
print(0 if K==1 or N==0 or N==K else fractions.gcd(N, K))

Submission status: Wrong Answer

Solution code:
import math

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

x = N

# if x < K:
#   ans1 = abs(x - K)
#   ans2 = abs(ans1 - K)
#   print(min(ans1, ans2))
# else:
# if x % K == 0:
#   print(0)
# else:
x = x - (K * math.floor(x / K))
ans1 = abs(x - K)
ans2 = abs(ans1 - K)
print(min(ans1, ans2))

Submission status: Wrong Answer

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

N = int(N)
K = int(K)

while True:
    n = abs(N-K)
    if n < N:
        break
    else:
        N = n
print(N)

Submission status: Wrong Answer

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

def process(a,b):
	if a%b == 0:
		return 0
	a = a%b

	for i in range(1000):
		a = min(a,abs(a-b))
	return a
if n == 0:
	print(0)
elif k == 1 or n == 1:
	print(0)
else:
	print(process(n,k))

Submission status: Wrong Answer

Solution code:
import sys
N, K = map(int, input().split())
if N == 1 or N == 0 or K == 1 or N == K:
    print(0)
    sys.exit()

if N > K:
    N = N%K

ans = [N]
for _ in range(100):
    ans.append(abs(N-K))
    N = abs(N-K)

print(min(ans))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
if K==0:
  ans=N
elif N%K==0:
  ans=0
else:
  if N%2==0:
    a=(N//K)+1
    ans=N
    for i in range(a):
      ans=min(ans,abs(ans-K))
  else:
    ans=1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

N, K = map(int, input().split())
diff = abs(K - N)

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

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

Submission status: Wrong Answer

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


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


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    solve(N, K)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

print(num)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#振動し始めたら終わり

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

A = []
i=0

if N-K >= 0:
  A.append(N-K)
  N = N-K
    
else:
  A.append(K-N)
  N = K-N
  
if N-K >= 0:
  A.append(N-K)
  N = N-K
    
else:
  A.append(K-N)
  N = K-N
  
if N-K >= 0:
  A.append(N-K)
  N = N-K
    
else:
  A.append(K-N)
  N = K-N
   
    

i = 2
if A[i-2] == A[i]:
  print(min(A))

else:
  while A[i-2] != A[i]:
    if K == 1:
      print('0')
      break
      
    elif N-K >= 0:
      A.append(N-K)
      N = N-K
      i+=1
      if A[i-2] != A[i]:
        print(min(A))
        break
        
    else:
      A.append(K-N)
      N = K-N
      i +=1
      if A[i-2] != A[i]:
        print(min(A))
        break

Submission status: Wrong Answer

Solution code:
import math

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

x = N

# if x < K:
#   ans1 = abs(x - K)
#   ans2 = abs(ans1 - K)
#   print(min(ans1, ans2))
# else:
if x % K == 0:
  print(0)
else:
  x = x - (K * math.floor(x / K))
  ans1 = abs(x - K)
  ans2 = abs(ans1 - K)
  print(min(ans1, ans2))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
if N == K:
  print(N)
elif N < K:
  print(N)
elif N > K:
  tmp = N%K
  if K%2 == 0:
    if tmp <= int(K/2):
      print(tmp)
    else:
      print(K-tmp)
  else:
    a = K-tmp
    b = tmp
    if a < b:
      print(a)
    elif a > b:
      print(b)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
nk = list(map(int, input().split()))
n = nk[0]
if nk[1] == 1:
  print(0)
elif n == 1:
  print(1)
elif nk[1] == 0:
  print(n)
elif n == 0:
  print(0)
else:
  for _ in nk:
    diff = abs(n - nk[1])
    if diff < n:
      n = diff
    else:
      break
  print(n)

Submission status: Wrong Answer

Solution code:
from fractions import gcd 
n,k = map(int,input().split())
if n >= k and n%k == 0:
    print(0)
elif n < k:
    print(min(abs(n-k),n))
else:
    print(gcd(n,k))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
if N == 0:
    print(N)
elif (K / N) > 1:
    print(N)
else:
    amari = N % K
    mainasu_noatai = abs(amari - K)
    if (amari > mainasu_noatai):
    	print(mainasu_noatai);
    else:
        print(amari)

Submission status: Wrong Answer

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

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

x = N % K
x_before = x
sa = x
for i in range(sa):
  x = abs(x-K)
  if x > x_before:
    print(x_before)
    exit()
  x_before = x

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
if k==1:
    ans=0
else:
    x=abs(n-k)
    y=abs(x-k)
    ans=min(n,min(x,y))
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
if N % K == 0:
    print(0)
else:
    while True:
        a = N / K
        r = N % K
        if N < K:
            print(r)
            break
        elif a == 0:
            print(0)
            break
        elif r == 0:
            print(K)
            break
        else:
            N = K
            K = r

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if N < K:
    print(str(N))
elif N%K==0 or N==0:
    print("0")
elif K==2:
    print("1")
else:
    print(str(min(abs(N%K - K), abs(N%K))))

Submission status: Wrong Answer

Solution code:

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

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

first_min_N = N - K*round(N/K)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def process(a,b):
	flag = True
	while flag:
		a,b = max(a,b),min(a,b)
		a = a%b
		if a == 0:
			flag = False

	for i in range(1000):
		a = min(a,abs(a-b))
	return a
if n == 0:
	print(0)
elif k == 1 or n == 1:
	print(0)
else:
	print(process(n,k))

Submission status: Wrong Answer

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

while True:
  if k>n:
    print(min(abs(n-k),abs(k-n)))
    break
  else:
    n=n%k

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

N = int(N)
K = int(K)
prevN = N

while True:
    if abs(N-K) < N:
        break
print(N)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# C - Replacing Integer
N,K=list(map(int, input().split()))

x = N
y = abs(x-K)
while x > y:
    x=y
    y = abs(x-K)
    print(x,y)

print(x)

Submission status: Wrong Answer

Solution code:
import sys
N, K = map(int, input().split())
if N == 1 or N == 0 or K == 1 or N == K:
    print(0)
    sys.exit()

if N > K:
    N = N%K

ans = [N]
for _ in range(10):
    ans.append(abs(N-K))
    N = abs(N-K)

print(min(ans))

Submission status: Wrong Answer

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

if N % K == 0:
    print(0)

else:
    while True:
        if N % K < abs(N - K):
            print(N % K)
            break;

        else:
            N = abs(N - K)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
if N==0 or K==1:
    print(0)
else:
    for i in range(10^18):
        if N>abs(N-K):
            N=abs(N-K)
        else:
            print(N)  
            break

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    value_diff_abs = 0

    if N == 0 or N == K :
        print("0")
    elif N > K :
        ans = N % K
        print(ans)
    else :
        for i in range(0 , N + 1) :
            value_diff_abs = N
            N = abs(N - K)
            if value_diff_abs < N :
                print(str(value_diff_abs))
                break


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

    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())
M=abs(N-K)

if K==0:
    print(abs(N))
elif N==0:
    print(abs(K))
else:
    if K<0:
        K=-K
        N=0-N
    if K<K-N:
        N=K-N
    if K<N:
        N=N%K
    Ans=[N,K-N]
    print(min(Ans))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
a,b=input().split()
a=int(a)
b=int(b)
y=[10**18]
f=0
for i in range (10000):
    x=b-a
    if x==0:
        y.append(0)
        f=0
        break
    elif x<0:
        y.append(x*(-1))
        a=x*(-1)
    else :
        y.append(x)
        a=x
    if int(y[i]) <= int(y[i+1]):
        f=y[i]
        break

print(f)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
l=(n//k)*k
if l==0:
  l=k
while abs(n-l) < n:
  n = abs(n-l)
print(n)

Submission status: Wrong Answer

Solution code:
import math

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

x = N

if x < K:
  ans1 = abs(x - K)
  ans2 = abs(ans1 - K)
  ans3 = abs(ans2 - K)
  ans4 = abs(ans3 - K)
  print(min(ans1, ans2, ans3, ans4))
else:
  if x % K == 0:
    print(0)
  else:
    x = x - (K * math.floor(x / K))
    ans1 = abs(x - K)
    ans2 = abs(ans1 - K)
    ans3 = abs(ans2 - K)
    ans4 = abs(ans3 - K)
    print(min(ans1, ans2, ans3, ans4))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = []
a = N

if N < K:
    print(N)
elif N == K or K == 1:
    print(0)
else:
    ans2 = N % K
    ans1 =  (N % K ) - K
    if abs(ans2) > abs(ans1):
        print(abs(ans1))
    else:
        print(abs(ans2))

Submission status: Wrong Answer

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

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

if n == 0 or n == k:
    print(0)
elif k == 1:
    print(1)
else:
    if n > k:
        r = n % k
        if r == 0:
            print(0)
        else:
            n_1 = abs(r - k)
            n_2 = abs(n_1 - k)
            print(min(n_1, n_2))
    else:
        n_1 = abs(n - k)
        n_2 = abs(n_1 - k)
        print(min(n_1, n_2))

Submission status: Wrong Answer

Solution code:
import sys
import os
import math

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, K = il()
    q, mod = divmod(N, K)
    if q == 0:
        print(mod)
    else:
        print(min(q, mod))



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

val = int(N / K)
val1 = N - K * val
val2 = K - val1

if val1 >= val2:
    print(val2)
else:
    print(val1)

Submission status: Wrong Answer

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


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


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    solve(N, K)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

if K == 0:
    print(N)

elif N < K:
    print(N)
    
else:
    if (N % K) == 0:
        print(abs(N % K))
    else:
        print(abs((N % K) - 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:
    print(min(x, abs(n - k)))

Submission status: Wrong Answer

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

if k == 1:
    ans = 0
else:
    ans = abs(n-k)
    for i in range(100):
        ans = min(ans, abs(ans-k))
print(ans)

Submission status: Wrong Answer

Solution code:

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

if n % k ==  0:
    print(0)
    exit()
if n < k:
    ans = n
else:
    ans = k - (n % k)

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = [int(i) for i in input().strip().split()]
if n % k == 0:
  print(0)
elif n <= n % k:
  print(n)
else:
  print(abs(n % k - k) if n > n % k else n)

Submission status: Wrong Answer

Solution code:
import sys

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

if N < K:
    pass
if N == 1000000000000000000:
    print("0")
    sys.exit()
else:
    while True:
        if N < 0:
            break
        N -= K

print(abs(N))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
N, K = map(int, input().split())
if N == 1 or N == 0 or K == 1 or N == K:
    print(0)
    sys.exit()

if N > K:
    N = N%K
elif K > N:
    K = K%N

ans = [N]
for _ in range(10):
    ans.append(abs(N-K))
    N = abs(N-K)

print(min(ans))

Submission status: Wrong Answer

Solution code:
def main(n: int, k: int):
    print(min(n, n % k))


if __name__ == "__main__":
    n, k = map(int, input().split())
    main(n, k)

Submission status: Wrong Answer

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

while True:
    if ((n-k) > 10000):
        n = n // 10000
    else:
        an = abs(n-k)
        if n <= an:
            break
        else:
            n = min(n, an)
print(n)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys


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

min = sys.maxsize

if n == 0 :print(0);exit()
if n >= 10**18 :print(0);exit()
if k >= 10**18 :print(0);exit()


while True:
    diff = abs(n- k)

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

        if diff < min:
            min = diff



print(min)

Submission status: Wrong Answer

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

if n < k:
    if k - n > n:
        print(n)
    else:
        print(k - n)
else:
    if n % k == 0:
        print(0)
    else:
        print(fractions.gcd(n, k))

Submission status: Wrong Answer

Solution code:
import math
N,K= list(map(int,input().split()))
Num = 10*18
for i in range(Num):
    if N % K ==0:
        Num = 0
        break
    if Num > abs((N%K)-K):
        Num = abs((N%K)-K)
        N  = Num
        if N < abs((N%K)-K):
            break
print(Num)

Submission status: Wrong Answer

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

a1 = abs(N-K*s)
a2 = abs(N-K*(s+1))

if a1 < a2:
  print(a1)
else:
  print(a2)

Submission status: Wrong Answer

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

def absdiff(x, K):
    return abs(x - K)

def culc(N, K):
    if K == 1:
        print(0)
        return

    x = N
    min = x
    x = absdiff(x, K)
    prev = -1
    while min >= x:
        print(min, prev, x)
        min = x
        x = absdiff(x, K)
        if prev == min:
            break
        prev = min

    print(min)
    return


culc(N, K)

Submission status: Wrong Answer

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


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


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    solve(N, K)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

if(N == 0):
  print(N)
elif(K == 1):
  print(K)
else:
  mod = N % K
  if(N<=K):
    if(K-mod < mod):
      print(K-mod)
    else:
      print(mod)
  else:
    print(K-mod)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
max1 = max(N, K)
min1 = min(N, K)

if min1 == 0:
    print(max1)
else:
    a = int(max1/min1)
    max1 -= a*min1
    print(max1)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
ans=(n % m)
ans=min(ans,abs(n-m))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

table = list(map(int, input().split()))


if not table[0] == 0 or table[1] == 0:
    if table[0] > table[1]:
        if table[0] % table[1] == 0:
            print(0)
            sys.exit()

        table[0] = table[0] % table[1]
    else:
        if table[1] % table[0] == 0:
            print(0)
            sys.exit()
        table[0] = table[1] % table[0]

min = abs(table[0] - table[1])
table[0] = min

while True:
    if min <= abs(table[0] - table[1]):
        break
    min = abs(table[0] - table[1])
    table[0] = min
print(min)

Submission status: Wrong Answer

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

print(N % K)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if N>=K:
    amari = N % K
    X = K - amari
    if X > amari:
        x = amari
    else :
        x = X
else:
    x = N

print(x)

Submission status: Wrong Answer

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

if N == K:
    print(0)
elif (N < K) and (abs(K-N) > N):
    print(N)
elif (N < K) and (abs(K-N) > N):
    print(abs(K-N))

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8

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

remainder = N % K
another_N = remainder

while(N > another_N):
    N = another_N
    another_N = abs(N - K)
    
print(N)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
if N==0 or K==1:
    print(0)
else:
    for i in range(10^18):
        if N>abs(N-K):
            N=abs(N-K)
        else:
            break
print(N)