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: Time Limit Exceeded

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

while (not M > mini):
    mini = min(mini, M)
    M = abs(M - K)
print(mini)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

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

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

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

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

    print(min)
    return


culc(N, K)

Submission status: Time Limit Exceeded

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

a = abs(N-K)

b = abs(a-K)

while a > b:
    a = b
    b = abs(b-K)

print(a)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

  if n <= k and n <= nk_abs:
    break
  if k == 1:
    n = 0
    break
  n = nk_abs

print(n)

Submission status: Time Limit Exceeded

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

if K == 1:
    print(0)
    sys.exit()
elif K == 2:
    if N % 2 == 0:
        print(0)
    else:
        print(1)
else:
    while N > 0:
        N = N - K

second = N + K

N = abs(N)

list_a.append(N)
list_a.append(second)
list_a.append(first)

list_a = sorted(list_a)

print(list_a[0])

Submission status: Time Limit Exceeded

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

print(num)

Submission status: Time Limit Exceeded

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

min = -1
x = n
while True:
    if n % m  == 0:
        min = 0
        break

    x = abs(x-m)

    
    if min == -1:
        min = abs(n - m)
    elif x >= min:
        break

    min = x


print(min)

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
# a=sorted(a)
c=abs(n-m)
if m==1:
   c=0
else:
    while True:
        n=abs(n-m)
        if n<=c:
            c=n
        else:
            break
print(c)

Submission status: Time Limit Exceeded

Solution code:
a,b=input().split()
a=int(a)
b=int(b)
y=[10**20]
f=0
for i in range (10**18):
    x=b-a
    if x==0:
        y.append(0)
        f=0
        break
    elif x<0:
        y.append(x*(-1))
        a=x*(-1)
    else :
        y.append(x)
        a=x
    if int(y[i]) <= int(y[i+1]):
        f=y[i]
        break

print(f)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

x = N
while x > abs(x-K):
    x = abs(x-K)

print(x)

Submission status: Time Limit Exceeded

Solution code:
n, k = list(map(int, input().split()))
global_min = None
counter = 0

while True:
    n = min(n, abs(n - k))

    if n == 0:
        print(n)
        break

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

    if (not global_min) or (n < global_min):
        # update value iteratively
        global_min = n
        counter += 1
    elif n == global_min:
        # detect loop
        print(n)
        break

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N, K= (map(int, input().split()))
number_list = [N]
while True:
    a = abs(N-K)
    if a in number_list:
        break
    else:    
        number_list.append(a)
        N = a
print(min(number_list))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

var = N % K
if var == 0:
        print(0)
elif var == K-1 or var == 1:
        print(1)
else:
        while True:
                prev = N
                N = abs(N-K)
                if prev < N:
                        break
        print(prev)

Submission status: Time Limit Exceeded

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

if K == 1:
    print(0)
    sys.exit()
else:
    while N > 0:
        N = N - K

second = N + K

N = abs(N)

list_a.append(N)
list_a.append(second)
list_a.append(first)

list_a = sorted(list_a)

print(list_a[0])

Submission status: Time Limit Exceeded

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

while True:
    M = N-K
    if M<0:
        M=-M
    if M < N:
        N = M
    else:break

print(N)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
for p in range(K):
  if (N - p) % K == 0 or (N + p) % K == 0:
    print(p)
    break

Submission status: Time Limit Exceeded

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

flag=N

if N/2>K and N%K==0:
    print(0)
else:
    while flag>0:
        flag=flag-K

    if flag<0 and -flag<N:
        print(-flag)
    elif flag<0 and -flag>N:
        print(N)
    elif flag==0:
        print(0)

Submission status: Time Limit Exceeded

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

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

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

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

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

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

    print(min)
    return


culc(N, K)

Submission status: Time Limit Exceeded

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

while True:
    if n % k == 0:
        print(0)
        break
    ans = n - k
    if ans < 0:
        hold = ans * -1
        next_ans = -1 * (hold - k)
        if hold < next_ans:
            print(hold)
        else:
            print(next_ans)
        break

    n = ans

Submission status: Time Limit Exceeded

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

a = abs(N-K)

b = abs(a-K)

while a >= b:
    a = b
    b = abs(b-K)

print(a)

Submission status: Time Limit Exceeded

Solution code:
k = input().split()
s = int(k[0])
j = int(k[1])
v = s + 1
while v > s:
    v = s
    s = abs(s - j)

