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:
# -*- coding: utf-8
a,b = map(int,input().split())
yon = 100000000000000000000000
if a<b :
    print(a%b)
elif a == b :
    print(0)
else:
    a = a%b
    yon = min(a,abs(a-b))
    print(yon)

Submission status: Wrong Answer

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


def solve(N: int, K: int):
    ans = N
    N =  abs(N - K * math.ceil(N / K))
    if ans > N:
        ans = N
    for i in range(2):
        N = abs(N - K)
        if ans > N:
            ans = N


    print(ans)
    return


# Generated by 1.1.7.1 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:
#!/usr/bin/env python3
import sys
import math


def solve(N: int, K: int):
    ans = N
    N =  abs(N - K * math.ceil(N / K))
    if ans > N:
        ans = N
    print(ans)
    return


# Generated by 1.1.7.1 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())
print(n%k)

Submission status: Wrong Answer

Solution code:
a,b=[int(s) for s in input().split()]
print(a%b)

Submission status: Wrong Answer

Solution code:
import math
a,b=[int(s) for s in input().split()]
print(math.gcd(a,b))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys

n,k = map(int,input().split())
x = n+(n%k)
min = sys.maxsize

while True:
    if x%k == 0:
        min = 0
        break

    x = abs(x-k)
    if x == min :break
    elif x < min : min = x

print(min)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if n==0:
    print(k)
else:
    r = n%k
    print(min([r, k-r]))

Submission status: Wrong Answer

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

min = N
while True:
  #print (min)
  if min<=min%K:
    break
  else:
    min = min%K
print (min)

Submission status: Wrong Answer

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

residual = N % K
if residual==0 or N<=K:
    print(residual)
else:
    residual_new = abs(residual-K)
    if residual > residual_new:
        print(residual_new)
    else:
        print(residual)

Submission status: Wrong Answer

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

residual = N % K
if residual==0 or N<=K:
    print(residual)
else:
    print(residual-K)

Submission status: Wrong Answer

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

residual = N % K
if residual==0 or N<=K:
    print(residual)
else:
    R = N - K
    conti_flag = 1
    while conti_flag:
        if R < 0:
            conti_flag = 0
        else:
            R = R - K
    print(abs(R))

Submission status: Wrong Answer

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

if n==0:
    print(k)
else:
    r = n%k
    print(min(r, k-r))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
[N, K] = [int(i) for i in input().split()]
print(min(abs(N - K * int(N/K) ), abs(N - K * (int(N/K)+1) ) ))

Submission status: Wrong Answer

Solution code:
[N, K] = [int(i) for i in input().split()]
# print(abs(N - K*int(N/K)), abs(N - K*(int(N/K)+1)))
print(min(abs(N - K * int(N/K) ), abs(N - K * (int(N/K)+1) ) ))

Submission status: Wrong Answer

Solution code:
[N, K] = [int(i) for i in input().split()]
# print(abs(N - K*int(N/K)), abs(N - K*(int(N/K)+1)))
print(min(abs(N - K*int(N/K)), abs(N - K*(int(N/K)+1)) ))

Submission status: Wrong Answer

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline

def main():
    n,k = map(int,input().split())
    dp=[n,k,abs(n-k)]
    if n ==0:
        print(k)
        exit()
    elif k==1:
        print(0)
        exit()
    while True:
        if min(dp) > abs(k-dp[-1]):
            dp.append(abs(k-dp[-1]))
        else:
            break
    print(min(dp))

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline

def main():
    n,k = map(int,input().split())
    dp=[n,k,abs(n-k)]
    if k==1:
        print(0)
        exit()
    elif n ==0:
        print(k)
        exit()
    while True:
        if min(dp) > (k-dp[-1]):
            dp.append(k-dp[-1])
        else:
            break
    print(min(dp))

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline

def main():
    n,k = map(int,input().split())
    dp=[n,k,abs(n-k)]
    if n==1 or k==1:
        print(0)
        exit()
    elif n ==0 or k==0:
        print(min(n,k))
        exit()
    while True:
        if min(dp) > (k-dp[-1]):
            dp.append(k-dp[-1])
        else:
            break
    print(min(dp))

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline

