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 = map(int,input().split());
i = 0;
while True:
    if (n - i)% k == 0:
        break;
    if (n + i)% k == 0:
        break;
    i += 1;

print(i);

Submission status: Time Limit Exceeded

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

if N > K and K == 1:
    print(0)
    exit(0)

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 = n

while n > 0:
  if k == 1:
    ans = 0
    break
    
  ans = min(ans, abs(n - k))
  n -= k

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

if n <= k :
    print(min(n, k-n))
    exit(0)
if n>k:
    while n-k >=0:
        n -= k
    print(min(n, k-n))

Submission status: Time Limit Exceeded

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



def solve(n, k):
    if k == 1:
        print(0)
        return

    res = n
    
    while res <= n and res >= 0:
        new = abs(res - k)
        if new < res:
            res = new
        else:
            break
    
    print(min(res,n))

solve(n,k)

Submission status: Time Limit Exceeded

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

if n < k:
    ans = min(abs(n-k), n)

else:    
    if n%k == 0:
        ans = 0
    else:
        for i in range(-k,k+1):
            if (i%k == n%k and i < 0):
                nega = -i
            elif (i%k == n%k and i >= 0):
                posi = i
        ans = (min(nega, posi))
print(ans)

Submission status: Time Limit Exceeded

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

if n%k == 0:
    ans = 0
else:
    for i in range(-k,k+1):
        if (i%k == n%k and i < 0):
            nega = -i
        elif (i%k == n%k and i >= 0):
            posi = i
    ans = (min(nega, posi))
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans = 1e+19

while True:
    N = abs(N-K)
    if N >= ans:
        break
    ans = N

print(ans)

Submission status: Time Limit Exceeded

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

ans = 1e+19

while True:
    N = abs(N-K)
    if N >= ans:
        break
    ans = N

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for _ in range(int(n / k) + 1):
    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())

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())
x = N
x_b = 0

if K == 1:
    x = 0
else:
    while True:
        x_b = x
        x = abs(x-K)
        if abs(x-K) == x_b:
            break

print(min(x_b,x))

Submission status: Time Limit Exceeded

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

while True:
    if not n%k:
        n = 0
        break
    num = n
    n = n-k
    if n < 0:
        n = min(abs(n), num)
        break

print(n)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
temp = N
mini = float("inf")
while True:
    mini = min(temp, mini)
    temp = abs(temp - K)
    if temp >= mini:
        break
print(mini)

Submission status: Time Limit Exceeded

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


resolve()

Submission status: Time Limit Exceeded

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

resolve()

Submission status: Time Limit Exceeded

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()

def main():
    n, k = map(int, input().split())
    if k == 1:
        n = 0
        print(n)
        return
    while n > k//2:
        n = abs(n-k)
    print(n)

main()

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
x0 = n
x1 = n
count = 0
while x0 >= 0 :
  x0 -= k
  count += 1
else:
  if min(x0+k,-x0) ==x0+k:
    print(x0+k)
  else :
    print(-x0)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

print(x)

Submission status: Time Limit Exceeded

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

a = []
while True:
    n = abs(n - k)
    a.append(n)
    if n <= k // 2:
        break
    
print(min(a))

Submission status: Time Limit Exceeded

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

a = []
while True:
    n = abs(n - k)
    a.append(n)
    if n < k // 2:
        break
    
print(min(a))

Submission status: Time Limit Exceeded

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

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

    if k % n == 0:
        break

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

print(n)

Submission status: Time Limit Exceeded

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

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

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

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

print(n)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

tmp = [n]

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
x=[N,abs(N-K)]
i=1
while x[i] < x[i-1]:
    x.append(abs(x[i]-K))
    i+=1
print(x[i-1])

Submission status: Time Limit Exceeded

Solution code:
x, k = [int(i) for i in input().split()]
for i in range(10000000000):
    if x <= abs(x-k):
        print(x)
        break
    else:
        x = abs(x-k)
        continue

Submission status: Time Limit Exceeded

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

while(n > k):
    n -= k
if(n < k/2):
    print(n)
else:
    print(k-n)

Submission status: Time Limit Exceeded

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

tmpx=float('inf')
x=abs(n-k)

while tmpx>x:
    tmpx=x
    x=abs(tmpx-k)

print(tmpx)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

while n>0:
    n -= k

n = abs(n)

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

Submission status: Time Limit Exceeded

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

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

print(min(x))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

    
    
    
print(min(f))

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
ans = 10**18 + 1
if k == 1:
    print(0)
    exit()
