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:
def solve(n, k):

    if k == 1:
        return 0
    prev = set()
    tmp_val = n
    while True:
        tmp_val = abs(tmp_val - k)
        if tmp_val in prev:
            break
        prev.add(tmp_val)
    return min(prev)


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

Submission status: Time Limit Exceeded

Solution code:
def solve(n, k):

    prev = set()
    tmp_val = n
    while True:
        tmp_val = abs(tmp_val - k)
        if tmp_val in prev:
            break
        prev.add(tmp_val)
    return min(prev)


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

Submission status: Time Limit Exceeded

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

while abs(n-k) < n:
    n = abs(n-k)

print(n)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3

if __name__ == "__main__":
    n, k = map(int, input().split())

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

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
if n==k or k==1:
  print(0)
else:
  ans=n
  res=[ans]
  while(1):
    ans=abs(ans-k)
    if ans in res or ans == k:
      break
    res.append(ans)
  print(min(res))

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
if n==k or k==1:
  print(0)
else:
  ans=n
  res=[]
  while(1):
    ans=abs(ans-k)
    if ans in res:
      break
    res.append(ans)
  print(min(res))

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
cou = 0
i = 0
flag = 0
if  k == 1:
  n = 0
elif k == 0:
  n = n
else:
  while flag < 1:
    if n < abs(n-k):
      flag = 1
    else:
      n = abs(n-k)
      i += 1

print(n)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
cou = 0
i = 0
flag = 0
if k != 1:
  while flag < 1:
    if n < abs(n-k):
      flag = 1
    else:
      n = abs(n-k)
      i += 1
elif k == 1:
  n = 0
print(n)

Submission status: Time Limit Exceeded

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

if n%k == 0:
    print(0)
else:
    while True:
        tmp = abs(n-k)

        if n > tmp:
            n = tmp
        else:
            break
    print(n)

Submission status: Time Limit Exceeded

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

while True:
    tmp = abs(n-k)

    if n > tmp:
        n = tmp
    else:
        break
print(n)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
list = []
list.append(n)
a = 0
while True:
  n = abs(n - k)
  if list[a] < n:
    n = list[a]
    break
  else:
  	list.append(n)
  a += 1

print(n)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
s={n}
while True:
  n=abs(n-k)
  if(n in s):
    break
  else:
    s.add(n)
print(min(s))

Submission status: Time Limit Exceeded

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

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

bef=-1
x =0
count = 0

while True:
	if count>0:
		N=bef
	x=abs(N-K)
	count+=1
	if bef!=-1 and bef<=x:
		break
	bef=x


print(bef)

Submission status: Time Limit Exceeded

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

while True:
    n1 = n
    n = abs(n - k)
    if n1 > n:
        continue
    else:
        print(n1)
        break

Submission status: Time Limit Exceeded

Solution code:
a = input()
a, b = a.split(' ')
a = int(a)
b = int(b)

while a >= b:
    a -= b

if a < 0:
    a *= -1

b -= a

print(a if a < b else b)

Submission status: Time Limit Exceeded

Solution code:
import sys
n,k = [int(_) for _ in input().split()]
base = n

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

while base > abs(base - k):
  base = abs(base - k)

print(base)

Submission status: Time Limit Exceeded

Solution code:
import sys
n,k = [int(_) for _ in input().split()]
base = n

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

while base > abs(base - k):
  base = abs(base - k)

print(base)

Submission status: Time Limit Exceeded

Solution code:
# %%
N,K = map(int ,input().split(" "))
list1 = []

#差分して絶対値を求める関数
def abs_ded(A,B):
    return abs(A-B)

#繰り返して、リストに代入していく関数
def make_list(A,B):
    list1.append(abs_ded(A,B))
    while 1:
        key = abs_ded(list1[-1],B)
        if key <= B:
            if key >= list1[-1]:
                answer = list1[-1]
                break
            else:
                list1.append(key)
    
    print(answer)

make_list(N,K)

Submission status: Time Limit Exceeded

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

#差分して絶対値を求める関数
def abs_ded(A,B):
    return abs(A-B)

#繰り返して、リストに代入していく関数
def make_list(A,B):
    list1.append(abs_ded(A,B))
    while 1:
        key = abs_ded(list1[-1],B)
        if key >= list1[-1]:
            answer = list1[-1]
            break
        else:
            list1.append(key)
    
    print(answer)

