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:
i = list(map(int, input().split()))
N = i[0]
K = i[1]

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

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
if 0 <= N <= 10^18 and 1 <= K <= 10^18:
    if N>K:
        N = abs(N-K)
        N = abs(N-K)
        print(N)
    else:
        print(N)
else:
    print("0")

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

tmp = n
for i in range(int(n**0.5)+1, 1, -1):
    tmp = min(abs(n-i*k), tmp)
    if abs(n-(i-1)*k) > tmp:
      break
print(tmp)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
ans = n - k
if k == 1 or n == 1:
    print(1)
    quit()
if n % k == 0:
    print(n//k)
    quit()
if n < k:
    print(n)
    quit()

print(min(ans, k - n%k))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
b = n / 2
a = abs(n - k)
if n == 0:
    print(n)
elif b >= k:
    print(n % k)
else:
    if n <= a:
        print(n)
    else:
        while n > a:
            n = a
            a = abs(a - k)
            if n < a:
                break
        print(n)

Submission status: Wrong Answer

Solution code:
import math

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

ans = 0
if N <= K:
    ans = min(N, abs(N - K))
else:
    xx = N / K
    c = math.ceil(xx) * K
    f = math.floor(xx) * K
    ans = min(abs(N - c), abs(N - f))

print(ans)

Submission status: Wrong Answer

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

b = math.floor(n/k)

l = [b-1,b,b+1]

print(min(abs(n-(l[0]*k)),abs(n-(l[1]*k)),abs(n-(l[2]*k))))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
while N>0:
    if N <K:
        if 2*N<=K:
            print(abs(N))
            break
        else:

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
# C
import math
def my_round(val, digit=0):
    p = 10 ** digit
    return (val * p * 2 + 1) // 2 / p

n, k = map(int, input().split())
s = my_round(n / k)
print(abs(n - int(s * k)))

Submission status: Wrong Answer

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

if n >= abs(n-k):
    ans = abs(n-k)
elif n < k :
    ans = n
elif n%k != 0 :
    ans = abs(n%k - k)
else :
    ans = 0

print(ans)

Submission status: Wrong Answer

Solution code:
i = list(map(int, input().split()))
N = i[0]
K = i[1]

for i in range(1000000):
    if i == 0:
        min = abs(N-K)
    min = abs(min-K)
    if (min-K) < 0:
        break
if min > 5:
    print('0')
elif (N%2) == 1 and (K%2) == 1:
    print('2')
elif (N%2) == 0 and (K%2) == 0:
    print('0')
elif (N%2) == 0 and (K%2) == 1:
    print('1')
elif (N%2) == 1 and (K%2) == 0:
    print('1')
elif K == 1:
    print('0')
else:
    print(min)

Submission status: Wrong Answer

Solution code:
# ABC161_C
from fractions import gcd
n,k=map(int,input().split())
# if k==1:
#     print(0)
#     exit()

t=n
# while n>abs(n-k):
# while t>0 and abs(t-k)>0:
while t>abs(t-k) and abs(t-k)>0:
    t=t%abs(t-k)
    if n==t:
        break
if t==k:
    t=0
print(t)
# ans=0
# ans=gcd(n,k)

# print(ans)
# ans=0
# # a=abs(n-k)
# ans=n%a
# print(ans)
# a=abs(n-k)
# # t=0
# while n>a:
#     n=n%a
# print(n)

Submission status: Wrong Answer

Solution code:
#import math
import fractions
N,K = map(int,input().split())
if (N==0 or K==1)<2:
    print (0)
else:
    print(fractions.gcd(N, K))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import random

N=random.randrange(10**18)
K=random.randrange(10**18)

while True:
    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())
n = n % k
if n == 0:
    print(0)
    exit()

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

while True:
    ans = n - k
    next_ans = abs(ans) - k
    if abs(next_ans) > abs(ans):
        print(abs(ans))
        break
    else:
        print(abs(next_ans))
        break