v = s
s = abs(s-j)
if v > s:
  print(s)
else:
  print(v)

Submission status: Time Limit Exceeded

Solution code:
def main():
  #n = int(input())
  #s = input()
  #s = input().split()
  n,k = list(map(int,input().split()))
  #a = list(map(int,input().split()))
  #a = [input() for i in range(n)]
  #a = [int(input()) for i in range(n)]
  #a = [input().split() for i in range(n)]
  #a = [list(map(int,input().split())) for i in range(n)]
  ans = n
  zen = n
  count = 0
  lis = []
  
  if k == 1:
    print(0)
  elif k == 2:
    if n % 2 == 0:
      print(2)
    else:
      print(3)
  else:  
    while 1:
      hi = abs(ans-k)
      if hi < zen:
        zen = hi
        ans = hi
        
      else:
        print(ans)
        exit()
      
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
list=list(map(int,input().split()))
x=list[0]
k=list[1]
y=abs(x-k)
while y>=k:
  y=abs(y-k)
z=k-y
if z >=y:
  print(y)
else:
  print(z)

Submission status: Time Limit Exceeded

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

min = abs(n - m)
x = n
while True:
    if n % m  == 0:
        min = 0
        break

    x = abs(x-m)

    if x > min:
        break

    min = x


print(min)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

r = []
if n % k == 0:
    print(0)
    exit()
while True:
    n = abs(n - k)
    r.append(n)
    if r.count(n) >= 2:
        break

print(min(r))

Submission status: Time Limit Exceeded

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

if K==1:
    N=0

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

print(N)

Submission status: Time Limit Exceeded

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

N = int(N)
K = int(K)

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

Submission status: Time Limit Exceeded

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

var = N % K
if var == 0:
        print(0)
elif var == K-1 or var == 1:
        print(1)
else:
        while True:
                prev = N
                N = abs(N-K)
                if prev == N:
                        break
        print(N)

Submission status: Time Limit Exceeded

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

k = abs(K)

while 1:
  j = abs(N - k)

  if N < j:
    break
  N = j

print(N)

Submission status: Time Limit Exceeded

Solution code:
def min(a, b):
    if a >= b:
        res = a - b
    elif a < b:
        res = b - a
    return res

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

min_num = float('inf')
res_list = []
while(1):
    res = min(n, k)
    if res < min_num:
        min_num = res
    if res == 0:
        break
    if res in res_list:
        break
    n = res
    res_list.append(res)

print(min_num)

Submission status: Time Limit Exceeded

Solution code:
cell=list(map(int,input().split()))
n=cell[0]
k=cell[1]
pren=n
while 2>0:
    n=abs(n-k)
    if n >pren:
        break
    else:
        pren=n


print(pren)

Submission status: Time Limit Exceeded

Solution code:
a,b=input().split()
a=int(a)
b=int(b)
y=[10**18]
f=0
for i in range (10**18):
    x=b-a
    if x==0:
        y.append(0)
        f=0
        break
    elif x<0:
        y.append(x*(-1))
        a=x*(-1)
    else :
        y.append(x)
        a=x
    if int(y[i]) <= int(y[i+1]):
        f=y[i]
        break

print(f)

Submission status: Time Limit Exceeded

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

V = False
num = 0

while V == False:
  num = abs(N - K)
  if N == num:
    V == True
  else:
    N = num
    
print(num)

Submission status: Time Limit Exceeded

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

def absolute(n,k):
    return abs(n-k)

x = absolute(n, k)
if n % k == 0:
    print(0)
elif n < x:
    print(n)
else:
    while absolute(x, k) < x:
        x = absolute(x, k)

    print(x)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
answer = 0
if k == 1:
  answer = 0
elif k >= n:
  gap = k-n
  if gap > n:
    answer = n
  else:
    answer = gap
else:
  while n > k:
    n = n - k
  if k >= n:
    gap = k-n
    if gap > n:
      answer = n
    else:
      answer = gap
print(answer)

Submission status: Time Limit Exceeded

Solution code:
import sys

N,K = map(int, sys.stdin.readline().strip().split())

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

d = []

d.append(N)
while True:
    d.append(abs(d[-1] - K))
    if d[-1] > d[-2]:
        print(d[-2])
        sys.exit()

Submission status: Time Limit Exceeded

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