make_list(N,K)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
if(K==1 and N>1):
    print(int(0))
elif(N==0 and K>=1):
    print(int(0))
else:
    i = 0
    l = 100000000000000000
    r = []
    while(i==0):
        r.append(N)
        s = abs(N-K)
        if(l>s):
            l = s
        N = s
        if(N in r):
            i+=1
    print(l)

Submission status: Time Limit Exceeded

Solution code:
lis=list(map(int,input().split()))
bef=0
bbef=0
ans=0
while lis[0]>=0:
  bbef=bef
  bef=lis[0]
  lis[0]=abs(lis[0]-lis[1])
  if bbef==lis[0]:
    if bef>=lis[0]:
      print(lis[0])
      break
    else:
      print(bef)
      break

Submission status: Time Limit Exceeded

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

while abs(n)>abs(n-m):
    n=abs(n-m)

print(n)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#C
NK = input().split(' ')
N=int(NK[0]) ; K = int(NK[1])
ans = [N]
if K == 1:
    print(0)
else:
    while N > abs(N-K):
        N = abs(N-K)
        ans.append(N)
    print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
#C
NK = input().split(' ')
N=int(NK[0]) ; K = int(NK[1])
ans = [N]
while N > abs(N-K):
    N = abs(N-K)
    ans.append(N)
print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
#C
NK = input().split(' ')
N=int(NK[0]) ; K = int(NK[1])
ans = [N]
for s in range(K*N):
    N = abs(N-K)
    ans.append(N)
print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
base = input().split()
n = int(base[0])
k = int(base[1])
ans = []
def fnk(a):
  if a>=0:
    return a
  else:
    return a*(-1)
if n%k==0:
  print(0)
elif n%k==1:
  print(1)
else:
  while fnk(n-k) not in ans:
    ans.append(fnk(n-k))
    n = fnk(n-k)
  print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
base = input().split()
n = int(base[0])
k = int(base[1])
ans = []
def fnk(a):
  if a>=0:
    return a
  else:
    return a*(-1)
if n%k==0:
  print(0)
else:
  while fnk(n-k) not in ans:
    ans.append(fnk(n-k))
    n = fnk(n-k)
  print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
base = input().split()
n = int(base[0])
k = int(base[1])
ans = []
def fnk(a):
  if a>=0:
    return a
  else:
    return a*(-1)
while fnk(n-k) not in ans:
  ans.append(fnk(n-k))
  n = fnk(n-k)
print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys

def main():
    n, k = [int(_) for _ in input().split()]
    
    m = abs(n - n // k * k)
    while True:
        ans = abs(m - k)
        if m <= ans:
            print(m)
            sys.exit()
main()

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
n ,k = map(int, input().split())
 
 
if n == k or k == 1:
    print(0)
    sys.exit()
    
while True:
    b = abs(n-k)
    if b == 0:
        print(0)
        sys.exit()
    elif b == 1:
        print(1)
        sys.exit()
    elif n < b:
        print(n)
        sys.exit()
    n = b

Submission status: Time Limit Exceeded

Solution code:
import sys
n ,k = map(int, input().split())
 
 
if n == k or k == 1:
    print(0)
    sys.exit()
    
def min_N(n, k):
    a = n
    while True:
        b = abs(a-k)
        if b == 0:
            print(0)
            sys.exit()
        elif b == 1:
            print(1)
            sys.exit()
        elif a < b:
            print(a)
            sys.exit()
        a = b
    print(a)
    sys.exit()
    
min_N(n, k)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
while True:
    if n == 0 or n == k:
        print(0)
        exit()
    elif n < k:
        m = k - n
        if n > m:
            n = m
        else:
            print(n)
            exit()
    elif n > k:
        m = n - k
        if n > m:
            n = m
        else:
            print(n)
            exit()
    else:
        print(n)
        exit()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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


while n>abs(n-k):
    n=abs(n-k)
if n==k:
    n=0
print(n)
# 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: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
if K==1 :
    print(0)
    exit()
if K==2 and N%K==0 :
  print(0)
  exit()
if K==2 and N%K==1 :
  print(1)
  exit()
for i in range(10**8):
    if abs(N-K*(i+1)) >= abs(N-K*i) :
        print(abs(N-K*i))
        exit()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

#差の絶対値
def SanoZettai(x, y):
  s = x - y
  if s >= 0:
    return s
  else:
    return -s
  
 



if  N > 1000000000:
  N = N // 10000000

  
if not N < 10000:
  N = N // 2

  
if not N < 10000:
  N = N // 2


if not N < 10000:
  N = N // 2

  
if not N < 10000:
  N = N // 2

#差の絶対値の配列
l = N

x = N
for i in range(N):
  x = SanoZettai(x, K)
  if x < l:
    l = x


print(l)

Submission status: Time Limit Exceeded

Solution code:
n,k = (int(x) for x in input().split())
if k == 1:
  print(0)
elif k == 2:
  if n%2==0:
    print(0)
  else:
    print(1)
elif k == 3:
  if n%3==0:
    print(0)
  else:
    print(1)
else:
  while abs(n-k) < n:
    n = abs(n-k)
  print(n)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
n,k = map(int,input().split())
l = []
a = abs(n-k)
b = abs(a-k)
if n < a:
    print(n)
    sys.exit()
else:
    for i in range(10**100):
        l.append(a)
        l.append(b)
        if l[i]<l[i+1]:
            print(l[i])

Submission status: Time Limit Exceeded

Solution code:
import math
 
n,k = map(int,input().split())
lst = []
lst.append(n)
 
if k > 0:
    kaisuu = math.ceil(n / k)
    for i in range(kaisuu):
        n = n - k
        lst.append(n)
 
    list2 = []
    kazu = len(lst)
    for number in lst:
        if number>= 0:
            list2.append(number)
        elif number < 0:
            mainasu = abs(number)
            list2.append(mainasu)
    print (min(list2))
    
elif k == 0:
    print (n)

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)
    if min < K:
        if min < abs(min-K):
            print(min)
            break
        else:
            print(abs(min-K))
            break
    if min % K ==0:
        print(0)