Submission status: Wrong Answer

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

if n <=k :
    print(min(n,abs(n-k)))
else:
    n = n-k*int(n/k)
    if n > abs(n-k):
        print(abs(n-k))
    else:
        print(n)

Submission status: Wrong Answer

Solution code:
N,K = map(float, raw_input().split())

d = long(N/K)

p = [N, abs(N - K*(d-1)), abs(N - K*d), abs(N - K*(d+1))]
print int(min(p))

Submission status: Wrong Answer

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

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

elif N<K:
    if N<=K/2:
        print(N)
    elif N>K/2:
        if -(N-K)<-(N-K-K):
            print(-(N-K))
        else:
            print(-(N-K-K))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
ans=[a]
for i in range(10000):
    ans.append(abs(ans[i]-b))
print(0 if b==1 else min(ans))

Submission status: Wrong Answer

Solution code:
def abs_result(x,y):
    tmp = abs(x-y)
    return tmp

def main():
    line = list(map(int,input().split()))
    n,k = line[0],line[1]
    
    # nを、nとkの差の絶対値で置き換える
    if n == 0 or k == 1:
        print(0)
        return
    
    if n % 2 == 0 and k % 2 == 0 and n < k:
        print(2)
    
    if n % 2 == 0 and k % 2 == 0 and n > k:
        print(0)
        
    if n % 2 == 1 and k % 2 == 0 and n > k:
        print(1)
        
    
    com = 2
    for i in range(0,10**18):
        ans = abs_result(n,k)
        if ans == 0:
            print(0)
            return
        elif ans == 1:
            print(1)
            return
        if ans <= com:
            print(ans)
            return
        n = ans

main()

Submission status: Wrong Answer

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

min=abs(N-K)
N=abs(min-K)
while(N<min):
    min=N

print(min)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
i1 = input()

list1 = [int(s) for s in i1.split()]

n = list1[0]
k = list1[1]


def do(x):
    return abs(x - k)

def next_search(last_index):
    if (last_index%2) == 1:
        # 奇数
        return (last_index-1) / 2
    else:
        return last_index /2

def truemin(value):
    list1 = []
    list1.append(value)
    list1.append(do(value))
    list1.append(value)
    list1.append(do(value))
    return int(min(list1))

past = do(n)

index = 1000000000000000000

for a in range(100):
    # print("index" + index.__str__())

    if index == 0.0:
        # print("indexがゼロ")
        print(truemin(n))
        break

    future = n - index*k
    if future < 0:
        index = next_search(index)
        # print("future:" + future.__str__())
    elif future < k:
        # print("ここで収束!")
        # print("future:" + future.__str__())
        print(truemin(future))
        break

Submission status: Wrong Answer

Solution code:
n, m = (int(x) for x in input().split())

a = n % m
b = n / m
if b < 1:
    print(n)
elif b == 1:
    print(0)
elif abs(a-m) > a:
    print(a)
else:
    print(abs(a-m))

Submission status: Wrong Answer

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

