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:
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(ansmin, ansmax)
print(abs(ans))

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:
    ans = K*math.ceil(N/K)-N

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
import math
if K>=N:
  N=min(K-N,N)
else:
  N=N-(math.ceil(N/K-1)*K)
  N=min(abs(K-N),N)
print(N)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
import math
if K>=N:
  N=min(K-N,N)
else:
  N=N-(math.floor(N/K)-1)*K
  N=min(K-N,N)
print(N)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
import math
if K>=N:
  N=min(K-N,N)
else:
  N=N-(math.ceil(N/K)-1)*K
  N=min(K-N,N)
print(N)

Submission status: Wrong Answer

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

if N >= K:
  A = N//K
  B = N - A*K
  print(min([B, abs(B-K)]))
else:
  if K >= 2:
    print(N)
  else:
    print(abs(N-K))

Submission status: Wrong Answer

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

if n % k == 0:
  print(0)
elif n / k != 0:
  min = abs(n - (k * int(n / k)))
  if min > abs(min - k):
    min = abs(min - k)
  print(min)
elif n > abs(n - k):
  print(abs(n - k))
else:
  print(n)

Submission status: Wrong Answer

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

N,K = map(int,input().split())
for i in range (N):
  if abs(N)>abs(N-K):
    N-=K
  else:
    print(abs(N))
    exit()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from math import gcd
from sys import exit

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

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

print(gcd(N,M))

Submission status: Wrong Answer

Solution code:
import math
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:
import math
n,k = map(int,input().split())
     
m = math.floor(n/k)
n = n-m*k
     
print(min(n,k-n))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

if n > int(abs(n-k)):
  n = int(abs(n-k))
print(n)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
import math
n, k = map(int, input().split())
ans = min(n%k, k*math.ceil(n/k)-n)
print (ans)

Submission status: Wrong Answer

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

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

D1=N//K
D2=N//K+1

Ans=min (abs(N-D1*K),abs(N-D2*K))
print(Ans)

Submission status: Wrong Answer

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

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