else :
    print(min)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
if K==1 :
    print(0)
    exit()
if K==2 and N%K==0 :
  print(0)
  exit()
for i in range(10**8):
    if abs(N-K*(i+1)) >= abs(N-K*i) :
        print(abs(N-K*i))
        exit()

Submission status: Time Limit Exceeded

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

def difference(val1, val2):
  tmp = val1 - val2
  return tmp if tmp >= 0 else -(tmp)

if __name__ == '__main__':
  print(main())

Submission status: Time Limit Exceeded

Solution code:
import sys

def main():
    N, K = [int(_) for _ in input().split()]
    
    # dif = abs(N - K)
    # if N < dif:
    #     print(N)
    #     sys.exit()
    # elif K < dif:
    #     print(K)
    #     sys.exit()

    m = 0
    while True:
        m = abs(N - K)
        if m < N:
            N = m
            if N==0:
                print(N)
                break
        else:
            print(N)
            break

main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N, K = map(int, input().split())

for i in range(100000000):
    if np.abs(N-K) < np.abs(N):
        N = abs(N-K)   
    else:
        print(N)
        break

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
data = input().split()

first = int(data[0])
minus = int(data[1])

while True:
  abs_num = abs(first - minus)
  if abs_num < first:
  	first = abs_num
  else:
    break;

print(first)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
A=[]
for i in range(10000000):
    N=abs(N-K)
    A.append(N)
    if N==0 or N==1 or N==2:
        break
print(min(A))

Submission status: Time Limit Exceeded

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


for i in range(1000000000000000000):
    diff = abs(n - k)
    if n > diff:
        n = diff
    else:
        break
print(n)

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int, input().split()) 
min = abs(N-K)
if N < K:
    if min < abs(min-K):
        print(min)
    else:
        print(abs(min-K))
else: 
    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: Time Limit Exceeded

Solution code:
n,k = (int(x) for x in input().split())
if k == 1:
  print(0)
elif k == 2:
  if n%2==0:
    print(0)
  else:
    print(1)
else:
  while abs(n-k) < n:
    n = abs(n-k)
  print(n)

Submission status: Time Limit Exceeded

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

if k > 0:
    kaisuu = math.ceil(n / k)
    for i in range(kaisuu):
        n = n - k
        lst.append(n)

    list2 = []
    kazu = len(lst)
    for number in lst:
        if number>= 0:
            list2.append(number)
        elif number < 0:
            mainasu = abs(number)
            list2.append(mainasu)
    print (min(list2))
    
elif k == 0:
    print (n)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
