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())
a = []
for i in range(10**18):
    n = abs(n-k)
    a.append(n)
    if a[len(a)-2] < n:
        print(a[len(a)-2])
        break

Submission status: Time Limit Exceeded

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

lN = [N]
c = 0
for i in range(100000000000000000):
    N = abs(N - K)
    lN.append(N)
    c += 1

print(min(lN))

Submission status: Time Limit Exceeded

Solution code:
(x, y)= input().split()
d1 = int(x)
d2 = d1
while 1:
    d1 = abs(d2-int(y))
    if d1 <= d2:
       d2 = d1
    else:
        break
print(d2)

Submission status: Time Limit Exceeded

Solution code:
import math

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


def check(K,N ):
    min = 0
    abs_num = 0

    OLD_ABS =N
    NEW_ABS = abs(K-N)

    while NEW_ABS < OLD_ABS:
        OLD_ABS = N #OK
        N = NEW_ABS ##OK
        NEW_ABS =abs(K-N)
    #    print("N_NEW",NEW_ABS,"OLS_ABS",OLD_ABS,"N",N)
    print(N)

    

     
            
K,N = nyu()
check(K,N)
#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:
n, k = list(map(int, input().split()))

n_prev = n

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

Submission status: Time Limit Exceeded

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

m = N

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

Submission status: Time Limit Exceeded

Solution code:
X = input().split()
N = int(X[0])
K = int(X[1])

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

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int, input().split())
sFlag=True
N_min=min(N,K)
N_flag=0
while sFlag:
  N=abs(N-K)
  N_K=N-K
  if(N_K<0):
    N_flag=N_flag+1
  N_min=min(N_min,N)
  if(N_flag==2):
    sFlag=False
  if(K==1):
    N_min=0
    sFlag=False
print(N_min)

Submission status: Time Limit Exceeded

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

pre_value=n + k
tmp=n
while tmp < pre_value:
    pre_value = tmp
    tmp = abs( tmp - k)
    
print(pre_value)

Submission status: Time Limit Exceeded

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

while True:
  if abs(tmp - K) < tmp:
    tmp = abs(tmp - K)
  else:
    break
 	
print(tmp)

Submission status: Time Limit Exceeded

Solution code:
import math

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

while True:
  if abs(tmp - K) < tmp:
    tmp = abs(tmp - K)
  else:
    break
 	
print(tmp)

Submission status: Time Limit Exceeded

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

x = [N]
i = 0
while True:
    N = abs(N-K)
    x.append(N)
    i += 1
    if x[i-1] <= x[i]:
        break
print(min(x))

Submission status: Time Limit Exceeded

Solution code:
num_list = list(map(int, input().split()))
tmp_num = num_list[0]
draw_num = num_list[1]

if tmp_num % draw_num == 0:
  print(0)
  exit()

tmp_num_list = [] 
  
while True: 
  tmp_num_list.append(tmp_num)
  tmp_num = abs(tmp_num - draw_num)
  if tmp_num in tmp_num_list:
    break
  
print(min(tmp_num_list))

Submission status: Time Limit Exceeded

Solution code:
num_list = list(map(int, input().split()))
tmp_num = num_list[0]

if tmp_num % num_list[1] == 0:
  print(0)
  exit()

tmp_num_list = [] 
  
while True: 
  tmp_num_list.append(tmp_num)
  tmp_num = abs(tmp_num - num_list[1])
  if tmp_num in tmp_num_list:
    break
  
print(min(tmp_num_list))

Submission status: Time Limit Exceeded

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

min_n = n
n = abs(n - k)
#print(n, k)
if n >= min_n:
    print(min_n)

else:
    cou = 0
    while(min_n >= n):

        cou += 1
        min_n = n
        n = abs(n - k)
        #print(n)
        

    print(min_n)

Submission status: Time Limit Exceeded

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

value = abs(N - K)

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

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

print(value)

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

Submission status: Time Limit Exceeded

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

if N>=abs(N-K):
    print(int(abs(N-K)))
else:
    print(int(N))