while ans > k:
    ans = min(ans, n)
    n = abs(n-k)

print(min(ans, k-ans))

Submission status: Time Limit Exceeded

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

for i in range(10**19):
  n -= k
  if n <= 0:
    break
    
print(min(abs(n),n+k))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

N = s[0]
K = s[1]

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

Submission status: Time Limit Exceeded

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

        ans = 0

        if N%K==0:
                ans = 0
        else:
                if N>=K:
                        temp = K
                        while temp<N:
                                temp += K
                        upper = temp-N
                        lower = abs(upper-K)
                        ans = min(upper, lower)
                else:
                        ans = min(N, K-N)

        print("{}".format(ans))

if __name__=="__main__":
        main()

Submission status: Time Limit Exceeded

Solution code:
ans =0
for i in range(1000000000000000000):
  ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

    min_num = N
    while True:
        dif = abs(min_num - K)
        if dif < min_num:
            min_num = dif
        else:
            break
    print(min_num)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
List = list(map(int, input().split()))
x = List[0]
y = abs(x-List[1])

while x >= y:
    z = abs(x-List[1])
    x = y
    y = z

print(x)

Submission status: Time Limit Exceeded

Solution code:
def mini(N, K):
    return abs(N-K)


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=map(int,input().split())

s=math.ceil(n/k)
list_t=[]
for i in range(s+1):
  t=abs(n-i*k)
  list_t.append(t)
  
print(min(list_t))

Submission status: Time Limit Exceeded

Solution code:
N, K = list(map(int, input().split()))
flag =False
while flag==False:
    if 2*N > K:
        N = N%K
    else:
        print(N)
        flag = True

Submission status: Time Limit Exceeded

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

m = 10**18
while True:
    n = min(n, abs(n-k)) 
    if n < m:
        m = n
    else:
        break

print(m)

Submission status: Time Limit Exceeded

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

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

        else:
            print(N)
            break

Submission status: Time Limit Exceeded

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

while True:
    delta = abs(N - K)
    if delta <N:
        N = delta

    else:
        print(N)
        break

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
s = n
while s > 0:
    s = s-k
if s*(-1) > s+k:
    print(s+k)
else:
    print(s*(-1))

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())
preN = N
while preN >= 0:
  preN = abs(N-K)
  if preN >= 0:
    N = preN
print(N)

Submission status: Time Limit Exceeded

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

if n >= k:
    while(n >= k):
        n -= k
if n > k/2:
    while(n > k/2):
        n = k-n
if n <= k/2:
    print(n)

Submission status: Time Limit Exceeded

Solution code:
import sys

if sys.platform =='ios':
    sys.stdin=open('input_file.txt')
   
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


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

if k==1:
	print("0")

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

Submission status: Time Limit Exceeded

Solution code:
N,K = [int(i) for i in input().split()]
if N%K==0:
    print("0")
else:
    while abs(N-K) < N:
        N = abs(N-K)
    print(N)

Submission status: Time Limit Exceeded

Solution code:
N,K = [int(i) for i 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())
s = n % k
t = s
for _ in range(t):
  if abs(s-k)<s:
    s = abs(s-k)
print(s)

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:
import sys
n,k = map(int,input().split())
m = n
c = 0
r = True
if n%k == 0:
    print(0)
    sys.exit()
while r == True:
    if k >= 2*m:
        r = False
    elif k >= m:
        c += 1
        r = False
    else:
        c += 1
        m -= k

print(abs(n-(c*k)))

Submission status: Time Limit Exceeded

Solution code:
import sys
n,k = map(int,input().split())
m = n
c = 0
r = True
while r == True:
    if k >= 2*m:
        r = False
    elif k >= m:
        c += 1
        r = False
    else:
        c += 1
        m -= k

print(abs(n-(c*k)))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

a=N
while True:
    if a-K>=0:
        a=a-K
    elif K-a>=a:
        a=a
        break
    else:
        a=K-a
print(a)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

if N > K:
    x = N-K
else:
    x = K-N

while  N > x :
    N = x

    if N > K:
        x = N-K

    else:
        x = K-N

print(N)

Submission status: Time Limit Exceeded

Solution code:
N, K = [int(i) for i in input().split()]
while N > abs(K - N):
    if N < K:
        big = K
        small = N
    else:
        big = N
        small = K

    sa = big - small
    N = sa

print(N)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
count = 0
if n > k:
  while n > k :
    n -= k
    count += 1
  opponent = abs(n - k)  
  print(opponent) if n >= opponent else print(n)