while True:
    ans = n - k
    if ans < 0:
        hold = ans * -1
        next_ans = -1 * (hold - k)
        if hold < next_ans:
            print(hold)
        else:
            print(next_ans)
        break

    n = ans

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(4100000)

def main():
  n,k = map(int, input().split())
  
  if k == 1:
    return 0
  
  result = check(n, k, [n])
    
  return result
 
def check(val1, val2, checked):
  tmp = val1 - val2 if val1 - val2 > 0 else val2 -val1
  if tmp == 0:
    return 0
  if tmp in checked:
    return min(checked)
  checked.append(tmp)
  return check(tmp, val2, checked)
  
print(main())

Submission status: Time Limit Exceeded

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

prev_num = n
while True:
  num = abs(prev_num - k)
  if num > prev_num:
    print(prev_num)
    break
  else:
    prev_num = num

Submission status: Time Limit Exceeded

Solution code:
def main():
  #n = int(input())
  #s = input()
  #s = input().split()
  n,k = list(map(int,input().split()))
  #a = list(map(int,input().split()))
  #a = [input() for i in range(n)]
  #a = [int(input()) for i in range(n)]
  #a = [input().split() for i in range(n)]
  #a = [list(map(int,input().split())) for i in range(n)]
  ans = n
  zen = n
  count = 0
  lis = []
  
  if k == 1:
    print(0)
  else:  
    while 1:
      hi = abs(ans-k)
      if hi < zen:
        zen = hi
        ans = hi
        
      else:
        print(ans)
        exit()
      
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

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)


while True:
    ab = resZ(N, K)
    # print(ab)
    if N < ab:
        print(N)
        # print('end')
        sys.exit()

    N = ab
# def

Submission status: Time Limit Exceeded

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

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

seen = [N]
x = N
while True:
  x = abs(x-K)
  if x == 1:
    print(1)
    sys.exit()
  if x in seen:
    print(min(seen))
    sys.exit()
  else:
    seen.append(x)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a = []
if n % k == 0:
  print(0)
else :
  while n > 0 :
    n -= k
    a.append(n)
    
  if len(a) == 1 : 
    if a[-1] * -1 > n + k :
      print(n + k)
    else :
      print(a[-1] * -1)

  else :
    if (a[-1]) * -1 > a[-2] :
      print(a[-2])
    else :
      print((a[-1]) * -1)

Submission status: Time Limit Exceeded

Solution code:
import sys


n,k= map(int,input().split())
v={}

min = sys.maxsize

if n == 0 :print(0);exit()
if n >= 10**18 :print(0);exit()

while True:
    diff = abs(n- k)

    if v.get(diff):
        break
    else:
        v[diff] = True
        n = diff

        if diff < min:
            min = diff



print(min)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
init = [int(s) for s in input().rstrip().split(' ')]

if init[1] == 1:
    print(0)
elif init[0] % init[1] == 0 and init[0] > init[1]:
    print(0)
elif init[0] == init[1]:
    print(0)
else:
    count = 0
    tmp = init[0]
    minX = tmp + 10
    preMinX = tmp + 10
    while count < 30:
        tmp = abs(tmp - init[1])
        minX = min(minX,tmp)

        if minX == 0 or preMinX == tmp:
            print(tmp)
            break
        else:
            preMinX = minX

    if count > 30:
        print(tmp)

Submission status: Time Limit Exceeded

Solution code:
import math

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

while True:
    if abs(N-K) < N:
        N = abs(N-K)
        continue
    else:break

print(N)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N,K = list(map(int, input().split()))
while True:
  temp_N=N-K
  if temp_N>0:
    N=np.abs(temp_N)
  else:
    break
if np.abs(temp_N)>N:
  print(N)
else:
  print(np.abs(temp_N))

Submission status: Time Limit Exceeded

Solution code:
def calc(n,k):
    p=[n]
    d=abs(n-k)
    while(d not in p):

        p.append(d)
        d = abs(d - k)
    return min(p)
n,k=input().split()
n=int(n)
k=int(k)
print(calc(n,k))

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
# a=sorted(a)
c=abs(n-m)
while True:
    n=abs(n-m)
    if n<=c:
        c=n
    else:
        break
print(c)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
def func(x):
    return abs(x-k)
ans = n
while min(func(n), n) != n:
    ans = func(n)
    n = func(n)
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

#while n > abs(n-k):
#    n = abs(n-k)
while 2*n > k:
    
    if n > k:
        n = n-k
    elif n <= k:
        n = k-n
        