Submission status: Time Limit Exceeded

Solution code:
N,K=(int(x) for x in input().split())
x=N
while x>K:
    x=abs(x-K)
print(min([x,abs(K-x)]))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
x, k = (int(x) for x in input().split())
while True:
    absolute = abs(x - k)
    if x <= absolute:
        print(x)
        break
    else:
        continue

Submission status: Time Limit Exceeded

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

while True:
    if x > abs(x - k):
        x = abs(x - k)
    else:
        break
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:
N,K=map(int,input().split())

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

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
if(k==1):
  print(0)
else:
  t = n//k
  for i in range(t):
    n = abs(n-k)
  if(n < abs(k-n)):
    print(n)
  else:
    print(abs(k-n))

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
if(k==1):
  print(0)
else:
  while(len(str(n))>1):
    n = n/2
  t = n//k
  for i in range(t):
    n = abs(n-k)
  if(n < abs(k-n)):
    print(n)
  else:
    print(abs(k-n))

Submission status: Time Limit Exceeded

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

a = []
a.append(N)

if N % K == 0:
    print(0)
else:
    while True:
        if abs(N - K) not in a:
            a.append(abs(N - K))
            N = abs(N - K)
        else:
            print(min(a))
            break

Submission status: Time Limit Exceeded

Solution code:
n,k =map(int,input().split())
b=[n]
while abs(n-k)<n:
  n=abs(n-k)
b.append(n)
print(b[-1])

Submission status: Time Limit Exceeded

Solution code:
n,k =map(int,input().split())
b=[n]
while abs(n-k)<n:
  n=abs(n-k)
  b.append(n)
print(b[-1])

Submission status: Time Limit Exceeded

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


if k == 1:
    n = 0
else:
    while True:
        if abs(k - n) < n:
            n = abs(k - n)
        else:
            FLG = True
            break

print(n)

Submission status: Time Limit Exceeded

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

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

print(n)

Submission status: Time Limit Exceeded

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

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

  print(min)

if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
l = input()
m = l.split(" ")
n = int(m[0])
k = int(m[1])
while (abs(n-k)<n):
  n = abs(n-k)
print(n)

Submission status: Time Limit Exceeded

Solution code:
l = input()
m = l.split(" ")
n = int(m[0])
k = int(m[1])
while (abs(n-k)<n):
  n = abs(n-k)
  a = abs(n-k)
print(n)

Submission status: Time Limit Exceeded

Solution code:
l = input()
m = l.split(" ")
n = int(m[0])
k = int(m[1])
a = abs(n-k)
while a < n:
  n = a
  a = abs(n-k)
print(n)

Submission status: Time Limit Exceeded

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

if abs(N + K) > abs(N):
    print(abs(N))
elif abs(N + K) < abs (N):
    print(abs(N + K))
else:
    print(abs(N))

Submission status: Time Limit Exceeded

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

list=[]


if N%K==0:
    print(0)
else:
    while N not in list:
        list.append(N)
        N=abs(N-K)
    print(min(list))

Submission status: Time Limit Exceeded

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

list=[]

while N not in list:
    list.append(N)
    N=abs(N-K)

print(min(list))

Submission status: Time Limit Exceeded

Solution code:
NK = input().split(' ')
N = int(NK[0])
K = int(NK[1])
 
while True:
  d = abs(N-K)
  if d<N:
    N = d
  else:
    break

print(N)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
ans = 0
judge = True

while judge == True:
    if K == 1:
        N = 0
        break

    ans = N - K
    N = abs(ans)

    ans = N - K
    mem = abs(ans)

    if N > mem:
        pass
    else:
        judge = False


print(N)

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
A=N
for i in range(N+1):
    A=min(A,abs(N-K*i))
print(A)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

answer = 10**18+1
formar = 10*18+1
c_v = N

if N % K == 0:
    print(0)
else:
    N = N//K
    while True:
        if answer > c_v:
            answer = c_v
        n_v = abs(c_v - K)
        if formar == n_v:
            break
        formar = c_v
        c_v = n_v
    print(answer)