l_input = [int(e) for e in input().split()]
N = l_input[0]
K = l_input[1]
result = [N]
for i in range(1000000000000000000000000000000):
    result.append(abs(result[i]-K))
    ans = result[i+1] in result
    if result[i+1] < K/2:
        break
print(min(result))

Submission status: Time Limit Exceeded

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

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
  elif (N - (K - p)) % K == 0 or (N + (K - p)) % K == 0:
    print(K - p)
    break

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

if N % K == 0:
    print("0")
    sys.exit()
tmp = abs(N-K)
if tmp > N:
    print(N)
    sys.exit()
else:
    while(True):
        tmp_new = abs(tmp-K)
        if tmp_new > tmp:
            print(tmp)
            sys.exit()
        else:
            tmp = tmp_new

Submission status: Time Limit Exceeded

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

for i in range(100000000):
    if abs(N-K) < abs(N):
        N = abs(N-K)   
    else:
        print(N)
        break

Submission status: Time Limit Exceeded

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:
    res = []
    ans = N
    for i in range(max(N,K)):
        ans = abs(ans - K)
        res.append(ans)
        if i >= 2:
            if res[i] == res[i-2]:
                print(min(res))
                exit()
    print(min(res))

Submission status: Time Limit Exceeded

Solution code:
n,k = (int(x) for x in input().split())
while abs(n-k) < n:
  n = abs(n-k)
print(n)

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)
    if min < K:
        if min < abs(min-K):
            print(min)
            break
        else:
            print(abs(min-K))
            break
else :
    print(min)

Submission status: Time Limit Exceeded

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

if K == 1:
    print(0)

elif N == K:
    print(0)

elif N == 0:
    print(0)

elif N % 2 == 0 and K % 2 == 0 and N > K and K % N == 0:
    print(0)
elif N % 2 == 0 and K % 2 == 0 and N > K and K % N != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10 ** 18):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break


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


elif N % 2 == 1 and K % 2 == 1 and N > K and K % N == 0:
    print(0)
elif N % 2 == 1 and K % 2 == 1 and N > K and K % N != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10**18):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break


elif N % 2 == 0 and K % 2 == 0 and N < K and K % N == 0:
    print(N)
elif N % 2 == 0 and K % 2 == 0 and N < K and K % N != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10 ** 18):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break


elif N % 2 == 0 and K % 2 == 1 and N > K and N % K != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10 ** 18):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break
elif N % 2 == 0 and K % 2 == 1 and N > K and N % K == 0:
    print(0)


elif N % 2 == 0 and K % 2 == 1 and N < K and N % K != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10 ** 18):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break
elif N % 2 == 0 and K % 2 == 1 and N < K and N % K == 0:
    print(0)


elif N % 2 == 1 and K % 2 == 0 and N > K and N % K != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10 ** 18):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break
elif N % 2 == 1 and K % 2 == 0 and N > K and N % K == 0:
    print(0)


elif N % 2 == 1 and K % 2 == 0 and N < K and N % K != 0:
    if abs(N - K) > N:
        print(N)
    else:
        for i in range(10 ** 18):
            if N > abs(N - K):
                N = abs(N - K)
            else:
                print(N)
                break
elif N % 2 == 1 and K % 2 == 0 and N < K and N % K == 0:
    print(N)

else:
    print("NG")

Submission status: Time Limit Exceeded

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

if N / K == N:
    print("0")
    sys.exit()
tmp = abs(N-K)
if tmp > N:
    print(N)
    sys.exit()
else:
    while(True):
        tmp_new = abs(tmp-K)
        if tmp_new > tmp:
            print(tmp)
            sys.exit()
        else:
            tmp = tmp_new

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())
while True:
    if abs(N-K)>N:
        print(N)
        break
    else:
        S=abs(N-K)
        if S<=N:
            N=S
        else:
            print(N)
            break

Submission status: Time Limit Exceeded

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
  sw = abs(N-K)
  n += 1
  if N > sw:
    N = sw
  else:
    break
print(N)

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
if K==1 :
    print(0)
    exit()
for i in range(10**9):
    if abs(N-K*(i+1)) >= abs(N-K*i) :
        print(abs(N-K*i))
        exit()

Submission status: Time Limit Exceeded

Solution code:
import math
 
n,k = map(int,input().split())
lst = []
lst.append(n)
kaisuu = math.ceil(n / k)
for i in range(kaisuu):
    n = n - k
    lst.append(n)
 