print(n)

Submission status: Time Limit Exceeded

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

def getValue(initial, diff):
  initial = abs(initial - diff)
  return initial

tempValue = n

if k > 1:
  while(tempValue > getValue(tempValue, k)):
    tempValue = getValue(tempValue, k)
else:
  tempValue = 0

print(tempValue)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
new_N = N
result_list = [N]
for _ in range(N):
    new_N = abs(new_N - K)
    result_list.append(new_N)

print(min(result_list))

Submission status: Time Limit Exceeded

Solution code:
def main():
  n,k = map(int, input().split())
  
  if k == 1:
    return 0
  
  checked = [n]
  
  while True:
    tmp = difference(checked[-1], k)
    if tmp in checked:
      break
     
    checked.append(tmp)
    
  return min(checked)
 
def difference(val1, val2):
  tmp = val1 - val2
  return tmp if tmp >= 0 else -(tmp)
  
print(main())

Submission status: Time Limit Exceeded

Solution code:
x,n = list(map(int,input().split()))
while x >= n:
    x -= n
print(min(x,n-x))

Submission status: Time Limit Exceeded

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

N = int(N)
K = int(K)
prevN = N

while True:
    prevN = N
    N = abs(prevN - K)
    if prevN < N:
        break
print(prevN)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
arr = []
while True:
    c = abs(N-K)
    arr.append(c)
    a = set(arr)
    if len(arr) > len(a):
        break
    N = c
print(min(arr))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:

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

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

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

Submission status: Time Limit Exceeded

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

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

print(min)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=n
while True:
    
    if (n-k)>0:
        n=n-k
        a=n
    else:
        b=n-k
        break
   
if abs(a)>abs(b):
    print(str(abs(b)))
else:
    print(str(abs(a)))

Submission status: Time Limit Exceeded

Solution code:
cell=list(map(int,input().split()))
n=cell[0]
pren=n
while 2>0:
    n=abs(n-cell[1])
    if n >pren:
        break
    else:
        pren=n


print(pren)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

n = inp[0]
k = inp[1]

if n % k == 0:
    print(0)
else :
    i = 0
    while(1):
        if n - (i * k) < 0:
            break
        else :
            i += 1

    if (i * k) - n > n - ((i - 1) * k):
        print(n - ((i - 1) * k))
    else :
        print((i * k) - n)

Submission status: Time Limit Exceeded

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

while True:
  if k <= n:
    n -= k
  elif n > k//2:
    n = k - n
  else:
    break

    
print(n)

Submission status: Time Limit Exceeded

Solution code:
s=input().split()

N=int(s[0])
K=int(s[1])

if N > K/2 and N < K:
    print(abs(N-K))

if N <= K/2:
    print(N)

if N==K:
    print(0)

while N > 1000*K:
    N=N-1000*K
    if N <=1000*K:
        break

while N > 100*K:
    N=N-100*K
    if N <=100*K:
        break

if N > K:
    while N > K:
        N=N-K
        if N <= K:
            break

    if N > K/2 and N < K:
        print(abs(N-K))

    if N <= K/2:
        print(N)

    if N==K:
        print(0)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N,K=(int(i) for i in input().split())
while N>0:
    N=N-K
M=N+K
print(min(abs(N),M))

Submission status: Time Limit Exceeded

Solution code:
def replaceInt(N, M):
    N = abs(N-M)
    return N
    
N, M = [int(i) for i in input().split()]

if N == M :
    output = 0
elif N < M :
    if N <= M / 2:
        output = N
    else:
        output = abs(M-N)
else :
    if M == 1:
        output = 0
    else :
        output_candidates = [N]
        while True:
            N = replaceInt(N,M)
            if N in output_candidates:
                break
            output_candidates.append(N)
        output = min(output_candidates)
        
print(output)

Submission status: Time Limit Exceeded

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

judge = 0

if n%k==0:
    judge = 1
    print(0)
if judge == 0:
    while n>= 0:
        n -= k

    if abs(n+k)<abs(n):
        print(abs(n+k))
    else:
        print(abs(n))

Submission status: Time Limit Exceeded

Solution code:
import sys


n,k= map(int,input().split())
v={}

min = sys.maxsize

if n == 0 :print(0);exit()
if n >= 10**18 :print(0);exit()


while True:
    diff = abs(n- k)

    if v.get(diff):
        break
    else:
        v[diff] = True
        n = diff

        if diff < min:
            min = diff