a = N - (N // K)*K

if N < K:
        print(N)
else:
        m = 1000000000000000000
        for i in range(100):
                a = abs(a - K)
                if a < m:
                        m = a 
        print(m)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if N > 1000000000000000*K:
    while x > K:
        x = abs(x - 1000000000000000*K)
        if x < K:
            break

while x > K:
    x = abs(x - K)
    if x < K:
        break
x = abs(x - K)        
print(x)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
ans=float('inf')
if n==k:
    print(0)
elif k == 1:
    print(0)
elif n < k :
    print(min(n,k-n))
elif n%2!=0 or k%2!=0:
    print(1)
else:
    print(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#coding: utf-8
A = list(map(int, input().split()))
N = A[0]
K = A[1]

n = 0
sw = 0
while True:
  if N%K == 0:
    N = 0
    break
  if N/K >1:
    C = int(N/K)
    N = N-C*K

  sw = abs(N-K)
  n += 1
  if N > sw:
    N = sw
  else:
    break
print(N)

Submission status: Wrong Answer

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

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

while True:
    ans = n - k
    next_ans = ans - k
    if abs(next_ans) > abs(ans):
        print(abs(ans))
        break
    else:
        print(abs(next_ans))
        break

Submission status: Wrong Answer

Solution code:
n, m = (int(x) for x in input().split())

a = n % m
b = n / m
if b < 1:
    print(n)
elif b == n or b == m:
    print(0)
elif abs(a-m) > a:
    print(a)
else:
    print(abs(a-m))

Submission status: Wrong Answer

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

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

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

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

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

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

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

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

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

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

def main():
  n,k = input_list()
  if n == k:
    print(0)
    exit()
  if k == 0:
    print(n)
    exit()
  if n > k:
    if n % k == 0:
      print(0)
      exit()
    if n % 2 == 0 and k % 2 == 0:
      print(n%k)
      exit()
    if n % 2 != 0 and k % 2 == 0:
      one = n % k
      two = abs(one - k)
      print(min(one,two))
      exit()
    if n % 2 == 0 and k % 2 != 0:
      one = n % k
      two = abs(one - k)
      print(min(one,two))
      exit()
    one = abs(n-k)
    two = abs(one-k)
    print(min(one,two))
    exit()
  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())
b = n/k
if b < 1:
    t = 0
else:
    t = int(b)
tt = t + 1
a = n - t*k
b = tt*k - n
ans=min([a,b])
print(str(ans))

Submission status: Wrong Answer

Solution code:
#import math
import fractions
N,K = map(int,input().split())
if (min(N,K))<2:
    print (0)
else:
    print(fractions.gcd(N, K))

Submission status: Wrong Answer

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

if n <=k :
    print(min(n,abs(n-k)))
else:
    n = n-k*int(n/k)
    print(min(n,abs(n-k)))

Submission status: Wrong Answer

Solution code:
# C
import math
def my_round(val, digit=0):
    p = 10 ** digit
    return (val * p * 2 + 1) // 2 / p

n, k = map(int, input().split())
s = my_round(n / k)
print(int(abs(n - (s * k))))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
b = n / 2
a = abs(n - k)
if b >= k:
    print(n % k)
else:
    if n <= a:
        print(n)
    else:
        while n > a:
            n = a
            a = abs(a - k)
            if n < a:
                break
        print(n)

Submission status: Wrong Answer

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

x = n
# 最初の差分だけは繰り返せるので余りを使って短縮
x1 = abs(x-k)
x2 = abs(x1-k)
# print("1:{} 2:{}".format(x1,x2))
if abs(n-x1) > 0:
  x %= abs(n-x1)
while True:
  # 今回のループでの結果
  x2 = abs(x-k)
#  print("abs({} - {}) -> {}".format(x,k,x2))
  if x2 >= x:
    # 差が大きくなったら終了
    break
  # 計算結果が小さくなっているうちは繰り返す
  x = x2

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

Submission status: Wrong Answer

Solution code:
i1 = input()

list1 = [int(s) for s in i1.split()]

n = list1[0]
k = list1[1]


def do(x):
    return abs(x - k)

def next_search(last_index):
    if (last_index%2) == 1:
        # 奇数
        return (last_index-1) / 2
    else:
        return last_index /2

def truemin(value):
    list1 = []
    list1.append(value)
    list1.append(do(value))
    list1.append(value)
    list1.append(do(value))
    return min(list1)

past = do(n)

index = 1000000000000000000

for a in range(100):
    # print("index" + index.__str__())

    if index == 0.0:
        # print("indexがゼロ")
        print(truemin(n))
        break

    future = n - index*k
    if future < 0:
        index = next_search(index)
        # print("future:" + future.__str__())
    elif future < k:
        # print("ここで収束!")
        # print("future:" + future.__str__())
        print(truemin(future))
        break

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
ans=float('inf')
if n==k:
    print(0)
elif k == 1:
    print(0)
elif n < k :
    print(min(n,k-n))
elif n%2==0 and k%2==0:
    print(0)
else:
    print(1)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=[]
for i in range(100000):
    N=abs(N-K)
    A.append(N)
print(min(A))

Submission status: Wrong Answer

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

if n < k :
    ans = n
elif n%k != 0 :
    ans = abs(n%k - k)
else :
    ans = 0

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
Nj = N
while (Nj == K) is False:
    Ni = Nj
    Nj = abs(Ni - K)
    if Nj > Ni:
        print(Ni)
        break
    else:
        continue
if (Nj == 0) or (Nj == K) or (K == 1):
    print(0)

Submission status: Wrong Answer

Solution code:
import sys
a,b=map(int,input().split())
i=1
z=[0]*2
saisyo=abs(a-b)
if a==0:
  print(a)
  sys.exit()
if a<b:
  if a<saisyo:
    saisyo=a
  print(saisyo)
  sys.exit()


  
u=a%b
w=(a-u)/b
z[0]=a-(w*b)
z[1]=-(a-(w+1)*b)
r=int(min(z))


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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
line = input().rstrip().split(" ")

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

if N == K or K == 0:
    print("0")
    c += 1
elif N % K == 0:
    print("0")
    c += 1

calc1 = N - K
# print(str(calc1))
if calc1 < 0:
    calc1 *= -1
rec += calc1
if calc1 > N:
    print(str(N))
    c += 1
    # print("終了")

if N > K:
    M = N % K
    N = M

while c == 0:
    calc = rec - K
    if calc < 0:
        calc *= -1
    # print(str(calc) + "結果")
    if rec <= calc:
        print(str(rec))
        # print("終了")
        c += 1
    rec = calc
    # print(str(rec))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
ans=float('inf')
if k == 1:
    print(0)
    exit()
elif n==k:
    print(0)
elif n < k :
    print(min(n,k-n))
elif n%2==0 and k%2==0:
    print(0)
else:
    print(1)

Submission status: Wrong Answer

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

  mod_min = 10**18

  if N >= K:
    N = N%K
  else:
    pass

  min_abs = 10*18

  while(True):
    tmp_abs = abs(N-K)
    if min_abs > tmp_abs:
      min_abs = tmp_abs
      N = min_abs
    else:
      break
  
  print(min_abs)

if __name__ == "__main__":
  main()

Submission status: Wrong Answer

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

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

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

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

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

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

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

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

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

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

def main():
  n,k = input_list()
  if n == k:
    print(0)
    exit()
  if k == 0:
    print(n)
    exit()
  if n > k:
    if n % k == 0:
      print(0)
      exit()
    if n % 2 == 0 and k % 2 == 0:
      if k > 2:
        print(n%k)
        exit()
      print(0)
      exit()
    if n % 2 != 0 and k % 2 == 0:
      one = abs(n-k)
      two = abs(one-k)
      print(min(one,two))
      exit()
    if n % 2 == 0 and k % 2 != 0:
      one = n % k
      two = abs(one - k)
      print(min(one,two))
      exit()
    one = abs(n-k)
    two = abs(one-k)
    print(min(one,two))
    exit()
  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<0:
  tmp = n*(-1)
else:
  tmp = n
for i in range(100000):
  tmp = tmp - k
  if (tmp<0):
    tmp = tmp*(-1)
if(k==1):
  print(0)
elif (k==2):
  print(1)
else:
  print(tmp)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
if n < k:
    print(n)
    exit(0)
li = []
for i in range(n):
    if k == 1:
        print(0)
        exit(0)
    elif k == 2:
        if n % 2 == 0:
            print(0)
            exit(0)
        else:
            print(1)
            exit(0)
    else:
        li.append(abs(n - k))
        n -= k
        if i == 0:
            continue
        if li[i] >= li[i - 1]:
            print(min(li))
            exit(0)

Submission status: Wrong Answer

Solution code:
import math

S =  list(map(int,input().split()))
N = S[0]
K = S[1]
    
if N < K:
    if N < abs(N-K):
        print(N)
    else:
        print(abs(N-K))
        
if N == K:
    print(0)
if N > K:
    a = math.floor(N/K)
    b = N-K*a
    ba = abs(b-K)

    if b >= ba:
        print(ba)
    else:
        print(b)

Submission status: Wrong Answer

Solution code:
def abstract(a, b):
    return abs(a - b)

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

A = N
min_temp = A

if N == 0:
    print(0)
elif N == 1 or K == 1:
    print(K)
elif N == 2 or K == 2:
    print(2)
else:
    while True:
        min_temp = abstract(A, K)
        if A < min_temp:
            break
        elif A == min_temp:
            break
        A = min_temp
    print(A)

Submission status: Wrong Answer

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

if n%k < n and abs(n%k-k) < n%k:
    print(int(abs(n%k-k)))
elif n%k < n and abs(n%k-k) > n%k:
    print(int(n%k))
else:
    print(int(n))

Submission status: Wrong Answer

Solution code:
import fractions
n, k = map(int, input().split())
#
# if k == 1 or n == 0 or n == k:
#     print('0')

if (n > abs(n - k)):
    n = fractions.gcd(n, 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())
 

b = math.floor(n/k)

min = abs(n-(b*k))
for i in range(10**5):
    n = abs(n-k)
    if min > n:
        min = n

print(min)

Submission status: Wrong Answer

Solution code:
import math
N,K= list(map(int,input().split()))
a = int(N/K)
a1 = a*K
a2 = a1+K
if N % K ==0:
    Num = 0
else:
    if abs(N-a1)>abs(N-a2):
        Num = abs(N-a2)
    else:
        Num = abs(N-a1)
    
print(Num)

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))
results = set([n])
counter = 0
switch = False