Submission status: Time Limit Exceeded

Solution code:
#スペース区切りの入力
N, K = (int(x) for x in input().split())
# 型変換
n = abs(N)
k = abs(K)

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

Submission status: Time Limit Exceeded

Solution code:
import sys
N, M = map(int, input().split())
ans = N
while True:
   if abs(ans-M) < ans:
        ans = abs(ans-M)
   else:
    print(ans)
    sys.exit()

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
val = input().split()
N = int(val[0])
K = int(val[1])
min = N
list = [N]
calc = abs(min-K)
while calc not in list:
  list.append(calc)
  if calc < min:
    min = calc
  calc = abs(min-K)
print(min)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:

def main():
    N, K = map(int, input().split(' '))
    
    # out = min(float('inf'), N)
    prev = N
    
    isFinish = False
    
    if N % K == 0:
        print(0)
        return
    
    while not isFinish:
        next = abs(prev - K)
        
        if next >= prev:
            isFinish = True
        else:
            prev = next
            
    print(prev)

main()

Submission status: Time Limit Exceeded

Solution code:

def main():
    N, K = map(int, input().split(' '))
    
    # out = min(float('inf'), N)
    prev = N
    
    isFinish = False
    
    if N % K == 0:
        print(0)
        return
    
    while not isFinish:
        next = abs(prev - K)
        
        if next > prev:
            isFinish = True
        else:
            prev = next
            
    print(prev)
        
        
    
    
    
    
main()

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

# In[10]:


N,K = map(int,input().split())
new = abs(N-K)
while(new<N):
    N = new
    new = abs(new-K)
print(N)

Submission status: Time Limit Exceeded

Solution code:
from sys import maxsize

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

if n % k == 0:
    print(0)
else:
    p = maxsize
    while n < p:
        p = n
        n = abs(n - k)
    print(p)

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int,input().split())
n_min = 10**18
n_min_sb = 10**19
if k == 1:
    n_min = 0
else:
    while n_min != n_min_sb:
        n = abs(n-k)
        if n_min > n:
            n_min_sb = n_min
            n_min = n
        if n_min < n:
            n_min_sb = n_min
print(n_min)

Submission status: Time Limit Exceeded

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

res = 0
pn = n
for i in range(10**18):
    if n % K == 0:
        res = 0
        break
    if abs(pn - K) > pn:
        res = pn
        break
    else:
        pn = n
        n = abs(pn - K)

print (res)

Submission status: Time Limit Exceeded

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

res = 0
pn = n
for i in range(10**18):
    if K == 1:
        res = 0
        break
    if abs(pn - K) > pn:
        res = pn
        break
    else:
        pn = n
        n = abs(pn - K)

print (res)

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

Submission status: Time Limit Exceeded

Solution code:
N , K = map(int, input().split())
flag = "F"
while 1:
    if N>10000000*K:
        N = N -10000000*K
    if N>10000000*K:
        N = N -10000000*K
    if N>1000000*K:
        N = N -1000000*K
    if N>100000*K:
        N = N -100000*K
    if N>10000*K:
        N = N -10000*K
    
    a = N
    N = abs(N - K)
    

    if a<=K and N <= K:
        if a < N:
            result = a
            flag="T"
        else:
            result = N
            flag="T"
    if flag == "T":
        break
print(result)

Submission status: Time Limit Exceeded

Solution code:
N , K = map(int, input().split())
flag = "F"
while 1:
    if N>10000000*K:
        N = N -10000000*K
    a = N
    N = abs(N - K)
    

    if a<=K and N <= K:
        if a < N:
            result = a
            flag="T"
        else:
            result = N
            flag="T"
    if flag == "T":
        break
print(result)

Submission status: Time Limit Exceeded

Solution code:
N , K = map(int, input().split())
flag = "F"
while 1:
    if N>100000000*K:
        N -100000000*K
    a = N
    N = abs(N - K)
    

    if a<=K and N <= K:
        if a < N:
            result = a
            flag="T"
        else:
            result = N
            flag="T"
    if flag == "T":
        break
