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

flag=N

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:
import sys

n,k = map(int, input().split())
if n == 0 or k == 1:
  print(0)
  sys.exit()
while True:
  ans = abs(n-k)
  if ans >= n:
    break
  elif n > ans:
    n = ans
print(n)

Submission status: Time Limit Exceeded

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

tr = N

for i in range(N):
    x = abs(tr-K)
    tr = min(x,N,tr)

print(tr)

Submission status: Time Limit Exceeded

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

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

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

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

if n == 0:
    print(0)

while True:
    pass
#print(0)

Submission status: Time Limit Exceeded

Solution code:
#162.C
N,K = map(int,input().split())
X = abs(N-K)
while True:
    if abs(X-K)< X:
        X =abs(X-K)
        continue
    else:
        print(X)
        break

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[1] == 0:
    print(init[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 < 100:
        tmp = abs(tmp - init[1])
        minX = min(minX,tmp)

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

    if count > 100:
        print(tmp)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, 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)
    elif n%3 == 1:
        print(1)
    else:
        print(1)
elif k == 4:
    if n%4 == 0:
        print(0)
    elif n%4 == 1:
        print(1)
    elif n%4 == 2:
        print(2)
    else:
        print(1)
elif k == 5:
    if n%5 == 0:
        print(0)
    elif n%5 == 1:
        print(1)
    elif n%5 == 2:
        print(2)
    elif n%5 == 3:
        print(2)
    else:
        print(1)
else:
    while n > k:
        n = n-k
    if n < k-n:
        print(n)
    else:
        print(k-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):
        Num = abs(N-K)
        N  = Num
        if N < abs(N-K):
            break
print(Num)

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)

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 = list(map(int, 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:

NK = input().split()
N = int(NK[0])
K = int(NK[1])

def function(N,K):
    pre =N
    # if N > K*10:
    #     return K-1



    while(1):    
        # print('N',N)
        N = abs(N-K)
        if pre == N:
            return pre

        if pre< N:
            return pre
        pre = N 

print(function(N,K))

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
  n = nk_abs
  count += 1
  
print(n)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

tmp1 = abs(N - K)

while True:
    if K == 1:
        print(0)
        break
    tmp2 = abs(tmp1 - K)
    if tmp1 < tmp2:
        print(tmp1)
        break
    else:
        tmp1 = tmp2

Submission status: Time Limit Exceeded

Solution code:
NK = input().split();
N,K = int(NK[0]),int(NK[1]);
min = N;

flag = True;
while flag == True:
    N = abs(N - K);
    if(N <= min):
        min = N;
    else:
        flag = False;
    
print(min);

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
n = [N]
n.append(abs(n[-1]-K))
while abs(n[-1]-K)!=n[-2]:
    n.append(abs(n[-1]-K))
n.sort()
print(n[0])

Submission status: Time Limit Exceeded

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

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())
num=list()
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())

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:

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

while n > k:
    n = n - k

temp = n

n = n - k

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

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
y=abs(N-K)
A=[]
while True:
    a=abs(y-K)
    A=[a]
    
    
print(min(A))

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
if(k==1): print(0);exit();
t = abs(n-k)
while(1):
	a = abs(t-k)
	if(a >= t):
		print(t)
		exit()
	t = a

Submission status: Time Limit Exceeded

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

flag = 0
if K == 1:
    print(0)
    quit()
if K == 2:
    if N % 2:
        print(1)
        quit()
    else:
        print(0)
        quit()
while True:
    ab = abs(N - K)
    if N > ab:
        N = ab
    else:
        break

print(N)

Submission status: Time Limit Exceeded

Solution code:
N,K =  map(int,input().split())
if K == 1 or N%K == 0:
    print(0)
    exit()
S = set()
S.add(N)
for x in range(N):
    c = abs(N-K)
    if N == c:
        break
    N = abs(N-K)
    S.add(N)
print(min(S))

Submission status: Time Limit Exceeded

Solution code:
def getInput():
    return [int(i) for i in input().rstrip().split(' ')]

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

N, K = getInput()
x = N
mins = x

while(mins > 0):
    if K == 1:
        mins = 0
    x = swap(x, K)
    if  x < mins:
        mins = x
    else:
        break

print(mins)

Submission status: Time Limit Exceeded

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

while True:
    ans = abs(n - k)
    if ans < k/2:
        print(ans)
        break;
    n = ans

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
for v in range(1000000000000000001):
    if k*v >= n:
        t = v
        tt = v - 1
        break
if t*k - n >= n - tt*k:
    print(str(n - tt*k))
else:
    print(str(t*k - n))

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 or N > output_candidates[-1]:
                break
            output_candidates.append(N)
        output = min(output_candidates)
        