lst2 = []
kazu = len(lst)
for number in lst:
    if number >= 0:
        lst2.append(number)
    elif number < 0:
        mainasu = abs(number)
        lst2.append(mainasu)
        
print (min(lst2))

Submission status: Time Limit Exceeded

Solution code:
import math
if True:
    a = list(map(int,input().split()))
    m = 10**20
    if(a[1]==1):
        print(0)
    else:
        while True:
            k = abs(a[0]-a[1])
            if(k>=m):
                break
            else:
                a[0] = k
                m = k
        print(a[0])

Submission status: Time Limit Exceeded

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

for i in range(100000000):
    if abs(N-K) > abs(N):
        print(N)
        break
    else:
        N = abs(N-K)

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
if K==1 :
    print(0)
    exit()
for i in range(10**9):
    if abs(N-K*(i+1)) >= abs(N-K*i) :
        print(abs(N-K*i))
        exit()

Submission status: Time Limit Exceeded

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

Solution code:

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

while True:
  if abs(x-k)>=min:
    break
  else:
    min=abs(x-k)
    x=abs(x-k)

print(min)

Submission status: Time Limit Exceeded

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(1,int(n**0.5)+1):
    if abs(n-i*k) > tmp:
      break
    tmp = min(abs(n-i*k), tmp)
print(tmp)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

if (N%K == 0):
    print(0)
else:
    while (M < mini):
        mini = min(mini, M)
        M = abs(M - K)
    print(mini)

Submission status: Time Limit Exceeded

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

if k == 1 or n == 0 or n == k:
    print('0')
else:
    # n = fractions.gcd(n, k)
    while n > abs(n - k):
        n = abs(n - k)
    print(n)

Submission status: Time Limit Exceeded

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))
else:
    for i in range(n):
        n=abs(n-k)
        ans=min(ans,n)
    
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

n,k = map(int,input().split())
lst = []
lst.append(n)
kaisuu = math.ceil(n / k)
for i in range(kaisuu):
    n = n - k
    lst.append(n)
 
list2 = []
kazu = len(lst)
for number in lst:
    if number>= 0:
        list2.append(number)
    elif number < 0:
        mainasu = abs(number)
        list2.append(mainasu)
        
print (min(list2))

Submission status: Time Limit Exceeded

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

list = []
for i in range(n//k + 2) :
    list.append(abs(n-k))
    n = abs(n-k)

print(min(list))

Submission status: Time Limit Exceeded

Solution code:
def funcname(n, k):
    return n - k

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

ans = 0
tmp = 0

flag = True
while flag == True:
    if n > k:
        n = funcname(n,k)
    else:
        tmp = funcname(n,k)
        flag = False

if abs(n) < abs(tmp):
    print(abs(n))
else:
    print(abs(tmp))

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
a,b=map(int,input().split())
if b==1:
  a=a-10**str(a).count('0')
ans=[a]
for i in range(1000000000):
  ans.append(abs(ans[i]-b))
print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
lst = []
lst.append(n)
kaisuu = (n // k) + 1
for i in range(kaisuu):
    n = n - k
    lst.append(n)

list2 = []
kazu = len(lst)
for number in lst:
    if number>= 0:
        list2.append(number)
    elif number < 0:
        mainasu = abs(number)
        list2.append(mainasu)
        
print (min(list2))

Submission status: Time Limit Exceeded

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

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

if n > k:
    tmp = k 
    while not (n - tmp) % k == 0:
        tmp -= 1
    n = tmp

while True:
    tmp = n
    n = a(n)
    if tmp < n:
        print(tmp)
        break

Submission status: Time Limit Exceeded

Solution code:
X = list(map(int, input().split()))
N = X[0]
K = X[1]
if N % K == 0:
  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(' '))
ans_list = []
ans_list.append(N)
ans = N
for i in range(N//K+1):
    if N == ans:
        ans = abs(N-K)
    else:
        ans = abs(ans-K)
    ans_list.append(ans)
    
print(min(ans_list))

Submission status: Time Limit Exceeded

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

#差の絶対値
def SanoZettai(x, y):
  s = x - y
  if s >= 0:
    return s
  else:
    return -s
  
 




#差の絶対値の配列
l = N

x = N
for i in range(N):
  x = SanoZettai(x, K)
  if x < l:
    l = x

print(l)

Submission status: Time Limit Exceeded

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