Problem:


Score : 300 points

Problem Statement

Given any integer x, Aoki can do the operation below.

Operation: Replace x with the absolute difference of x and K.

You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more times.

Constraints

0 ≤ N ≤ 10^{18}

1 ≤ K ≤ 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the minimum possible value taken by N after Aoki does the operation zero or more times.

Sample Input 1

7 4

Sample Output 1

1

Initially, N=7.

After one operation, N becomes |7-4| = 3.

After two operations, N becomes |3-4| = 1, which is the minimum value taken by N.

Sample Input 2

2 6

Sample Output 2

2

N=2 after zero operations is the minimum.

Sample Input 3

1000000000000000000 1

Sample Output 3

0



Sample input:
7 4


Sample output:
1

Submission status: Wrong Answer

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

if n < k:
    ans = n
    print(ans)
    exit()

elif n % k == 0:
    ans = 0
    print(ans)
    exit()
else:
    n = n % k
    for i in range(10000000000000000000):
        res = abs(n - k)
        if n <= res:
            ans = n
            print(ans)
            exit()
        else:
            n = res

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

Submission status: Wrong Answer

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


if(x > 10*10*10*10*10*10*10*10*10*10*K):
    x=x-abs(x-K)

while(x > min(x, abs(x-K))):
    x=min(x, abs(x-K))

print(x)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if n // k == 0:
    if n < k:
        print(n)
    else:
        print(0)
else:
    print(n%k)

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

Submission status: Wrong Answer

Solution code:
I=lambda:list(map(int,input().split()))
n,k=I()
if n%k==0:
    print(0)
elif n<k:
    print(min(n,abs(n-k)))
else:
    print(min(n,n%k,abs(n-k)))

Submission status: Wrong Answer

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

k = int(N/K)
print(N - N*k)

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) and N / K <= 1:
            print('N % K = ' + str(N % K) + ', abs(N - K) = ' + str(abs(N - K)))
            print(N % K)
            break;

        else:
            print('N % K = ' + str(N % K) + ', abs(N - K) = ' + str(abs(N - K)))
            N = abs(N - K)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


count = n / k

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

Submission status: Wrong Answer

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

a = int(N / K)

b = abs(N - K * a)
c = abs(b - K)
d = abs(c - K)
e = abs(b + K)

print(min(b, c, N, d, e))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

    if N*2 == K:
        print(N)
        return

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

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

    print(min)
    return


culc(N, K)

Submission status: Wrong Answer

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

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

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

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

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

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

    return min(p1,p,m)

ks = []
tmp_K = K

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

print(min(ks))

Submission status: Wrong Answer

Solution code:
def count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count

def input_list():
  return list(map(int, input().split()))

def input_list_str():
  return list(map(str, input().split()))

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

# 2で割り切れる回数
def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c

# 素因数分解
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

def main():
  n,k = input_list()
  if n == k:
    print(0)
    exit()
  if k == 0:
    print(n)
    exit()
  if n > k:
    if n % k == 0:
      print(0)
      exit()
    if n % 2 == 0 and k % 2 == 0:
      if k > 2:
        print(2)
        exit()
      print(0)
      exit()
    if n % 2 != 0 and k % 2 == 0:
      one = abs(n-k)
      if k > one:
        print(one)
        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())

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

Submission status: Wrong Answer

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

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 N == 0 or K == 1:
        print(0)
    elif q == 0:
        print(min(mod, abs(N-K)))
    else:
        print(min(q, mod, abs(N-K)))



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
nums = [int(e) for e in input().split()]
Min=nums[0]
y=nums[1]
t=True
while t:
  if(abs(Min -y)<Min):
    Min = Min-y
  elif(abs(Min - y) > Min):
    break
print(Min)

Submission status: Wrong Answer

Solution code:
import sys
a,b=map(int,input().split())
i=1
z=[0]*2
saisyo=abs(a-b)
if b==0:
  print(a)
  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
# import math #sqrt
# import decimal
# import queue # queue
# import bisect
# import heapq # priolity-queue
# import time
# import itertools
# import collections # deque
# from operator import itemgetter
# from fractions import Fraction

mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

def main():
    n,k = map(int,input().split())
    f = abs(n-k)
    if f==0:
        print(0)
        return
    print(min(n%k,k%f))
    return

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
num_list = [int(i) for i in input().split()]
min_list = []

if num_list[0] < num_list[1]:
    print(num_list[0])