D1=N//K
D2=(N//K) +1

Ans=min (abs(D1*K-N),abs(D2*K-N))
print(Ans)

Submission status: Wrong Answer

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

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

D1=int(N//K)
D2=int(N//K)+1

Ans=min (abs(D1*K-N),abs(D2*K-N))
print(Ans)

Submission status: Wrong Answer

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

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

DM2=N//K-2
DM1=N//K-1
D0=N//K
DP1=N//K+1
DP2=N//K+2

Ans=min (abs(DM2*K-N),abs(DM1*K-N),abs(D0*K-N),abs(DP1*K-N),abs(DP2*K-N))
print(Ans)

Submission status: Wrong Answer

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

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

D1=N//K
D2=N//K+1

Ans=min (abs(D1*K-N),abs(D2*K-N))
print(Ans)

Submission status: Wrong Answer

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

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

D1=N//K
D2=N//K+1

Ans=min (abs(D1*K-N),abs(D2*K-N))
print(Ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if N <= K:
    print(min(N, abs(N-K)))
else:
    print(int(min(N-int(N/K)*K, abs(N-(int(N/K)+1)*K))))

Submission status: Wrong Answer

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

## Library

import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter

## input

# n=int(input())
# A,B,C,D=map(int, input().split())
# string = input()
# yoko = list(map(int, input().split()))
# tate = [int(input()) for _ in range(N)]
# N, M = map(int,input().split()) 
#    P = [list(map(int,input().split())) for i in range(M)]

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

## Logic

print(abs(K-(N%K)))

Submission status: Wrong Answer

Solution code:
def resolve():
    N, K = list(map(int, input().split()))
    if (K == 1):
        print(0)
        return
    if N % K == 0:
        print(0)
        return
    if (K > N):
        print(N)
        return
    x = N
    memo = [N]
    while True:
        x = min(abs(x - K), x)
        if x in memo:
            print(min(memo))
            return
        memo.append(x)
    return


resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    N, K = list(map(int, input().split()))
    if (K == 1):
        print(0)
        return
    if N % K == 0:
        print(N / K)
        return
    x = N
    memo = [N]
    while True:
        x = min(abs(x - K), x)
        if x in memo:
            print(min(memo))
            return
        memo.append(x)
    return


resolve()

Submission status: Wrong Answer

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


def solve(N: int, K: int):
    print(abs(N - K*round(N/K)))


# 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


def solve(N: int, K: int):
    return abs(N - K*round(N/K))


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

if abs(n-k)<n:
    n = abs(n-k)
    print(n)
else:
    print(n)

Submission status: Wrong Answer

Solution code:
import sys
n,k=map(int,input().split())
x=n
res=0
for i in range(1000000):
    x=min(abs(x-k),x)
print(x)

Submission status: Wrong Answer

Solution code:
import sys
n,k=map(int,input().split())
x=n
res=0
for i in range(10000):
    x_1=x
    x=min(abs(x-k),x)
    if x_1==x:
        res+=1
        if res>3:
            print(x)
            sys.exit()

Submission status: Wrong Answer

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

def change(n,k):
    if n <= k:
        print(n)
    elif n % k == 0:
        print(0)
    else:
        for i in range(n):
            a = []
            a.append(n)
            n = abs(n-k)
            if a[-1] < n:
                print(a[-1])
                break

change(n,k)

Submission status: Wrong Answer

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

def change(n,k):
    if n <= k:
        return n
    elif n % k == 0:
        return 0
    else:
        for i in range(n):
            a = []
            a.append(n)
            n = abs(n-k)
            if a[-1] < n:
                print(a[-1])
                break

change(n,k)

Submission status: Wrong Answer

Solution code:

try:
    N_s,K_s=input().split(' ')
    n=int(N_s)
    k=int(K_s)
    if n>=0 and n<=10**18 and k>=1 and k<=10**18:
        
        if k==1 and n==10**18:
            print(0)
        else:
            n=abs(n-k)
            minn=n
            counter=1
            while True:
                
                n=abs(n-k)
                if n<minn:
                    minn=n
                    counter+=1
                else:
                    break
            print(counter)
                
            
except Exception as e:
    print(e)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
list = []
ans = 0

if k > n:
  ans = n
else:
  if n%k == 0:
    ans = 0
  else:
    a = n%k
    if abs(a-k) > a:
      ans = a
    else:
      ans = abs(a-k)
    
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=set()
for i in range(1,100000):
    n=n%k
    a.add(n)
    n=abs(n-k)
a=list(a)    
print(a[0])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN

n, k = list(map(int, input().split()))
s = Decimal(str(n/k)).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
print(abs(s*k - n))

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

#     if k % n == 0:
#         break

#     new = abs(n-k)
#     if new < n:
#         n = new
#         continue
#     else:
#         break

# print(n)

Submission status: Wrong Answer

Solution code:
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN

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

if n == 0:
    print(0)

elif n % k == 0:
    print(0)

elif k % n == 0:
    print(n)

else:
    s = Decimal(str(n/k)).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
    print(abs(s*k - n))

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

#     if k % n == 0:
#         break

#     new = abs(n-k)
#     if new < n:
#         n = new
#         continue
#     else:
#         break

# print(n)

Submission status: Wrong Answer

Solution code:
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN

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

if n == 0:
    print(k)

elif n % k == 0:
    print(0)

elif k % n == 0:
    print(n)

else:
    s = Decimal(str(n/k)).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
    print(abs(s*k - n))

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

#     if k % n == 0:
#         break

#     new = abs(n-k)
#     if new < n:
#         n = new
#         continue
#     else:
#         break

# print(n)

Submission status: Wrong Answer

Solution code:
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN

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

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

elif k % n == 0:
    print(n)

else:
    s = Decimal(str(n/k)).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
    print(abs(s*k - n))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

tmp = [n]

if k == 1:
    ans = 1
else:
    while True:
        m = abs(n - k)
        if m in tmp:
            ans = min(tmp)
            break
        else:
            tmp.append(m)
            n = m

print(ans)

Submission status: Wrong Answer

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

print(abs(n - k * round(n / k)))

Submission status: Wrong Answer

Solution code:
import math
def resolve():
    N,K = map(int,input().split())
    A = math.gcd(N,K)
    if N == 1 or K == 1:
        print(0)
    else: 
        print(A)
resolve()

Submission status: Wrong Answer

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math


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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

if n>k:
    if x<=k:
        print(k%x)
    else:
        print(x%k)
else:
    print(min(n, x))

Submission status: Wrong Answer

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

if n>k:
    print(x%k)
else:
    print(min(n, x))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
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)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import os
import heapq
import sys
import math
from collections import defaultdict
from io import BytesIO, IOBase
def check_kth_power(n, k):
        while n%k == 0:
            n = n/k

        if n != 1:
            return False
        return True
def main():
    #for _ in range(int(input())):
    n,k=map(int,input().split())
    if n<=k:
        print(n)
    else:
        if n%k==0:
            res=k*(n//k)-n
            print(res)
        else:
            res=k*(n//k+1)-n
            print(res)
        
BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
list = [n]
list.append(abs(n-k))
while(abs(list[-1]) >= abs(list[-2])):
	n = abs(n-k)
	list.append(n)

print(min(list))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
list = [n]
list.append(abs(n-k))
while(list[-1] >= list[-2]):
	n = abs(n-k)
	list.append(n)

print(min(list))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
list = [n]
list.append(abs(n-k))
while(list[-1] < list[-2]):
	n = n-k
	list.append(n)

print(min(list))

Submission status: Wrong Answer

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

for i in range(10):
	n = abs(n-k)
	list.append(n)

print(min(list))

Submission status: Wrong Answer

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

def solve():
    global n, k
    if n == k or n % k == 0:
        return 0
    if n < k:
        return n
    
    n = n - (k * (n // k))
    pre = n
    while True:
        n = min(n, abs(n-k))
        if n == pre:
            break
        pre = n
    return n

print(solve())

Submission status: Wrong Answer

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

x = [n-k*i for i in range(n//k+2)]
x[-1] = abs(x[-1])
print(x)
print(min(x))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
def op(n, k):
  return abs(n - k)


if __name__ == '__main__':
    n, k = map(int, input().split())
    print(abs(n % k))

Submission status: Wrong Answer

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

"""
while N > K:
  N = abs(N - K)
  #print(N)

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

m = -(-N//K)
print(min(N - m*K, -(N - (m+1)*K)))

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

Submission status: Wrong Answer

Solution code:
from math import gcd

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

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

Submission status: Wrong Answer

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

"""
while N > K:
  N = abs(N - K)
  #print(N)

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

m = -(-N//K)
print(abs(N - m*K))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=N
f=[]
while S>K:
    S=abs(S-K)


f.append(abs(S-K))
f.append(abs(S-2*K))

    
    
    
print(min(f))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
se_ = set([N])
a = N % K
while True:
    a = max(K-a,a-K)
    if a in se_:
        break
    se_.add(a)

print(min(se_))

Submission status: Wrong Answer

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


ggg = math.gcd(N, K)

if ggg == K:
    print(0)

else:
    print(ggg)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
list = []
ans = 0

if k > n:
  print(n)
else:
  if n%k == 0:
    ans = 0
  else:
    ans = 1
    
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
list = []
ans = 0

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

print(ans)

Submission status: Wrong Answer

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

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:
    print(n)
elif n==k:
    print(int(0))
else:
    a=int(n-k*(n//k))
    print(min(a,abs(a-k)))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
if n<k:
    print(n)
elif n==k:
    print('0')
else:
    a=int(n-k*(n//k))
    print(min(a,abs(a-k)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
if k==1:
    print(1)
else:
    d = [n]
    x = abs(n-k)
    while(x not in d):
        d.append(x)
        x = abs(x-k)
    print(min(d))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
arr=[]
arr.append(n)
for i in range(2):
    n=abs(n-k)
    arr.append(n)
print(min(arr))

Submission status: Wrong Answer

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

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

n = N
c = True
while(c):
    if n > K:
        # n - abs(n - K) => n - (n - K) => K > 0
        n = n - max(int(n / K) - 2, 1) * K
    elif n < K:
        # n - abs(n - K) => n - (K - n) => 2n - K
        if 2 * n - K > 0:
            n = K - n
        else:
            c = False
    else:
        # n == K で n - abs(n - K) => n（変更後も同じ）
        c = False
print(n)

Submission status: Wrong Answer

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

n = N
c = True
while(c):
    if n > K:
        # n - abs(n - K) => n - (n - K) => K > 0
        n = n - max(int(n / K) - 1, 1) * K # ( > n - K)
    elif n < K:
        # n - abs(n - K) => n - (K - n) => 2n - K
        if 2 * n - K > 0:
            n = K - n
        else:
            c = False
    else:
        # n == K で n - abs(n - K) => n（変更後も同じ）
        c = False
print(n)

Submission status: Wrong Answer

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

n = N
c = True
while(c):
    if n > K:
        # n - abs(n - K) => n - (n - K) => K > 0
        n = n - int(n / K) * K
    elif n < K:
        # n - abs(n - K) => n - (K - n) => 2n - K
        if 2 * n - K > 0:
            n = K - n
        else:
            c = False
    else:
        # n == K で n - abs(n - K) => n（変更後も同じ）
        c = False
print(n)

Submission status: Wrong Answer

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

N_set = set([])
N_set.add(float('inf'))
N_new = N

while (N_new > 0 and N_new < min(N_set)):
    N_set.add(N_new)
    if N_new >= K:
        N_new = N_new % K
    else:
        N_new = K - N_new
    print("N_new", N_new)

N_set.add(N_new)

ans = min(N, min(N_set))

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
for i in range((n+1)//k):
    n = abs(n-k)
print(n)

Submission status: Wrong Answer

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

if N % K == 0:
    res = 0
elif N % K > 2 / K:
    res = K - (N % K)
else:
    res = N % K

print(res)

Submission status: Wrong Answer

Solution code:
from math import ceil
N, K = map(int, input().split())

if N<=K:
    print(min(abs(N-K), N))
else:
    print(min(N - (N//K) * K, abs(N - ceil(N/K)*K), N, abs(K-N)))

Submission status: Wrong Answer

Solution code:
from math import ceil
N, K = map(int, input().split())

print(min(N - (N//K) * K, abs(N - ceil(N/K)*K), N, abs(K-N)))

Submission status: Wrong Answer

Solution code:
from math import ceil
N, K = map(int, input().split())

print(min(N - (N//K) * K, abs(N - ceil(N/K)*K), N))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# ABC161C

def f(n, k):
    for i in range(10):
        if n < k:
            print(n)
        else:
            m =  n % k
            if m <= k:
                print(m)
            else:
                n = abs(n - k)
                print(k)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=N%K
T=N//K
if T==0:
    print(N)
else:
    if S==0:
        print(0)
    else:
        if S-K<0:
            print(K-S)
        else:
            print(S)

Submission status: Wrong Answer

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

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

t2 = K % (abs(N - K))

if t2 > N:
    print(N)
else:
    print(t2)

Submission status: Wrong Answer

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

t1 = abs(N - K)

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

t2 = K % t1

if t2 > N:
    print(N)
else:
    print(t2)

Submission status: Wrong Answer

Solution code:
x,y=map(int,input().split())
if x%2!=0 and y%2==0:
    print(1)
else:
    print(x%y)

Submission status: Wrong Answer

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

s=math.ceil(n/k)

print(min(abs(n-s*k),n,abs(n-(s-1)*k)))

Submission status: Wrong Answer

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

s=math.floor(n/k)

print(min(abs(n-s*k),n))

Submission status: Wrong Answer

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

s=math.ceil(n/k)

print(min(abs(n-s*k),n))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
if k >= 2*n:
	print(n)
elif k == n:
	print(0)
elif k == 1:
	print(0)
elif n > k:
	print(min(n%k,1))
else:
	print(k%n)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
if k >= 2*n:
	print(n)
elif k == n:
	print(0)
elif k == 1:
	print(0)
elif n == 0:
	print(0)
else:
	print(1)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
if k >= 2*n:
	print(n)
elif k == n:
	print(0)
elif k ==1:
	print(0)
else:
	print(1)

Submission status: Wrong Answer

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

if k == 1:
    print(0)
else:
    print(min(abs(n - k), abs(abs(n-k) - k)))

Submission status: Wrong Answer

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

a = n // k

if n - k * a <= -(n - k * (a + 1)):
    print(n - k * a)

else:
    print(-n)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
for i in range(300):
  if 0 <= N-K <= N:
    N = N-K
  elif 0 <= K-N <= N:
    N = K-N
print(N)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
for i in range(300):
  if 0 <= N-K < N:
    N = N-K
  elif 0 <= K-N <= N:
    N = K-N
print(N)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
for i in range(150):
  if 0 <= N-K < N:
    N = N-K
  elif 0 < K-N <= N:
    N = K-N
print(N)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
for i in range(100):
  if 0 <= N-K < N:
    N = N-K
  elif 0 < K-N < N:
    N = K-N
print(N)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import math

n, k = [float(x) for x in stdin.readline().rstrip().split()]
print(int(min(abs(n-math.floor(n/k)*k), abs(n-math.ceil(n/k)*k))))

Submission status: Wrong Answer

Solution code:
from sys import stdin
import math

n, k = [int(x) for x in stdin.readline().rstrip().split()]
print(min(abs(n-math.floor(n/k)*k), abs(n-math.ceil(n/k)*k)))

Submission status: Wrong Answer

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

def gcd(a,b):
    b,a = sorted([a,b])
    while(b!=0):
        save = b
        b = a%b
        a = save
    return a


if(n%k==0):
    print(0)
else:
    print(gcd(n,k))

Submission status: Wrong Answer

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

else:
    num = N//K
    delta = abs(N-num*K)
    if delta < abs(delta-K):
        print(abs(delta-K))
    else:
        print(delta)

Submission status: Wrong Answer

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

else:
    num = N//K
    print(num)
    delta = abs(N-num*K)
    if delta < abs(delta-K):
        print(abs(delta-K))
    else:
        print(delta)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#K = int(input())
N,K = map(int,input().split())
#N = int(input())
#A = list(map(int,input().split()))
flag=True
c=N
if N%K==0:
    print(0)
else:
    l=N//K
    N=l*N
    while flag==True:

        N=abs(N-K)
        if c <= N:
            flag=False
        else:
            c=N
print(c)

Submission status: Wrong Answer

Solution code:
#K = int(input())
N,K = map(int,input().split())
#N = int(input())
#A = list(map(int,input().split()))
flag=True
c=N
if N%K==0:
    print(0)
else:
    l=N//K
    while flag==True:

        N=abs(l-K)
        if c <= N:
            flag=False
        else:
            c=N
print(c)

Submission status: Wrong Answer

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

if N%K > N:
  print(N)
  exit()

N = N%K
N = abs(N-K)
print(N)

Submission status: Wrong Answer

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