print(output)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
L = []
if n > k:
  n = n - k*(n//k)
 
for i in range(k+1):
  L.append(abs(n-k))
  n = abs(n-k)
  
print(min(L))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# coding:utf-8
import math
import fractions

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

if K==0:
    print("N")
elif N==0 or N%K==0:
    print("0")
else:
    a=fractions.gcd(N,K)
    N=N%K
    N=int(N/a)
    K=int(K/a)
    mi=N
    t=N
    for i in range(N):
        t=abs(t-K)
        mi=min(mi,t)
    print(mi*a)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math

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

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

pre=n-math.floor(n/k)*k

pre=n
while 1:
    if pre < abs(pre-k):
        print(pre)
        sys.exit()
    pre = abs(pre - k)

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

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
# coding: utf-8
"""
about module
"""

def main():
    n, k = map(int, input().split())

    while n*2 > k:
        n = abs(n-k)
    return n

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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())
ans=n
while True:
  if ans>=abs(ans-k):
    ans=abs(ans-k)
  else:
    break
print(ans)

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:
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())
if k==1 or n%k==0:
    print(0)
    exit()
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 1:
    n=abs(n-k)
    
    if k>=n:
        break
if n>abs(k-n):
    n=k-n
print(n)

Submission status: Time Limit Exceeded

Solution code:
#coding: utf-8

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

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

elif abs(N-K) >= N:
    if N >= K:
        print(N%K)
    else:
        print(N)
else:
    print('jj')

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
if n%k==0:
    n=0
while 1==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 abs(N-K) < abs(N) or abs(K-N) < abs(N):
    A= abs(N-K)
    B = abs(K-N)
    if A < B:
        N = A
    else:
        N = B
print(str(N))

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

r=0

if(N<K):
  if(abs(N-K)<N):
    r=abs(N-K)
  else:
    r=N

elif(K==1):
   r=0

elif(K<N):
  while(0<N):
    r=abs(N-K)
    N=N-K

print(r)

Submission status: Time Limit Exceeded

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

flag = 0
while True:
    ab = abs(N - K)
    if N > ab:
        N = ab
    else:
        break

print(N)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

def substract(min):
    tmp = min
    if N % K == 0:
        return 0
    while(1):
        tmp = abs(tmp - K)
        if tmp > min:
            break
        min = tmp
    return min

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

Submission status: Time Limit Exceeded

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

x = -1*(n-k)
if n<x:
    print(n)
else:
    print(x)

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
    
n1=abs(n)
n2=n+k
print(min(n1,n2))

Submission status: Time Limit Exceeded

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

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

print(min)

Submission status: Time Limit Exceeded

Solution code:
in1 = input().split()
in1num = [int(x) for x in in1]
N = in1num[0]
K = in1num[1]
#print(N, K)

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

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:
import math

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

ab = abs(n - k)

while n > ab:
  n = ab
  ab = abs(n - k)

print(n)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math

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

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


pre=n
while 1:
    if pre < abs(pre-k):
        print(pre)
        sys.exit()
    pre=abs(pre-k)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,raw_input().split())
if n%k==0:
    print 0
    quit()
tmp=10**18+1
while tmp>=n:
    tmp=n
    n=abs(n-k)
print tmp

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, 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)
    elif n%3 == 1:
        print(1)
    else:
        print(2)
else:
    while n > k:
        n = n-k
    if n < k-n:
        print(n)
    else:
        print(k-n)

Submission status: Time Limit Exceeded

Solution code:
#coding: utf-8

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

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

elif abs(N-K) >= N:
    if N >= K:
        print(N%K)
    else:
        print(N)
else:
    print('jj')

Submission status: Time Limit Exceeded

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

I = N // K
n = N

for i in range(I+2):
  N = abs(N - K)
  if N < n:
    n = N

print(n)

Submission status: Time Limit Exceeded

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

while flg:
    tmp = abs(n-k)
    n = tmp
    tmp2 = abs(n-k)
    if tmp2 > tmp:
        flg = False
    
print(tmp)

Submission status: Time Limit Exceeded

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

def func(x,K):
    tmp=x-K
    if tmp<0:
        tmp*=(-1)
    return tmp
    
ans = N
while(True):
    N=func(N,K)
    if N<ans:
        ans=N
    else:
        break
print(ans)

Submission status: Time Limit Exceeded

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

mn = n

for i in range(10**7):
    n = abs(n-k)
    mn = min(mn,n)

print(mn)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
if n==0:
  print(0)
elif abs(n-2*k)>=abs(n):
  print(min(abs(n-k),abs(n)))
else:
  for i in range(10**19):
    if n<=i*k:
      print(min(abs(n-i*k),abs(n-(i-1)*k)))
      break

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N, K = np.array(list(map(int, input().split())))
x_i_1 = abs(N-K)
while 1:
    x_i = abs(x_i_1-K)
    if x_i >= x_i_1:
        print(x_i_1)
        break
    else:
        x_i_1 = x_i
        continue

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

t = n

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

  print(t)

Submission status: Time Limit Exceeded

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

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

print(abs(m))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def aoki_fun(x, k):
    return x-k if x-k >= 0 else k-x


N, K = map(int, input().split())
min_N = N
while(1):
    N = aoki_fun(N, K)
    if N < min_N:
        min_N = N

    else:
        break
print(min_N)

Submission status: Time Limit Exceeded

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

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)