print(result)

Submission status: Time Limit Exceeded

Solution code:
N , K = map(int, input().split())
flag = "F"
while 1:
    if N>10000*K:
        N -10000*K
    a = N
    N = abs(N - K)
    

    if a<=K and N <= K:
        if a < N:
            result = a
            flag="T"
        else:
            result = N
            flag="T"
    if flag == "T":
        break
print(result)

Submission status: Time Limit Exceeded

Solution code:
N , K = map(int, input().split())
flag = "F"
while 1:
    a = N
    N = abs(N - K)

    if a<=K and N <= K:
        if a < N:
            result = a
            flag="T"
        else:
            result = N
            flag="T"
    if flag == "T":
        break
print(result)

Submission status: Time Limit Exceeded

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

if b == 1:
    print(0)
    exit()
      
while True:
    x = abs(a-b)
    if x < a:
        a = x
    else:
        print(a)
        break

Submission status: Time Limit Exceeded

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

while x>K/2:
    x = abs(x-K)
print(x)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

prev = N

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
list_input = [int(n) for n in input().split()]

N = list_input[0]
K = list_input[1]

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

print(N)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y) #再帰定義
N,K=list(map(int,(stdin.readline().strip().split())))
if N%K==0:print(0)
else:
    M=10**19
    while(True):
        M = N
        N-=K
        if N<=0:
            if abs(N)<abs(M):
                print(abs(N))
                break
            else:
                print(abs(M))
                break

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

while True:

    comp = abs(N-K)

    if(N<=comp):
        ans = N
        break

    else:
        N = comp


print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
x=n-k
if x==0:
    print("0")
elif x<0:
     if abs(n-k)<n:
         print (str(abs(n-k)))
     else:
         print(str(n))
else:
    while x>0:
        x-=k
    if x==0:
       print("0")
    else:
        if abs(x-k)<abs(x):
            print (str(abs(x-k)))
        else:
            print(str(abs(x)))

Submission status: Time Limit Exceeded

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

if N%K==0:
  print(0)