elif num_list[0] == num_list[1]:
    print(0)
elif num_list[1] % num_list[0] == 0:
    print(0)
else:
    kake_num = num_list[0] // num_list[1]
    check_num = num_list[0] - (num_list[1]*kake_num)
    min_list.append(check_num)
    min_list.append((num_list[1] - check_num))
    print(min(min_list))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
print(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
if(N<K):
    print(N)
elif(K==1):
    print(0)
elif(N>=K):
    d = int(N/K)
    a = N%K
    N = N - (d*K)
    print(abs(N-K))

Submission status: Wrong Answer

Solution code:

I=lambda:list(map(int,input().split()))
n,k=I()
if n%k==0:
    print(0)
elif n<k:
    print(min(n,abs(n-k)))

else:
    print(min(n%k,abs(n-k),n))

Submission status: Wrong Answer

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


if a== 0:
  print(a)
elif a <= b:
  while min > abs(a-b):
    min = abs(a-b)
    a   =abs(a-b)
elif a > b:
  a = a%b
  while min > abs(a-b):
    min = abs(a-b)
    a   =abs(a-b)
  
print(min)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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
    b=[]
    b.append(N)
    for i in range(a):
      b.append(abs(b[i]-K))
    ans=min(b)
  else:
    ans=1
print(ans)

Submission status: Wrong Answer

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

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 N < K:
    print(N)
    sys.exit()

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

k = N/K
print(K - N*k)

Submission status: Wrong Answer

Solution code:
k = input().split()
s = int(k[0])
j = int(k[1])
v = s + 1
if s > 10 * j:
    s = j + 100
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:
def replacing(a, b):
    new = a - b
    new1 = abs(new) - b
    return abs(new1)

Submission status: Wrong Answer

Solution code:
n, k = [int(i) for i in input().split()]
l = []
ans = n
while True:
  if n <= k:
    l.append(ans)
    break
  elif k == 1:
    l.append(0)
    break
  elif ans > 0:
    ans = ans - k
    l.append(ans)
  else:
    break

print(abs(min(l)))

Submission status: Wrong Answer

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

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:
a,b=input().split()
a=int(a)
b=int(b)
y=[10**20]
f=0
for i in range (10**18):
    print(i)
    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())
if N == K:
  print(N)
elif N < K:
  print(N)
elif N > K:
  tmp = N%K
  if K%2 == 0:
    a = K-tmp
    b = tmp
    if a < b:
      print(a)
    elif a > b:
      print(b)   
  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())
kari=int(N/K)
M=N-K*(kari)
L=abs(M-K)
B=abs(L-K)
print(min(N,abs(M-K),L,B,abs(M)))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:

I=lambda:list(map(int,input().split())) 
n,k=I()
print(min(n,n%k,abs(n-k)))

Submission status: Wrong Answer

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

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

first_min_N = N - K*math.floor(N/K)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
k = input().split()
s = int(k[0])
j = int(k[1])
v = s + 1
if s > 100 * j:
    s = j + 100
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:
def solve():
    N, M = map(int, input().split())
    a = abs(N-M)
    
    if N == 0:
        return 0
    
    if a == 0:
        return 0
    
    if M == 1:
        return 0
        
    n = N
    while n > abs(n-M) and n != 0:
        n = min(n, abs(n-M))
    
    return min(n, abs(n-a))

print(solve())

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)
    elif K == 1 and N != 0 :
        print("0")
    elif K == 2 and N % 2 == 0 and N != 0 :
        print("0")
    elif K == 2 and N % 2 != 0 and N != 0 :
        print ("1")
    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
            elif 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())
divflg = True
flg = True

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

while flg:
    tmp = abs(n-k)
    n = tmp
    tmp2 = abs(n-k)
    if tmp2 > tmp:
        flg = False
    
print(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(N)
    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%2 == 0 and K%2 == 0):
  if N > K:
    print(0)
    sys.exit(0)
  else:
    print(N)
    sys.exit(0)
if (N%2 != 0 and K%2 != 0):
  if N<K:
    print(N)
    sys.exit(0)
  else:
    print(min(N,abs(N-K),Q))
    sys.exit(0)
else:
    if N > K:
        if N%K == 0:
            print(0)
            sys.exit(0)
        else:
            print(min(N,Q,fractions.gcd(N,K)))
            sys.exit(0)
    else:
        print(min(N,Q))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