print(min)

Submission status: Time Limit Exceeded

Solution code:
data = input().split()
N = int(data[0])
K = int(data[1])
result = [N]
while True:
  num = abs(N-K)
  result.append(num)
  if num >= N:
    break
  N = num
answer = min(result)
print(answer)

Submission status: Time Limit Exceeded

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

while True:
    a=n
    if (n-k)>0:
        n=n-k
        a=n
    else:
        b=n-k
        break
   
if abs(a)>abs(b):
    print(str(abs(b)))
else:
    print(str(abs(a)))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int,input().split())
count = 0
while(True):
  nk_abs = abs(n - k)

  if min(n, k, nk_abs) == n:
    break
  if k == 1:
    n = 0
    break
  n = nk_abs
  count += 1

print(n)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
N, K = map(int, input().split())
if K == 1:
  print(0)
  sys.exit()
  
a = abs(N-K)
while abs(a - K) < a:
  a = abs(a-K)
  if a == N:
    print(a)
    sys.exit()
  
print(a)

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int, input().split())
stopf = True
def exchangeK():
    global stopf
    global N
    if N > abs(N-K):
        N = abs(N-K)
    else:
        stopf = False
while stopf:
    exchangeK()
print(N)

Submission status: Time Limit Exceeded

Solution code:
def min(a, b):
    if a >= b:
        res = a - b
    elif a < b:
        res = b - a
    return res

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

min_num = float('inf')
res_list = []
while(1):
    res = min(n, k)
    if res < min_num:
        min_num = res
    if res == 0:
        break
    if res in res_list:
        break
    n = res
    res_list.append(res)

print(min_num)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

while n > k:
    n = n - k

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

print(min(n,k-n))

Submission status: Time Limit Exceeded

Solution code:
s=input().split()

N=int(s[0])
K=int(s[1])

if N > K/2 and N < K:
    print(abs(N-K))

if N <= K/2:
    print(N)

if N==K:
    print(0)

while N > 100*K:
    N=N-100*K
    if N <=100*K:
        break

if N > K:
    while N > K:
        N=N-K
        if N <= K:
            break

    if N > K/2 and N < K:
        print(abs(N-K))

    if N <= K/2:
        print(N)

    if N==K:
        print(0)

Submission status: Time Limit Exceeded

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: Time Limit Exceeded

Solution code:
a,b=input().split()
a=int(a)
b=int(b)
y=[10**18]
f=0
for i in range (10**16):
    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: Time Limit Exceeded

Solution code:
n = list(map(int,input().split()))
a = n[0]
b = n[1]
while 1>0:
  c=a
  if a>b:
    a=a-b
  else:
    a=b-a
  if c<=a:
    break
print(c)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input('').split(' '))
s=0
h=0

while s==0:
    h=n
    n=n-k
    if n<0:
        n=n*(-1)
    if h>n:
        s=0
    else:
        s=1
        n=h

print(n)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
N, K = map(int, input().split())
if K == 1:
  print(0)
  sys.exit()
  
min = N
a = abs(N-K)
while abs(a - K) < a:
  a = abs(a-K)
  
print(a)

Submission status: Time Limit Exceeded

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

a = []

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

Submission status: Time Limit Exceeded

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

def absolute(n,k):
    return abs(n-k)

x = absolute(n, k)
if n < x:
    print(n)
else:
    while absolute(x, k) < x:
        x = absolute(x, k)

    print(x)

Submission status: Time Limit Exceeded

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

flag = True

min = N

if K == 1:
    ans = 0
elif N % K == 0:
    ans = 0
elif abs(N - K) > N:
    ans = N
else:
    while(flag):
        if abs(min - K) < min:
            min = abs(min - K)
        else:
            flag = False
            ans = min

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math
N,K= list(map(int,input().split()))
Num = 10*18
while Num > 0:
    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: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# 入力
n, k = map(int, input().split())

# 出力
if k == 1:
  print(0)
  exit()
  
min = n

while(1):
  n = abs(n-k)
  if min > n:
    min = n
  else:
    break

print(min)

Submission status: Time Limit Exceeded

Solution code:
a,b=input().split()
a=int(a)
b=int(b)
y=[10**18]
f=0
for i in range (10**16):
    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 y[i]<=y[i+1]:
        f=y[i]
        break

print(f)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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


if __name__ == '__main__':
    main()