def main():
    n,k = map(int,input().split())
    dp=[n,k,abs(n-k)]
    if min(dp)==1:
        print(0)
        exit()
    elif n ==0 or k==0:
        print(min(n,k))
        exit()
    while True:
        if min(dp) > (k-dp[-1]):
            dp.append(k-dp[-1])
        else:
            break
    print(min(dp))

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline

def main():
    n,k = map(int,input().split())
    dp=[]
    while True:
        dp.append(abs(n-k))
        dp.append(n)
        dp.append(k)
        if min(dp)==abs(n-k):
            n=abs(n-k)
        else:
            break
    print(min(dp))

if __name__=="__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if N % K == 0:
  print(0)
  
while True:
  N = N - K
  if N < 0:
    print(N + K)
    exit()

Submission status: Wrong Answer

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

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

print(sur)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if n < k:
  print(n)

elif n == k:
  print(0)
  
else:
  n = n % k
  rival_n = k - n
  if n >= rival_n:
    print(rival_n)
  else:
    print(n)

Submission status: Wrong Answer

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

prev = -1
while prev != min(N,K):
  prev = min(N,K)
  if N == K or N == 0 or K == 0:
    prev = 0
    break
  if N > K:
    N = N % K
  else:
    K = K % N
  
print(prev)

Submission status: Wrong Answer

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

prev = -1
while prev != min(N,K):
  prev = min(N,K)
  if N == K or N == 0 or K == 0:
    prev = abs(N-K)
    break
  if N > K:
    N = N % K
  else:
    K = K % N
  
print(prev)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if n < k:
    print(n)
    exit(0)
elif n == k:
    print(0)
    exit(0)
else:
    pre_n = n
    while n > 0:
        temp = n % k
        if pre_n > temp:
            pre_n = n
            n = temp
        else:
            print(n)
            exit(0)

    print(n)

Submission status: Wrong Answer

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

pre_n = n
while n > 0:
    temp = n % k
    if pre_n > temp:
        pre_n = n
        n = temp
    else:
        print(n)
        exit(0)

print(n)

Submission status: Wrong Answer

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

pre_n = float('inf')
while n > 0:
    temp = abs(n-k)
    if pre_n > n:
        pre_n = n
        n = temp
    else:
        print(n)
        exit(0)

print(n)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#C
N, K = map(int,input().split())
if N%K==0:
    print(0)
else:
    print(min(N%K,-N%(K+1)))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
a=N//K
if a==0:
    print(N)
elif N%K!=0:
    x=N-a*K
    y=abs(N-(a+1)*K)
    if x>y:
        print(y)
    else:
        print(x)
else:
    print(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = 0

ans_1 = int(N/K)
ans_2 = ans_1 + 1

ans = min(abs(N-ans_1*K), abs(N-ans_2*K))
print(ans)

Submission status: Wrong Answer

Solution code:
import math
n,k = map(int,input().split())
if n % k == 0:
    print(0)
else:
    print(math.gcd(n,k))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split(" "))
if N==0:
  print(0)
elif abs(N)%K==0:
  print(1)
else:
  M=abs(N)%K
  S=abs(M-K)
  print(min(M,S))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split(" "))
if abs(N)%K==0:
  print(1)
else:
  M=abs(N)%K
  S=abs(M-K)
  print(min(M,S))

Submission status: Wrong Answer

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