print(n)

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 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())
if k == 1:
    print(0)
else:
    while n > k:
        n = n-k
    if n < k-n:
        print(n)
    else:
        print(k-n)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
for i in range(10000000000):
    a = abs(N - K)
    if N > a:
        N = a
    if N  == 0:
        break


print(N)

Submission status: Time Limit Exceeded

Solution code:
def a(n,k):
  ans = n
  while(True):
    if abs(ans-k) < ans:
      ans = abs(ans-k)
    else:
      break

  print(ans)
  
n, k = map(int, input().split())
a(n,k)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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=map(int,input().split())
while abs(n-k) < n:
  n = abs(n-k)
print(n)

Submission status: Time Limit Exceeded

Solution code:
# coding:utf-8
import math
import fractions

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

if K==0:
    print("N")
elif N==0 or N%K==0:
    print("0")
else:
    mi=N
    t=N
    for i in range(math.ceil(N*1.0/fractions.gcd(N,K))):
        t=abs(t-K)
        mi=min(mi,t)
    print(mi)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:

d={}

n,k=map(int,input().split(" "))
flag=True
while(flag):

    if(abs(n-k) not in d.keys()):
        d[abs(n-k)]=True
        n=abs(n-k)
    else:
        flag=False
#print(d.keys())
print(min(d.keys()))

Submission status: Time Limit Exceeded

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

t = n

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    n,k = map(int,input().split())
    while 1:
        check = abs(n-k)
        if n < check:
            break
        else:
            n = check
    print(n)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

if k == 1:
    print(0)
else:
    for i in range(1000000000000000000):
        if n > abs(n-k):
            n = abs(n-k)
        elif n <= abs(n-k):
            break
    print(n)

Submission status: Time Limit Exceeded

Solution code:
#200404C
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(1):
  m=abs(n-k)

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

Submission status: Time Limit Exceeded

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

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

minimum = N
previous_value = 1000000000000000001
while True:
    N = abs(N - K)

    if N < minimum:
        minimum = N

    if N >= previous_value:
        break

    previous_value = N

print(minimum)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000000)

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

if k == 1:
    print(0)
elif n < k / 2:
    print(n)
else:
    while n >= k / 2:
        n = abs(k - n)
        
    print(n)

Submission status: Time Limit Exceeded

Solution code:
import sys

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

pre = N
while True:
    if N % K == 0 and N // K > 1:
        print(0)
        sys.exit()
        
    N = abs(N-K)
    if pre <= N:
        print(pre)
        sys.exit()
    else:
        pre = N

Submission status: Time Limit Exceeded

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

result=abs(n-k)

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

print(result)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

    if ans == min(ans,abs(ans-k)):
        print(ans)
        exit()

Submission status: Time Limit Exceeded

Solution code:
n = list(map(int, input().split()))
re = True
num = n[0]
if n[1] == 1:
  print(0)
elif n[1] == 2:
  if n[0]%2 == 0:
    print(0)
  else:
    print(1)
else:
  while re:
    k = num - n[1]
    if num > abs(k):
      num = abs(k)
    else:
      re = False
  print(num)

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

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

print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

def f(x,k):
    return (abs(x-k) < x)

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

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
n=int(n)
k=int(k)
n = n % k
minimam = k
for i in range(k):
  if n < minimam:
    minimam = n
  n = abs(n-k)
print(minimam)

Submission status: Time Limit Exceeded

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

if k == 1:
    print(0)
else:
    while 1:
        n = abs(n-k)
        ans = min(ans, n)

        if n in done or n == 0:
            break
        else:
            done.append(n)
    print(ans)

Submission status: Time Limit Exceeded

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


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

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


if __name__ == '__main__':
    main()

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 in seen:
    print(min(seen))
    sys.exit()
  else:
    seen.append(x)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys

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

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

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
if k==1:
    print(0)
    exit()
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 = abs(n - k)

i = False

if n % k == 0:
  ans = 0
  i = True

while not i:
  if ans > abs(ans - k):
    ans = abs(ans - k)
  else:
    i = True

print(ans)

Submission status: Time Limit Exceeded

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

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

low = 10**19
while True:
    tmp = abs(n-k)
    if tmp < low:
        low = tmp
        n = tmp
    else: break

print(low)

Submission status: Time Limit Exceeded

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

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

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

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,input().split())
H = 0
while True:
  if (N > K):
    H = N-K
  else:
    H = K - N
  if (H>N):
    break
  else:
    N = H
print(N)

Submission status: Time Limit Exceeded

Solution code:
n = list(map(int, input().split()))
re = True
num = n[0]
if n[1] == 1:
  print(0)
else:
  while re:
    k = num - n[1]
    if num > abs(k):
      num = abs(k)
    else:
      re = False
  print(num)

Submission status: Time Limit Exceeded

Solution code:
N, K = list(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())

if K == 1:
    print(0)
    quit()

pre = N
new = abs(N - K)
while new < pre:
    #print(pre, new)
    pre = new
    new = abs(new - K)
print(pre)