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= list(map(int,input().split()))
Num = 10*18
for i in range(N):
    if N % K ==0:
        Num = 0
        break
    if Num > abs((N%K)-K):
        Num = abs((N%K)-K)
        N  = Num
        if N < abs((N%K)-K):
            break
print(Num)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K=list(map(int,input().split()))
n=N%K
old_n=K+1
while True:
  if old_n<=n:
    print(old_n)
    break
  old_n=n
  N=abs(n-K)

Submission status: Wrong Answer

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

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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


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


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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

result = abs(N - K)

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if N < K:
    print(N)
elif N >= K:
    for i in range(P):
        N = abs(N-K)
    print(N)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
ans=float('inf')
if k == 1:
    print(0)
    exit()
for i in range(n):
    n=abs(n-k)
    ans=min(ans,n)
    
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
L=list(map(int, input().split()))
N=L[0]
K=L[1]
m1=0
m2=0
m=0

if N<K:
    print(N)
elif N==K:
    print(0)
else:
    m1=abs(N % K)
    m2=abs((N % K) - K)
    m=min(m1,m2)
    print(m)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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



while True:
    if k == 1:
        print('0')
    
    if n >= abs(n-k):
        n = abs(n-k)
    
    else:
        n <= abs(n-k)
        break

print(n)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

def substract(min):
    if N % K == 0:
        return 0
    tmp = N % K
    if abs(tmp - K) < min:
        return abs(tmp-K)
    return min

if abs(N-K) < N or abs(N-K) < K:
    print(substract(abs(N-K)))
elif N < K:
    print(N)
else:
    print(K)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

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

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

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

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


def mod(n, k):
    while k:
        n, k = k, n % k
    return n


def diff(n, k):
    while abs(n - k) < n:
        n = abs(n - k)
    return n


def main():
    N, K = map(int, input().split())

    result = mod(N, K)
    result = diff(result, K)
    print(result)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
if K == 1 or N == 0 or N == K:
    print(0)
    exit()
elif K == 0:
    print(N)
    exit()
elif N == 1 or N == K-1 or K == N-1:
    print(1)
    exit()
else:
    l = []
    ans = N
    for i in range(max(N,K)):
        ans = abs(ans - K)
        l.append(ans)
        if i >= 10:
            if ans in l:
                print(min(l))
                exit()
    print(min(l))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a,b=input().split()
a=int(a)
b=int(b)
y=[10**18]
f=0
for i in range (10000):
    x=b-a
    if x==0:
        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:
in_ = input()
n, k = map(int, in_.split())

while True:
    n = n % k
    if n == 0:
        r = n
        break
    r = n - k
    if r < 0:
        if n > r:
            r = n
        break
    else:
        n = r
print(abs(r))

Submission status: Wrong Answer

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

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

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

print(min(ans))

Submission status: Wrong Answer

Solution code:
import sys
a = [int(i) for i in input().split()]
N = a[0]
K = a[1]

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


def resZ(i, K):
    return abs(i - K)


for i in range(10):
    ab = resZ(N, K)
    if N < ab:
        print(N)
        sys.exit()

    N = ab
# def

Submission status: Wrong Answer

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

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

ans = []
before = abs(N - K)
N = N % K
for i in range(10000):
    ze = abs(N - K)
    ans.append(ze)
    before = ze
    N = ze
print(min(ans))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
left = 0
right = 10**19

while right - left >1:
  middle = (right + left)//2
  if middle < abs(middle - k) :
    left = middle
  else:
    right = middle
if n == 0:
  print(0)
elif k//n==2:
  print(n)
elif n % k == 0:
  print(0)
else:
  print(min(left,abs(n-k),n))

Submission status: Wrong Answer

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


def main(n, k):
    print(n, k)
    if n % k == 0:
        # print('n%k=0')
        res = 0
    else:
        # print(n % k, abs((n % k)-k), n)
        res = min(n % k, abs((n % k) - k), n)

    print(res)


main(N, K)
# main(7, 4)
# main(2, 6)
# main(1000000000000000000, 1)
# main(9, 6)

Submission status: Wrong Answer

Solution code:
def gcd(x, y): 
  
   while(y): 
       x, y = y, x % y 
  
   return x 

n, k = [int(x) for x in input().strip().split()]

if n==0:
    res = 0
elif k==0:
    res=n
else:
    res = gcd(n,k)

if n>=k:
    if res==k:
        res=0

print(res)

Submission status: Wrong Answer

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

a = int(N / K)

b = abs(int(N - K * a))
c = abs(int(b - K))