else:
  minimum=N
  N=abs(N-K)

  if minimum <=N:
      print(minimum)
  else:
      while minimum > N:
          minimum=N
          N=abs(N-K)
      print(minimum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

a=[n]
i=0
while True:
    a.append(abs(a[i]-k))

    if a[i+1]>=a[i]:
        print(a[i])
        break
    i=i+1

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

print(n)

Submission status: Time Limit Exceeded

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

print(temp)

Submission status: Time Limit Exceeded

Solution code:
num=input().rstrip().split(" ")
num1=int(num[0])
num2=int(num[1])
result=1
list0=[num1]
while result>0:
  result=num1-num2
  if result<0:
    result=-result
    list0.append(result)
    break
  else:
     list0.append(result)
list0=sorted(list0)
print(list0[0])

Submission status: Time Limit Exceeded

Solution code:
m, n = map(int, input().split())
arr=[]
while abs(m-n) not in arr:
  arr.append(abs(m-n))
  m= abs(m-n)

print(min(arr))

Submission status: Time Limit Exceeded

Solution code:

N, K = map(int,input().split())
#abstract = abs(N - K)

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

print(N)

Submission status: Time Limit Exceeded

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


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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:

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

#x = abs(k-x)
y = int(abs(n/k)) + 1
l = n
s = [n]
for i in range(y):
    l = abs(l-k)
    s +=[l]
print(min(s))

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    n, k = [int(i) for i in input().split(" ")]
    while abs(n - k) <= n:
        if n%(k*10)==0:
            n = int(n/(k*10))
        else:
            n = abs(n - k)
        
    print(n)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    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:
def CalAbs(a, b):
    sol=a-b
    if sol<0:
        sol=sol*(-1)
    return sol

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

rec=10**18+1
i=0

while i<10**19:
    if rec>N:
        rec=N
    N=CalAbs(N,K)
    i=i+1
print(rec)

Submission status: Time Limit Exceeded

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

while(abs(n-k) <= abs(pre-k)):
    if n % k == 0:
        n = 0
        break
    pre = n
    n = abs(n-k)

print(n)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    a, b = map(int, input().split())
    save_result = []
    append_f = save_result.append
    append_f(a)
    while True:
        a = a - b
        if a < 0:
            abs_s = abs(a)
            append_f(abs_s)
            break
        else:
            append_f(a)

    print(min(save_result))

resolve()

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    a, b = map(int, input().split())
    save_result = []
    save_result.append(a)
    while True:
        a = a - b
        if a < 0:
            abs_s = abs(a)
            save_result.append(abs_s)
            break
        else:
            save_result.append(a)

    print(min(save_result))
resolve()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n,k=[int(x) for x in input().split()]
#Sequence decreases first then starts increasing.This point has to be identified.
prev,cur=n+1,n
if k<=0:
  print(n)
else:
  while cur<prev:
    prev = cur
    cur = abs(cur-k)
  print(prev)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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

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

print(min)

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 = [int(i) for i in input().split()]
if N % K == 0:
    print(0)
else:
    while True:
        x = abs(N - K)
        if x < N:
            N = x
        else:
            break
    print(N)

Submission status: Time Limit Exceeded

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

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

print(N)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
x,k=map(int,input().split())
while x>=0:
    x-=k
if x<0:
    x*=-1
y=x-k
if y <0:
    y*=-1
if x<y:
    print(x)
if x>y:
    print(y)
if x==y:
    print(x)

Submission status: Time Limit Exceeded

Solution code:
n, anoki = [int(x) for x in input().split(" ")]
x = n
found = []
if anoki == 1:
  found.append(n)
  found.append(0)
while x not in found:
  found.append(x)
  x = abs(x-anoki)
print(min(found))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

min = 10**18+1
flag = True

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

Submission status: Time Limit Exceeded

Solution code:
def C():
    nk=list(map(int,input().split()))
    
    abs_val=abs(nk[0]-nk[1])
    
    while True:
        if abs_val>nk[1]:
            abs_val=abs(abs_val-nk[1])
        elif abs_val<nk[1]:
            abs_val=min(abs_val,abs(abs_val-nk[1]))
            break
        else :
            # 0
            abs_val=abs_val-nk[1]
            break
    
    print(min(abs_val,nk[0]))
    
    
    
if __name__=='__main__':
    C()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split(" "))
min1=n
flag=1
while(flag==1):
  n=abs(n-k)
  if(n<min1):
    min1=n
    flag=1
  else:
    flag=0
print(min1)

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())
ans=[]
ans.append(n)
for i in range(n):
    ans.append(abs(ans[i]-k))
print(min(ans))

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

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

min = 10**18

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

print(min)

Submission status: Time Limit Exceeded

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

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

min = 10**18

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

print(min)

Submission status: Time Limit Exceeded

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

min = 10**18

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

print(min)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
seen = set()
minimum = n
if n % k == 0:
    print(0)
else:
    while True:
        if n in seen:
            print(minimum)
            break
        else:
            minimum = min(minimum, n)
            seen.add(n)
        n = abs(n-k)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
seen = set()
minimum = n
if k == 1:
    print(0)
elif n % 2 == 0 and k == 2:
    print(0)
else:
    while True:
        if n in seen:
            print(minimum)
            break
        else:
            minimum = min(minimum, n)
            seen.add(n)
        n = abs(n-k)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
seen = set()
minimum = n
if k == 1:
    print("0")
else:
    while True:
        if n in seen:
            print(minimum)
            break
        else:
            minimum = min(minimum, n)
            seen.add(n)
        n = abs(n-k)

Submission status: Time Limit Exceeded

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

a = n

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

def DIF(N):
  global K
  return abs(N-K)

if N < DIF(N):
  print(N)
else:
  if N % K == 0:
    print(0)
  else:
    while N >= DIF(N):
      N = DIF(N)
    print(N)

Submission status: Time Limit Exceeded

Solution code:
import sys


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

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

n_list = [n]

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

n_list.sort()
print(n_list[0])