while counter < 50:
    # print(n)

    if n == 0 or k == 0 or n == k:
        print(min(n, k))
        switch = True
        break

    # test
    if n % k == 0:
        print('0')
        switch = True
        break


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

    # detect loop
    if n in results:
        print(min(results))
        switch = True
        break
    else:
        results.add(n)

    counter += 1


if not switch:
    # print('loop ended n:', n)
    results = list(results)
    print(min(min(results), min(results) % k))

Submission status: Wrong Answer

Solution code:
n, m = (int(x) for x in input().split())

a = n % m
b = n / m
if b < 1:
    print(n)
elif b == n:
    print(0)
elif abs(a-m) > a:
    print(a)
else:
    print(abs(a-m))

Submission status: Wrong Answer

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

x = N
pre = N 
ans = 0 
while True :
    print("x = %d - %d" % (x , K))
    x = abs(x - K)
    if abs(pre - K) > abs(x -K) :
        ans = abs(x - K)
        if abs(x - K) < abs(abs(x -K) -K) :
            break

print(ans)

Submission status: Wrong Answer

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

if N < K:
        print(N)
else:
        m = 1000000000000000000
        for i in range(100):
                if a > abs(a - K): 
                        m = abs(a - K)
                a = abs(a - K)
        print(m)

Submission status: Wrong Answer