if __name__ == "__main__":
    a, b = map(int, input().split())
    tmp = 0
    if b == 1:
        print(0)
    elif a < b:
        print(a)
    else:
        while a >= 0:
            tmp = abs(a-b)
            a = a - b
        print(tmp)

Submission status: Wrong Answer

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

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

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

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(100000):
		a = min(a,abs(a-b))
	return a
if n == 0:
	print(0)
elif n==1 and k == 0:
	print(1)
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()))

ans = []
a = N

if N < K:
    print(N)
elif N == K or K == 1:
    print(0)
elif K == 0:
    print(N)
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:
n,k  = map(int,input().split())

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

Submission status: Wrong Answer

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

if N < K:
    print(N)

else:
    N = N % K

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

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



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

import math
x = math.floor(n/k)
ans = min( abs(n-(x-1)*k), abs(n-x*k), abs(n-(x+1)*k) )
print(ans)

Submission status: Wrong Answer

Solution code:
# import sys
# import math #sqrt
# import decimal
# import queue # queue
# import bisect
# import heapq # priolity-queue
# import time
# import itertools
# import collections # deque
# from operator import itemgetter
# from fractions import Fraction

mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

def main():
    n,k = map(int,input().split())
    f = abs(n-k)
    if f==0:
        print(0)
        return
    print(max(f,k)%min(f,k))
    return

if __name__=='__main__':
    main()

Submission status: Wrong Answer

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

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

cnt = gcd(n, k)

if n == 1 or k == 1:
    print(0)
else:
    print(min(n, cnt))

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
elif n%k == 0:
    a = 0
else:
    a = abs(k-n%k)
print(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]
    min = table[0]

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

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



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math
n,k=map(int,input().split())
if n>k:
    num=math.floor(n/k)
    n=n-num*k
    ans=min(k-n,n)
    print(ans)
else:
    print(min(k-n,n))

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
import sys

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

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

def main():
    n, k = map(int, input().split())

    x = abs(n-k)

    if x == 0:
        print(0)
    elif x > k:
        print(x%k)
    else:
        print(k%x)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

if N<K:
    print(N)
elif K==1 or N==K:
    print(0)
else:
    for i in range(K):
        absolt=abs(absolt-K)
        if absolt<=temp or temp==0:
            temp=absolt
    print(temp)

Submission status: Wrong Answer

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

if n < k:
    ans = n
    print(ans)
    exit()

elif n % k == 0:
    ans = 0
    print(ans)
    exit()
else:
    for i in range(10000000000000000000):
        res = abs(n - k)
        if n <= res:
            ans = n
            print(ans)
            exit()
        else:
            n = res

Submission status: Wrong Answer

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

from fractions import gcd

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

Submission status: Wrong Answer

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


b=N
while (True):
    X=abs(b-K)
    if b>X:
        b=X
        print(b)
        continue
    else:
        break

print(int(b))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import fractions
N, K = map(int,input().split())
N = fractions.gcd(N,K)
for i in range(10*9):
    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 k == 1:
    print("0")
else:
    while i < n:
        x = abs(x - k)
        i += 1
        print(min(n, x))

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
            elif value_diff_abs == N :
                print(str(value_diff_abs))
                break


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

if K == 1:
    print(0)
    sys.exit()
if N > K:
    ans = (N%K)
    if ans != 1:
        ans = N%(N%K)
else:
    ans = N
print(ans)

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

    if N*2 == K:
        print(N)
        return

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

    x = N
    min = x
    while min >= x:
        print(min, x)
        min = x
        x = absdiff(x, K)

    print(min)
    return


culc(N, K)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
if K == 1:
    print(0)
elif N % K == 0:
    print(2)
else:
    while N > abs(N - K):
        N = abs(N - K)
    print(N)

Submission status: Wrong Answer

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

if n < k:
  print(n)
else:
  temp = int(n/k)
  remain = n - (k*temp)
  if remain > 0:
    remain = abs(remain-k)
  print(remain)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
 
s1 = n//k
if s1 == 0:
    if n==0:
        print(k)
    else:
        s1 = k//n
        ans = min(abs(k - n*(s1-1)),abs(k - n*(s1),))
        print(ans)
else:
    ans = min(abs(n - k*(s1+1)),abs(n - k*(s1),))
    print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

A = [N]
i=1

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:
N,K=map(int,input().split())
for i in range(10000000):
    N=abs(N-K)
    A=[N]
    
    
print(min(A))

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(1)
elif k == 1 or n == 1:
	print(0)
else:
	print(process(n,k))