else:
  op = abs(n-k)
  print(op) if n >= op  else print(n)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
count = 0
lst =[n]
if n > k:
  while n > k :
    n -= k
    count += 1
    lst.append(n)
  opponent = abs(lst[count] - k)  
  if lst[count] >= opponent:
    print(opponent)
  else:
    print(lst[count])
else:
  op = abs(n-k)
  if n >= op:
    print(op)
  else:
    print(n)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

cm = N
if N % K == 0:
    print(0)
    exit()
while True:
    cm = min(cm, abs(N - K))
    if cm >= N:
        break
    N = cm

print(cm)

Submission status: Time Limit Exceeded

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

list=[N]
while True:
    i=0
    if list[i]-K>=0:
        N=list[i]-K
        list.append(N)
    else:
        N=K-list[i]
        list.append(N)
print(min(list))

Submission status: Time Limit Exceeded

Solution code:
def main():
	n,k = map(int,input().split())
	stop = 0
	while stop == 0:
		nabs = abs(n-k)
		if n < nabs:
			stop = 1
			print(n)
		else:
			n = nabs



if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
res = [N]
if N % K == 0:
  print(0)
else:
  while True:
    M = abs(N - K)
    if M  in res:
      break
    elif M not in res:
      N = M
      res.append(N)
  print(min(res))

Submission status: Time Limit Exceeded

Solution code:
import math
n,k = map(int, input().split())
# i = int(n/k)
# ilow = i
# iup = i+1
# print(min(n,abs(n-k*iup),n-k*ilow))

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

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
N_list=[]
N0=N
while True:
    add=abs(N0-K)
    if add not in N_list:
        N_list.append(add)
        N0=add
    else:break

print(min(N_list))

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

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):
        n = abs(n-k)
    print(n)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int, raw_input().split(' '))
seen = {}

while(n not in seen):
	seen[n] = abs(n - k)
	n = abs(n - k)

print min(seen.values())

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

    n=abs(n_b-K)
    if(n>n_b):
        print(n_b)
        exit(0)
    n_b=n

Submission status: Time Limit Exceeded

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

while N > 0:
    N = (N-K)

if N+K > -N:
    print(-N)
else:
    print(N+K)

Submission status: Time Limit Exceeded

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

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

print(N)

Submission status: Time Limit Exceeded

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

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

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

print(N)

Submission status: Time Limit Exceeded

Solution code:
def abs(n, k):
    if n - k >= 0:
        return n-k
    else:
        return k-n

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def sovle(x, y):
    while(x>=y):
        x=x-y
    if(x>y-x):
        return y-x
    else:return x



if __name__ == '__main__':
    nums = list(map(int, input().split(" ")))
    print(sovle(nums[0], nums[1]))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans1 = abs(N - K)
cnt = 0
if N > ans1:
    while True:
        ans2 = abs(ans1 - K)
        if ans1 > ans2:
            ans1 = ans2
            cnt = 1
            cnt += 1
        else:
            ANS = ans1
            break
elif N <= ans1:
    ANS = N

# print(ans1, ans2)
print(ANS)
# print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int, input().split())
if N == 0:
    print(0)
else:
    while True:
        if N > K*10000:
            N = N-(K*10000)
        elif N > K*1000:
            N = N-(K*1000)
        elif N > K*100:
            N = N-(K*100)
        else:
            break

    x0 = N #7
    x1 = abs(x0-K) #3
    while True:
        if x0 < x1:
            print(x0)
            break
        else:
            x0,x1 = x1,abs(x0-K)
            continue

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int, input().split())
x0 = N #7
x1 = abs(x0-K) #3
while True:
    if x0 < x1:
        print(x0)
        break
    else:
        x0,x1 = x1,abs(x0-K)
        continue

Submission status: Time Limit Exceeded

Solution code:
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 = (int(x) for x in input().split())
l = []
while 1:
    if not N % K:
        l.append(0)
        break
    N = abs(N - K)
    if N < K:
        if N in l:
            break
        l.append(N)
print(min(l))

Submission status: Time Limit Exceeded

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

new_li = []
n = 0

while True:
  if li[1] == 1:
    print(0)
    break

  li[0] = abs(li[0]-li[1])
  new_li.append(li[0])
  
  if n>=2:
    if new_li[n] == new_li[n-2]:
      print(min(new_li))
      break
  n += 1

Submission status: Time Limit Exceeded

Solution code:
li = list(map(int, input().split()))
new_li = []
n = 0
while True:
  li[0] = abs(li[0]-li[1])
  new_li.append(li[0])
  
  if n>=2:
    if new_li[n] == new_li[n-2]:
      print(min(new_li))
      break
  n += 1