Solution code:
import fractions
import sys
N,K = map(int,input().split())
L = []
L.append(N)
i = 0
if N == K:
    print(0)
    sys.exit(0)
if N == 0:
    print(0)
    sys.exit(0)
if K == 1:
    print(0)
    sys.exit(0)
Q = 0
N//=K
while True:
  L.append(abs(N-K))
  N = abs(N-K)
  i += 1
  if i > 10**5:
    Q = min(L)
    break
if N%K == 0 and N>=K:
    print(0)
else:
    if N < K:
        print(min(N,Q))
    else:
        print(min(N%K,Q,fractions.gcd(N,K)))

Submission status: Wrong Answer

Solution code:
import math
nk = list(map(int, input().split()))

c = math.ceil(nk[0]/nk[1])
x = nk[0] - nk[1]*c
p = x+nk[1]
print(abs(x) if abs(x) < p else p)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


if N == 0:
    print(N)

initialK = K


while True:
    if N <= initialK/2:
        print(N)
        break
    K = N%K
    if K <= initialK/2:
        print(K)
        break

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
N = n
if n % k == 0:
    print(0)
elif n > 100000000000:
    n -= 1000 *k
    while n > 0:
        n -= k
    if abs(n) > N:
        print(N)
    else:
        print(abs(n))