print(min(b,c,N))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
t = int(n/k)
tt = t + 1
ttt = t - 1
ans=min([n - t*k,tt*k - n,n - ttt*k])
print(str(ans))

Submission status: Wrong Answer

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

initial = N - (K * int(N / K))
if abs(initial - K) < initial:
    print(abs(initial - K))
else:
    print(initial)

Submission status: Wrong Answer

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

if n > k:
  n = n - k*int(n/k)
  n_ = abs(n-k)
  print(min([n, n_]))
elif n < k:
  n_ = abs(n-k)
  print(min([n, n_]))
else:
  print(0)

Submission status: Wrong Answer

Solution code:
import itertools
import math
import fractions

n,k = map(int,input().split())
ans = n
if k == 1 or n == 0 or n==k :
    print("0")
    exit()

if n > k:
    ans = n%k



while(1):
    
    ans = abs(ans-k)
    if ans == n:
        break
    if ans == 1 or 0:
        break
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if N==0:
    print(0)

N=min(N%K,K-N%K)
cand1=abs(N-K)
cand2=abs(cand1-K)
print(min(N,cand1,cand2))

Submission status: Wrong Answer

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

    print(store)

Submission status: Wrong Answer

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

if N % K == 0:
    print('0')
elif K % N == 0:
    print(N)
elif N <= K:
    print(min(N, (N-K)))
elif N > K:
    print('1')

Submission status: Wrong Answer

Solution code:
from fractions import gcd


def main():
    n, k = map(int, input().split())
    if n < k:
        print(min(n, k - n))
    elif k == 1 or n == 0 or n == k or n == 1:
        print(0)
    elif n % k == 0:
        print(0)
    else:
        ans = gcd(n, k)
        print(ans)


if __name__ == '__main__':
    main()

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 = abs(N-K)
    if K == 1:
        print(0)
    elif K < Q:
        print(N-Q)
    else:
        print(abs(K-Q))



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys
import math

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

x = N

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
min_kakeru = int(n/k)
max_kakeru = int(n/k) + 1
a1 = abs(n - min_kakeru * k)
a2 = abs(n - max_kakeru * k)

if n == 0:
  print(int(0))
elif a1 < a2:
  print(int(a1))
else:
  print(int(a2))

Submission status: Wrong Answer