for i in range(N//K):
  if abs(N-K)<N:
    N=abs(N-K)
print(N)

Submission status: Wrong Answer

Solution code:
a,b = map(int, input().split())
if abs(a-b) > a:
    print(a)
else:
    if a%b == 0:
        print(0)
    else:
        print(min(a//b, abs(a%b-b)))

Submission status: Wrong Answer

Solution code:
a,b = map(int, input().split())
if abs(a-b) > a:
    print(a)
else:
    if a%b == 0:
        print(0)
    else:
        print(a//b)

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split(" "))
    while True:
        n, remain = divmod(n, k)
        if n <= 0: return remain
        n = remain

x = main()
print(x)

Submission status: Wrong Answer

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

if n % k == 0:
  print(0)
else: 
  ans = n % k + k
  while ans > abs(n-k):
    n = abs(n - k)
    ans = min(n, ans)
  print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
N, K = list(map(lambda n: int(n), input().split(" ")))
 
while N <= int(K/2):
  N = min([abs(N-K), N % K])

print(N)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
ans=0
a=[]
if k==1:
  print(0)
elif abs(n-k) > n:
  print(n)
else:
  while len(a)==len(set(a)):
    n=abs(n-k)
    if n == 1:
      break
    a.append(n)
  print(min(a))

Submission status: Wrong Answer

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
dic = defaultdict(int)
n,k = readInts()
dic[n] = 1
n %= k
n = abs(n-k)
while True:
    n = abs(n-k)
    if dic[n]:
        break
    dic[n] = 1

m = float('inf')
for k,v in dic.items():
    m = min(m, k)
print(m)

Submission status: Wrong Answer

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
dic = defaultdict(int)
n,k = readInts()
dic[n] = 1
n %= k
while True:
    n = abs(n-k)
    if dic[n]:
        break
    dic[n] = 1
m = float('inf')
for k,v in dic.items():
    m = min(m, k)
print(m)

Submission status: Wrong Answer

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n,k = readInts()
if k == 1:
    print(0)
elif n == 0:
    print(0)
elif n == 1:
    print(1)
else:
    print(gcd(n,k))

Submission status: Wrong Answer

Solution code:
import math
import collections
import fractions
import itertools
import functools
import operator
import bisect

def solve():
    n, k = map(int, input().split())
    if n < k:
        print(n)
    else:
        print(n%k)
    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import math
import collections
import fractions
import itertools
import functools
import operator
import bisect

def solve():
    n, k = map(int, input().split())
    if n <= k:
        print(n)
    else:
        print(n%k)
    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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


while n > 1000*(n-k):
  n = n- 1000*(n-k)


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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans1 = abs(N - K)
ans2 = abs(ans1 - K)

print(ans2)

Submission status: Wrong Answer

Solution code:
import sys
import math

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

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

n_start = math.floor(n / k)
# print(n_start)
n = n - n_start*k

# print(n)


while True:
    min_n = n
    n = abs(n-k)
    # print("n, k =", n, k)
    if min_n > n:
        min_n = n
    else:
        break
print(min_n)

Submission status: Wrong Answer

Solution code:
import sys
import math

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

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

n_start = math.floor(n / k)
# print(n_start)
n = n - n_start*k

# print(n)


while True:
    min_n = n
    n = abs(n-k)
    # print("n, k =", n, k)
    if min_n > n:
        min_n = n
    else:
        break
print(min_n)

Submission status: Wrong Answer

Solution code:
import sys
import math

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

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

n_start = math.floor(n / k)
# print(n_start)
n = n - n_start*k

# print(n)


while True:
    min_n = n
    n = abs(n-k)
    # print("n, k =", n, k)
    if min_n >= n:
        min_n = n
    else:
        break
print(min_n)

Submission status: Wrong Answer

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

if k == 1:
    print(0)
    exit()
    
elif n % k == 0:
    print(0)
    
else:
    while n >= abs(n-k):
        n = abs(n-k)
        print(n)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from math import ceil
def main():
    n, k = map(int, input().split())
    if k == 1:
        print(0)
        sys.exit()
    t = ceil(n/k)*k
    r = min(t-n, k)
    print(r)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n, k = map(int, input().split())
    if k == 1:
        print(0)
        sys.exit()
    r1 = abs(n - k)
    r = min(r1, k)
    print(r)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n, k = map(int, input().split())
    if k == 1:
        print(0)
        sys.exit()
    r1 = abs(n - k)
    r2 = abs(r1 - k)
    r = min(r1, r2)
    print(r)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n, k = map(int, input().split())
    r = abs(n - (n//k)*k)
    print(r)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if ( n <= k ):
    print(n)
    sys.exit()

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


pren = n
ans = 0
done = False

while done == False:
    n = abs(pren - k)
    if (n >= pren):
        ans = pren  
        done = True
    else:
        pren = n

print(ans)

Submission status: Wrong Answer

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

if N == 0 or K == 1:
    N = 0
elif N == K:
    N = 0
elif N % K == 0:
    N = 0
else:
    N = N % K

print(N)

Submission status: Wrong Answer

Solution code:
import math

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

t0 = math.floor(N / K) - 1
t1 = math.floor(N / K)
t2 = math.ceil(N / K)
t3 = math.ceil(N / K) + 1
t4 = t0 - 1
t5 = t3 + 1

res = min(abs(N - t1 * K), abs(N - t2 * K), abs(N - t0 * K), abs(N - t3 * K), abs(N - t4 * K), abs(N - t5 * K))


print(res)

Submission status: Wrong Answer

Solution code:
import math

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

t0 = math.floor(N / K) - 1
t1 = math.floor(N / K)
t2 = math.ceil(N / K)
t3 = math.ceil(N / K) + 1

res = min(abs(N - t1 * K), abs(N - t2 * K), abs(N - t0 * K), abs(N - t3 * K))

print(res)

Submission status: Wrong Answer

Solution code:
import math

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

t1 = math.floor(N / K)
t2 = math.ceil(N / K)

res = min(abs(N - t1 * K), abs(N - t2 * K))

print(res)

Submission status: Wrong Answer

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

def act(int1, int2):
    return abs(int1-int2)

if N < K:
    pass
elif N == K:
    N = 0
elif N%K == 0:
    N = 0
else:
    while N >= K:
        N = act(N, K)

print(N)

Submission status: Wrong Answer

Solution code:
import sys
n, k = map(int, sys.stdin.buffer.read().split())
print(n % k)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math

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

if N == 1 or K == 1:
    print(0)
else:
    print(math.gcd(N,K))

Submission status: Wrong Answer

Solution code:
from math import gcd

n, k = map(int, input().split())
if n == 1 or k == 1:
    print(0)
else:
    print(gcd(n, k))

Submission status: Wrong Answer

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

N, K = map(int, input().split())
x = abs(N-K)
if x == 0:
  print(K)
  exit()

remainder = K % x if K >= x else x % K

if remainder > x // 2:
  print(abs(remainder - x))
else:
  print(remainder)

Submission status: Wrong Answer

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

num_count = (N-N%K)//K
if num_count != 0:
    ans = min(abs(N - num_count*K), abs(N - (num_count+1)*K))
else:
    ans = N
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

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

if n // k != 0:
    print(min(n % k, n // k))
else:
    print(min(n % k, n))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

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

if n // k != 0:
    m = n // k
    print(min(n % k, m))
else:
    print(n)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
m = 10 ** 18
j = n // k

for i in range(j + 1):
    m = min(m,abs(n-k))
    n = abs(n - k)

print(m)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

print(min(abs(N - K * a), abs(b)))

Submission status: Wrong Answer

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

x = n
li = [n]
while True:
    x = abs(k - x)
    if x in li:
        break
    li.append(x)

Submission status: Wrong Answer

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


if N%K == 0:
    print(0)
if N%K != 0 and N > K :
    a = N%K
    b = K%a
    if a >= b :
        print(b)
    if a < b:
        print(a)
if N%K != 0 and N < K :
    print(N)

Submission status: Wrong Answer

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

if N%K == 0:
    print(0)
if N%K != 0 and N > K :
    a = N%K
    b = K%a
    if a >= b :
        print(b)
    if a < b:
        print(a)
if N%K != 0 and N < K :
    c = K%N
    if N >= c :
        print(c)
    if N < c:
        print(c)

Submission status: Wrong Answer

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

while True:
    if K <= N-K:
        print(K)
        break
    elif K < K-N:
        print(K)
        break
    elif N >= K:
        N = N - K
        if N <= N-K:
            print(N)
            break
    elif N < K:
        N = K - N
        if N <= K-N:
            print(N)
            break

Submission status: Wrong Answer

Solution code:
N, K = input().split(' ')
NA = int(N)
K = int(K)
if NA - K > K:
  NA = NA - K*(NA//K - 1)
  print(NA)
N1 = abs(NA-K)

while N1 < NA:
  NA = N1
  N1 = abs(N1-K)
else:
  print(NA)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math  # noqa: F401
from typing import Dict, List, Optional, Tuple, Union  # noqa: F401


def main(N: int, K: int):
    if N == K:
        print(0)
        return

    if N > K:
        N -= N // K * K
        N = min((N, abs(N - K)))

    print(N)


if __name__ == "__main__":
    N, K = map(int, input().split())
    main(N, K)

Submission status: Wrong Answer

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

if N % K == 0:
  answer = 0
else:
  if N % K >= N:
    answer = N
  else:
    answer = N % K

  
print(answer)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

s, a = divmod(n, k)

ans = 0
if (a == 0):
    ans = s
else:
    ans = a

print(ans)

Submission status: Wrong Answer

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

s = abs(n-k)

if n % k == 0:
    print("0")
    exit()

if s > n % s:
    print(n%s)

else:
    print(s)

Submission status: Wrong Answer

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

s = abs(n-k)

if n % k == 0:
    print("0")
    exit()

if s < n % s:
    print(n%s)

else:
    print(s)

Submission status: Wrong Answer

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

s = abs(n-k)

if n % k == 0:
    print("0")
    exit()

if s >= n % s:
    print(n%s)

else:
    print(s)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
 
if n % k == 0:
    print("0")
    exit()

if n % 10 == 0 and k % 3 == 0:
    print("1")
 
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 == 0:
    print("0")
    exit()

print(n % abs(n - k))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k = [int(i) for i in input().split()]
temp = 0
cont = 0
a = 0
if n%k==0: print(0)
elif k%n==0: print(n)
else:
    if k>n: print(n)
    else: print(1)

Submission status: Wrong Answer

Solution code:

import math
n,k = [int(i) for i in input().split()]
temp = 0
cont = 0
a = 0
if n%k==0: print(0)
elif k%n==0: print(n)
else:
    gcd = math.gcd(n,k)
    n=n//gcd
    k=k//gcd
    while True:
        n = abs(n-k)
        if temp <= n:
            a=temp
            cont+=1
        if cont>=2: break
        temp=n
    
    print(temp)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split());
a = [0]*2
i = 0
while True:
    x = n - k;
    if x >= 0:
        n = x
    else:
        n = (-1)*x;
    if i == 0:
        if a[1] == n:
            break;
    else:
        if a[0] == n:
            break;
    i = (i+1) % 2;
    a[i] = n;

print(a[i]);

Submission status: Wrong Answer

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

if N < K:
    ans = 0
elif N % K == 0:
    ans = 0
else:
    ans = abs(N % K - K)



print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
ans = 0
# if N > K and K == 1:
#     print(0)
#     exit(0)
if N < abs(N - K):
    ans = 0
elif N % K == 0:
    ans = 0
else:
    ans = abs(N % K - K)
# while N > abs(N - K):
#     N = abs(N - K)


print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if n < k:
    print(n)
elif n % k == 0:
    print(0)
elif n % k != 0:
    print(min(k-(n % k), n % k))

Submission status: Wrong Answer

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

if n<k:
    print(n)
elif n%k==0:
    print(0)
elif n%k!=0:
    print(k-(n%k))

Submission status: Wrong Answer

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

for _ in range(n):

    if ans > n:
        ans = n

    n = abs(n-k)

print(ans)

Submission status: Wrong Answer

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



def solve(n, k):
    res = n // k

    if res == n:
        print(0)
    elif res == 0:
        print(n)
    else:
        print(res)

solve(n,k)

Submission status: Wrong Answer

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

cnt = 0
ans = N

if N == 0 or K == 1:
    print(0)
    exit()

ans2 = N
ans1 = K % N
if K > N:
    ans2 = N - ( K % N )
    ans1 = N
else:
    ans2 = K - ( N % K )
    ans1 = N % K

if ans1 < ans2:
    print(ans1)
else:
    print(ans2)

Submission status: Wrong Answer

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

cnt = 0
ans = N

if N == 0:
    print(0)
    exit()

ans2 = N
ans1 = K % N
if K > N:
    ans2 = N - ( K % N )
    ans1 = N
else:
    ans2 = K - ( N % K )
    ans1 = N % K

if ans1 < ans2:
    print(ans1)
else:
    print(ans2)

Submission status: Wrong Answer

Solution code:
import math
N, K = map(int, input().split())
if N == K:
    ans = 0
elif N < K:
    ans = N
else:
    ansmin = N-K*math.ceil(N/K)
    ansmax = K-ansmin
    ans = min(abs(ansmin), abs(ansmax))
print(ans)