Submission status: Wrong Answer

Solution code:
import sys
import math

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

if N < K:
    print(N)
elif N == 0:
    print(0)
elif N == K:
    print(0)
else:
    a = math.floor(N / K)

    ans1 = abs(N - (K * a))
    ans2 = abs(N - (K * (a + 1)))
    ans3 = abs(N - K)
    ans4 = abs(N - (K * (a - 1)))

    print(min(ans1, ans2, ans3, ans4))

Submission status: Wrong Answer

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

if n == 0:
    output = m    
elif n % m  == 0:
    output = 0
elif n >= m:
    if n/2 > m:
        output = n%m   
    else:
        output = n%(n-m)      
else:
    if n < m/2:
        output = n      
    else:
        output = m-n
                          
print(output)

Submission status: Wrong Answer

Solution code:
import numpy as np
import fractions
N, K = [int(i) for i in input().split(' ')]

if N == 0:
	result = 0
elif K == 0:
	result = N
elif N % K == 0:
	result = 0
elif K % N == 0:
	result = K/N if N > K/N else N
elif fractions.gcd(N, K) == 1 and N > K:
	result = 1
elif fractions.gcd(N, K) == 1 and K > N:
	result = K-N if N > K - N else N
elif N > K:
  result = N % K if N % K < K - N % K else K-N%K
elif N < K:
  result = K-N if N > K - N else N
  
print("{}".format(result))

#lis = np.array([int(n) for n in input().split(' ')])
#print("{} {}".format(sum(lis) / 4 / int(M), lis[int(M)-1]))

#count = sum(sum(lis) / 4.0 / int(M) <= lis)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
nk = list(map(int, input().split()))

c = math.ceil(nk[0]/nk[1])
x = nk[0] - nk[1]*c
p = x+nk[1]
print(str(c)+','+str(x)+','+str(p))
print(abs(x) if abs(x) < p else p)

Submission status: Wrong Answer

Solution code:
array = list(map(int, input().split()))
tmp = array[0] % array[1]
print(tmp)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
ans=float('inf')
if k == 1:
    print(0)
    exit()
elif n < k :
    print(min(n,k-n))
elif n%2!=0 or k%2!=0:
    print(1)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))
results = set([n])
counter = 0
switch = False

while counter < 50:
    # print(n)
    
    if n == 0 or k == 0 or n == k:
        print(n)
        switch = True
        break

    # test
    if n % k == 0:
        print('0')
        switch = True
        break


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

    # detect loop
    if n in results:
        print(min(results))
        switch = True
        break
    else:
        results.add(n)

    counter += 1


if not switch:
    # print('loop ended n:', n)
    results = list(results)
    print(min(min(results), min(results) % k))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
ans = int(N)
N_1 = N
N_2 = N
k_range = N // K
# print(k_range)

if k_range == 0:
    N = N - K
else:
    N_1 = N - (K * k_range)
    N_2 = N - (K * (k_range + 1))

if abs(N_1) < abs(N_2):
    if ans > abs(N_1):
        ans = abs(N_1)
    else:
        pass
else:
    if ans > abs(N_2):
        ans = abs(N_2)
    else:
        pass
print(ans)

Submission status: Wrong Answer

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

if k == 1 or n == 0 or n == k:
    print('0')
else:
    if(n > abs(n - k)):
        n = fractions.gcd(n, k)
    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 :
    ans = abs(n%k - k)
else :
    ans = 0

print(ans)