Solution code:
num = list(map(int, input().split()))
print(min([num[0],num[0]%num[1],abs((num[0]//num[1])-num[1])]))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
def g(n,k):
    for i in range(k):
        if n%k == i:
            print(i)
if k == 1:
    print(0)
elif k == 2:
    g(n,k)
elif k == 3:
    g(n,k)
elif k == 4:
    g(n,k)
elif k == 5:
    g(n,k)
elif k == 6 or k == 7 or k == 8 or k == 9 or k == 10:
    g(n,k)
else:
    while n > k:
        n = n-k
    if n < k-n:
        print(n)
    else:
        print(k-n)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
import fractions

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

Submission status: Wrong Answer

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

var = N % K
if var == 0 or N == 0:
        print(0)
elif var == K-1 or var == 1:
        print(1)
else:
        ans = N
        while ans != N:
                ans = N
                N = N // K
        print(ans)

Submission status: Wrong Answer

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

if N <= abs(N-K):
    pass
else:
    for i in range(N+1):
        try:
            if ((N-i) / K).is_integer():
                ans = i
                break
            elif ((N+i) / K).is_integer():
                ans = i
                break
        except:
            break

print(ans)

Submission status: Wrong Answer

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

if n < k:
    if n > k / 2:
        print(k - n)
    else:
        print(n)
else:
    s = math.ceil(n / k)
    ans = abs(n - s  * k)
    ans = min(ans,abs(ans-k))
    print(ans)

Submission status: Wrong Answer

Solution code:
def gcd(a, b):
    while a:
        a, b = b % a, a
    return b


n, k = map(int, input().split())
if n <= k:
    print(min(n, k-n))
else:
    print(gcd(n, k))

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

Submission status: Wrong Answer

Solution code:
n, k = (int(i) for i in input().split())
if k > n:
    print(n, abs(n-k))
elif n % k == 0:
    print(0)
else:
    q = n % k
    p = (k * (n//k+1)) - n
    print(min(p, q))

Submission status: Wrong Answer

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

def gcd(a,b):
  a, b = max(a, b), min(a, b)
  while b!=0:
    a,b=b,a%b
  return a

if N < K:
  print(min(N, abs(N-K)))
elif N == K:
  print(0)
else:
  ans = gcd(N, K)
  if ans == K:
    print(0)
  else:
    print(ans)

Submission status: Wrong Answer

Solution code:
num = list(map(int, input().split()))
min([num[0],num[0]%num[1],abs((num[0]//num[1])-num[1])])

Submission status: Wrong Answer

Solution code:
import itertools
import math
import fractions

n,k = map(int,input().split())
ans = n
if k == 1 or n == 0 or n==k :
    print("0")
    exit()

if n > k:
    ans = ans%k



while(1):
    
    ans = abs(ans-k)
    if ans == n:
        break
    if ans == 1 or 0:
        break
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

dec = N - abs(N - K)

q, r = divmod(N, dec)

print(r)

Submission status: Wrong Answer

Solution code:
n, k = (int(i) for i in input().split())
if k > n:
    print(n, n-k)
elif n % k == 0:
    print(0)
else:
    q = n % k
    p = (k * (n//k+1)) - n
    print(min(p, q))

Submission status: Wrong Answer

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

if n<k :
  print(n)
elif n>=k:
  x = n//k
  ans = n-(x*k)
  ans_x = abs(n-(k*(x+1)))
  if ans >= ans_x:
    print(ans_x)
  else:
    print(ans)

#print(ans_x,ans)

Submission status: Wrong Answer

Solution code:
n, k= list(map(int, input().split()))
if k>n:
    print(n)
    exit()
elif n%k == 0:
    print("0")
    exit()
amari = n%k
n = amari
while(True):
    new_n = abs(k-n)
    if new_n < n:
        n = new_n
    else :
        break
print(n)

Submission status: Wrong Answer

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

while True:
    if n < k:
        r = n
        break
    n = n % k
    if n == 0:
        r = n
        break
    r = n - k
    if r < 0:
        break
    else:
        n = r
print(abs(r))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
(a, b)=list(map(int, input().split()))
if a < b or a== 0:
  print(a)
elif b == 1:
  print('0')
else:
  c = a % b
  if c == 0:
    print('0')  
  else:
    print(abs(b-c))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

while True:
    if ((n - k)>1000000):
        n = n//k
    else:
        break

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

print(n)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
t = int(n/k)
tt = t + 1
ans=min([n - t*k,tt*k - n])
print(str(ans))

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
if K == 1 or N == 0 or N == K:
    print(0)
    exit()
elif K == 0:
    print(N)
    exit()
elif N == 1:
    print(1)
    exit()
else:
    l = []
    ans = N
    for i in range(min(N,K)):
        ans = abs(ans - K)
        l.append(ans)
    print(min(l))

Submission status: Wrong Answer

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

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

for _ in range(10):
    N = min(abs(N-K), N)
print(N)

Submission status: Wrong Answer

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

if k == 1 or n == 0:
  print(0)
elif n < k :
  r1 = n
  r2 = n - k
  if r2 < 0:
    r2 *= -1
  print(min(r1,r2))
else:
  r1 = n // k
  r2 = r1 - k
  if r2 < 0:
    r2 *= -1
  print(min(r1,r2))

Submission status: Wrong Answer

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

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

if n > k:
    tmp = n%k
    print(min(tmp,abs(tmp-k)))
    exit()

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

if n == 0:
    print(0)


print(1)

Submission status: Wrong Answer

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

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

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

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

    print(min)
    return


culc(N, K)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n, k = map(int, input().split())
if n == k:
    print(0)
elif n < k:
    print(n)
elif n > k:
    ans = n % k
    while (True):
        if ans < abs(ans - k):
            print(ans)
            exit()
        ans = abs(ans - k)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
ZET=N
b=100000000000000000
c=N
b1=N
for i in range(1000000000000000000):
  c=abs(b1-K)
  if b<c:
    break
  b1=c
  b=c
print(b1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
from operator import itemgetter
import math
# stdin = open("sample.txt")

N,K = [int(x) for x in stdin.readline().rstrip().split()]
ans1 = N % K
ans2 = (ans1 - K) * -1
print(ans1,ans2)
print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:
s = input().split(" ")
N = int(s[0])
K = int(s[1])

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

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(n, a, b))

Submission status: Wrong Answer

Solution code:
import math

data = input().split()

n = int(data[0])
k = int(data[1])

div = math.floor(n/k)
n2 = n - div*k
if div==0:
	n2 += k

ans = []
for i in range(4):
	ans.append(abs(n2-k))
	n2 = abs(n2-k)

print(min(ans))

Submission status: Wrong Answer

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

if N>K:
  y = int(N/K)
  x = N - y*K
else:
  x = N 

cnt = 0
ans = x
while cnt<10:
  x_next = abs(x-K)
  ans = min(ans, x_next)
  x = x_next
  cnt += 1

print(ans)

Submission status: Wrong Answer

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

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 % 2 == 0 and k % 2 == 0:
      print(0)
      exit()
    if n % k == 0:
      print(0)
      exit()
    print(1)
  else:
    one = abs(n-k)
    two = abs(one-k)
    print(min(one,two))
  
import math
import fractions
import collections
import itertools
from functools import reduce
main()

Submission status: Wrong Answer

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

if n==k:
  print(0)

if k > n:
  if abs(k-n)<=n:
    print(abs(k-n))
  else:
    print(n)
  
else:
  amari = n%k
  com3 = k -amari
  if amari<= com3:
    print(amari)
  else:
    print(com3)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
if K == 1 or N == 0 or N == K:
    print(0)
    exit()
elif K == 0:
    print(N)
    exit()
elif N == 1:
    print(1)
    exit()
else:
    l = []
    ans = N
    for i in range(max(N,K)):
        ans = abs(ans - K)
        l.append(ans)
        if i >= 1:
            if ans in l:
                print(min(l))
                exit()
    print(min(l))

Submission status: Wrong Answer

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

if N >= K:
  print(N % K)
else:
  keisan = abs(N - K)
  if keisan > N:
    print(N)
  else:
    print(keisan)

Submission status: Wrong Answer

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

if N==0:
    print(0)

N=min(N%K,K-N%K)

print(N)

Submission status: Wrong Answer

Solution code:
NS = [int(_) for _ in input().split()]

N = NS[0]
K = NS[1]

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

def solve(N, K):
    b = N % K
    if b != 0:
        b = rep(N%K, K)
    return b

print(solve(N, K))

Submission status: Wrong Answer

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

var = N % K
if var == 0:
        print(0)
elif var == K-1 or var == 1:
        print(1)
else:
        ans = N
        while ans != N:
                ans = N
                N = N // K
        print(ans)

Submission status: Wrong Answer

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

def func(x,K):
    tmp=x-K
    if tmp<0:
        tmp*=(-1)
    return tmp

# def pow(x, n):
#   ans = 1
#   while(n > 0):
#     if(bin(n & 1) == bin(1)):
#       ans = ans*x
#     x = x*x
#     n = n >> 1 #ビットシフト
#   return ans
#
# pow(3,100) #3の100乗

if (N>K):
    y=int(N/K)
    N=func(N,K*y)

ans = N
while(True):
    N=func(N,K)
    if N<ans:
        ans=N
    else:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    while True:
        if n % k == 0:
            n = 0
            break
        else:
            if n // k == 0:
                n = n % k
                if n > abs(n - k):
                    n = abs(n - k)
                break
            else:
                n = n // k
    print(n)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

    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 =  map(int,input().split())
T = N%K
if T != 3:
    print(N%K)
else:
    L = [T,abs(T-K)]
    print(min(L))

Submission status: Wrong Answer

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

a = int(N / K)

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

b = abs(int(N - K * a))
c = abs(int(b - K))

print(min(b,c))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

cnt = int(N / K) + 1

print(min(abs(N - (K * cnt)), abs(N - (K * (cnt-1))), N))

Submission status: Wrong Answer

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

def gcd(a,b):
  a, b = max(a, b), min(a, b)
  while b!=0:
    a,b=b,a%b
  return a

if N < K:
  print(N)
elif N == K:
  print(0)
else:
  ans = gcd(N, K)
  if ans == K:
    print(0)
  else:
    print(ans)

Submission status: Wrong Answer

Solution code:
s = input().split(" ")
N = int(s[0])
K = int(s[1])

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if K == 1:
  print(0)
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 K == 1 or N == 0 or N == K:
    print(0)
    exit()
elif K == 0:
    print(N)
    exit()
elif N == 1:
    print(1)
    exit()
else:
    l = []
    ans = N
    for i in range(min(N,K)):
        ans = abs(ans - K)
        l.append(ans)
        if i >= 1:
            if ans in l:
                print(min(l))
                exit()
    print(min(l))

Submission status: Wrong Answer

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

if n < k:
    if n > k / 2:
        print(k - n)
    else:
        print(n)
else:
    s = math.ceil(n / k)
    ans = min(abs(n - s * k), n - (s - 1) * k)
    print(ans)

Submission status: Wrong Answer

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

while True:
    if ((n - k)>1000):
        n = n//k
    else:
        break

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

print(n)