Submission status: Time Limit Exceeded

Solution code:
import collections

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

roop=0
i=[]
while roop==0 :
    N=abs(N-k)
    i.append(N)
    if collections.Counter(i)["N"] > 2 :
        roop=1

print(min(i))

Submission status: Time Limit Exceeded

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

roop=0
counter=0
i=[]
while roop==0 :
    N=abs(N-k)
    i.append(N)
    counter+=1
    if i.count(N) > 2 :
        roop=1

print(min(i))

Submission status: Time Limit Exceeded

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

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

N_1 = abs(N - K)

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

print(N)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

loop = [0]

for i in loop:
  if num[0] < num[1]:
    difference = abs(num[0] - num[1])
    if difference > num[0]:
      break
    else:
      num[0] = difference
      break
  elif num[1] == 1:
    num[0] = 0
    break
  elif num[1] == 0:
    break
  num[0] = num[0] % num[1]
  num[0] = abs(num[0] - num[1])
  loop.append(i+1)

print(num[0])

Submission status: Time Limit Exceeded

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

loop = [0]

for i in loop:
  if num[0] < num[1]:
    difference = abs(num[0] - num[1])
    if difference > num[0]:
      break
    else:
      num[0] = difference
      break
  elif num[1] == 1:
    num[0] = 0
    break
  elif num[1] == 0:
    break
  num[0] = abs(num[0] - num[1])
  loop.append(i+1)

print(num[0])

Submission status: Time Limit Exceeded

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

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

elif N<K and N<=K-N:
    print(N)

elif N<K and N>K-N:
    print(K-N)

elif N>K:

    for i in range(18):
        k = K*(10**(17-i))
        while N>=k:
            k=k+K*(10**(17-i))
        k = k-K*(10**(17-i))
    k = k+K
    if k-N<N-(k-K):
        print(k-N)
    else:
        print(N-(k-K))

Submission status: Time Limit Exceeded

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

if N == K:
    print(0)

elif N<K and N<=K-N:
    print(N)

elif N<K and N>K-N:
    print(K-N)

elif N>K:

    for i in range(18):
        k = K*(10**(17-i))
        while N>=k:
            k=k+K*(10**(17-i))
        k = k-K*(10**(17-i))
    k = k+K
    if k-N<N-(k-K):
        print(k-N)
    else:
        print(N-(k-K))

Submission status: Time Limit Exceeded

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

if N == K:
    print(0)

elif N<K and N<=K-N:
    print(N)

elif N<K and N>K-N:
    print(K-N)

elif N>K*(10**8):
    k = K*(10**8)
    for i in range(9):
        while N>k:
            k=k+K*(10**(8-i))
        k = k-K*(10**(8-i))
    k = k+K
    if k-N<N-k/K:
        print(k-N)
    else:
        print(N-k/K)


elif N>K:
    k = K
    while N>k:
        k=k+K
    if k-N<N-k/K:
        print(k-N)
    else:
        print(N-k/K)

Submission status: Time Limit Exceeded

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

if N == K:
    print(0)

elif N<K and N<=K-N:
    print(N)

elif N<K and N>K-N:
    print(K-N)

elif N>K:
    k = K
    while N>k:
        k=k+K
    if k-N<N-k/K:
        print(k-N)
    else:
        print(N-k/K)

Submission status: Time Limit Exceeded

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

if N == K:
    print(0)

elif N<K and N<=K-N:
    print(N)

elif N<K and N>K-N:
    print(K-N)

elif N>K:
    while N>0:
        N = N - K
    if -N<N+K:
        print(-N)
    else:
        print(N + K)

Submission status: Time Limit Exceeded

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

def abs(a):
    if a>=0:
        return a
    else:
        return -a

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

Submission status: Time Limit Exceeded

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

def abs(a):
    if a>=0:
        return a
    else:
        return -a

List=[]

while True:
    n=abs(n-k)
    if n not in List:
        List.append(n)
    else:
        break

print(min(List))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

next = n
while (next >= 0 or n <= next): 
  n = next 
  n = abs(n-k)
 
a, b = list(map(int, input().split()))

n = max(a, b)
k = min(a, b)
prev = k


while (k <= prev and k != 0):
    prev = k
    a = int(n/k)
    b = n % k
    n = max(a, b)
    k = min(a, b)
    # print(a, b, prev, n, k)

print(prev)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

print(n)

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)
    else:
        print(N)
        break