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())
if k==1 or n%k==0:
    ans=0
else:
    if n/2<k:
        x=abs(n-k)
        y=abs(x-k)
        ans=min(n,min(x,y))
    else:
        while 1:
            x=abs(n-k)
            y=abs(x-k)
            n=min(n,min(x,y))
            if n<k:
                x=abs(n-k)
                y=abs(x-k)
                ans=min(n,min(x,y))
                break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math

sys.setrecursionlimit(500 * 500)

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

def loop():
    def loop0(x, k, acc):
        ab = abs(x - k)
        
        if ab > acc:
            return acc
        else:
            return loop0(ab, k, ab)
    
    return loop0(N, K, N)

result = loop()
print(result)

Submission status: Time Limit Exceeded

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

for i in range(n):
    x=N-m
    x=abs(x)
    N=min(N,x)

print(N)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

def cmp(a, b):
    if a > b:
        return a%b
    return abs(a-b)

while True:
    ans = cmp(N, K)
    if 0 <= ans <=2:
        break
    N = ans
print(ans)

Submission status: Time Limit Exceeded

Solution code:
array = list(map(int, input().split()))
tmp = array[0]
array[0] = abs(array[0] - array[1])
while (tmp >= array[0]):
  tmp = array[0]
  array[0] = abs(array[0] - array[1])
print(tmp)

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

Submission status: Time Limit Exceeded

Solution code:
N, X = map(int, input().split())
b = 0
if X == 1:
  print(0)
else:
  while b <= N: 
    N = abs(N - X)
    b = abs(N - X)
  print(N)

Submission status: Time Limit Exceeded

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

while N>=K:
  N = N - K

while N!=0:
  if N>=abs(N-K):
    N = N-K
    if N<0:
      N = N * -1
  elif N==0:
    break
  else:
    break            
print(N)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

bef = N
next = abs(N - K)

while bef > next:
	bef = next
	next = abs(next - K)

else:
	print(bef)

Submission status: Time Limit Exceeded

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

for i in range(18):
    if N > 10 ** i * K:
        for j in range(999):
            if N > j*10 ** i * K:
                N=N-j*10 ** i * K


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

print(N)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:

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

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

Submission status: Time Limit Exceeded

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


if n == k or k == 1:
    print(0)
    sys.exit()
    
def min_N(n, k):
    a = n
    while True:
        b = abs(a-k)
        if a < b:
            print(a)
            sys.exit()
        elif b == 1:
            print(1)
            sys.exit()
        a = b
    print(a)
    sys.exit()
    
min_N(n, k)

Submission status: Time Limit Exceeded

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

x = n
# 最初の差分だけは繰り返せるので余りを使って短縮
#if abs(n-k) > 0:
#  x %= abs(n-k)
while True:
  # 今回のループでの結果
  x2 = abs(x-k)
  if x2 > x:
    # 差が大きくなったら終了
    break
  # 計算結果が小さくなっているうちは繰り返す
  x = x2

if n > x:
  print(x)
else:
  print(n)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N, K = map(int, input().split())

def exchange(_x, _k):
  return abs(_x - _k)

a = True
x = N
ans = N

while a:
  x = exchange(x, K)
  if x < ans:
    ans = x
  else:
    a = False

# 出力
print("{}".format(ans))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
# inputs = input().split(' ')
# # # # inputs2 = input().split(' ')
# inputs=[int(a) for a in inputs]
# # # # inp2=[int(a) for a in inputs2]

inputs = [10000000000000,999999]
# inputs = [2,6]

def move(a,b):
	c=a-b
	if c < 0:
		c*=-1
	return c


a=inputs[0]
b=inputs[1]
t_f = True
slist=[a]
x=int(a/b)

while True:
	# print(x)
	if x < a:
		x=b
		break
	if x > a:
		x= x*b



# 	if x > a or x==a:
# 		break
# 	else:
# 		x=x*10
# 		# print(x)

while t_f==True:
	s = move(a,x)
	a = s
	slist.append(a)	
	
	if slist[-1] > slist[-2] or slist[-1] == slist[-2]:
		t_f = False
	# print(slist)
	# print(s)

print(min(slist))

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
if k==1 or n%k==0:
    ans=0
else:
    tp=-1
    while 1:
        x=abs(n-k)
        if tp==x:
            break
        tp=x
        y=abs(x-k)
        n=min(n,min(x,y))
    ans=min(n,min(x,y))
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

while n > abs(n-k):
    n = abs(n-k)
    if k == 1:
        n = 0
        break
    
    if n%k == 0:
        n = 0
        break

print(n)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
# inputs = input().split(' ')
# # # # inputs2 = input().split(' ')
# inputs=[int(a) for a in inputs]
# # # # inp2=[int(a) for a in inputs2]

inputs = [10000000000000,999999]
# inputs = [2,6]

def move(a,b):
	c=a-b
	if c < 0:
		c*=-1
	return c


a=inputs[0]
b=inputs[1]
t_f = True
slist=[a]
x=int(a/b)

while True:
	# print(x)
	if x < a:
		x=b
		break
	if x > a:
		x= x/b



# 	if x > a or x==a:
# 		break
# 	else:
# 		x=x*10
# 		# print(x)

while t_f==True:
	s = move(a,x)
	a = s
	slist.append(a)	
	
	if slist[-1] > slist[-2] or slist[-1] == slist[-2]:
		t_f = False
	# print(slist)
	# print(s)

print(min(slist))

Submission status: Time Limit Exceeded

Solution code:
a = [int(a) for a in input().split(' ')]
N = a[0]
K = a[1]
    
# def abs_met(N):
#     if N > abs(N - K):
#         N = abs(N - K)
#         abs_met(N)
#     else:
#         print(N)
        
# if K == 1:
#     print(0)
# else:
#     abs_met(N)

l = []
for i in range(N):
    if N > abs(N - K):
        N = abs(N - K)
        l.append(N)
if len(l) == 0:
    print(N)
else:
    print(min(l))

Submission status: Time Limit Exceeded

Solution code:
n_k = input()
n = int(n_k.split(' ')[0])
k = int(n_k.split(' ')[1])
mini = 10**18
while mini > abs(n-k):
	mini = abs(n-k)
	n = abs(n-k)
print(mini)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
# n1 = int((N/K + 0.5)//1)
# n2 = int((N/K - 0.5)//1)
# if (N/K - 0.5)%1 != 0: n2 += 1

# print(min(abs(n1*K-N),abs(n2*K-N)))
i = N
while i >= K//2:
    i = abs(i-K)
print(i)

Submission status: Time Limit Exceeded

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

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

tmp = n
for i in range(1, int(n**0.5)+1):
    tmp = min(abs(n-i*k), tmp)
print(tmp)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
 
a=list(map(int,input().split()))
#読み込み
N = a[0]
M = a[1]
b = []
 
for i in range(pow(10,17)):
    N = abs(N-M)
    b.append(N)
 
list.sort(b)
print(b[0])

Submission status: Time Limit Exceeded

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

#差の絶対値
def SanoZettai(x, y):
  s = x - y
  if s >= 0:
    return s
  else:
    return -s
  
#差の絶対値の配列
l = [N]

x = N
for i in range(N):
  x = SanoZettai(x, K)
  l.append(x)


print(min(l))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

#桁が大きい時の処理
if(K == 1):
    while(int(len(str(N))) > 2):
        N = int(N - K * (N / 2));
# else:
#     while(int(len(str(N))) > 2):
#         N = int(N / K);
        
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())
Nj = N
while True:
    Ni = Nj
    Nj = abs(Ni - K)
    if Ni > N:
        print(N)
        break
    elif Nj > Ni:
        print(Ni)
        break
    elif K == 1 or N == K:
        print(0)
        break
    else:
        continue

Submission status: Time Limit Exceeded

Solution code:
N, K = input().split(" ")
temp=0
while abs(int(N)-int(K))<=int(N):
    temp = abs(int(N)-int(K))
    N = temp
print(N)

Submission status: Time Limit Exceeded

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

while N>=K:
  N = N - K

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

Submission status: Time Limit Exceeded

Solution code:

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

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

Submission status: Time Limit Exceeded

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


if n == k or k == 1:
    print(0)
    sys.exit()
    
def min_N(n, k):
    a = n
    while True:
        b = abs(a-k)
        if a < b:
            break
        elif b == 1:
            print(1)
            sys.exit()
        a = b
    print(a)
    sys.exit()
    
min_N(n, k)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
 
a=list(map(int,input().split()))
#読み込み
N = a[0]
M = a[1]
b = []

for i in range(pow(10,18)):
    N = abs(N-M)
    b.append(N)

list.sort(b)
print(b[0])

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())
if N % K == 0:
    print(0)
else:
    li = []
    li.append(N)
    aoki = N
    while aoki <= min(li):
        aoki = abs(min(li) - K)
        li.append(aoki)
    print(min(li))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
inputs = input().split(' ')
# # # inputs2 = input().split(' ')
inputs=[int(a) for a in inputs]
# # # inp2=[int(a) for a in inputs2]

# inputs = [10000000000000,1]
# inputs = [2,6]

def move(a,b):
	c=a-b
	if c < 0:
		c*=-1
	return c


a=inputs[0]
b=inputs[1]
t_f = True
slist=[a]
x=int(a/b)
if x < a:
	x=b

# while True:
# 	if x > a or x==a:
# 		break
# 	else:
# 		x=x*10
# 		# print(x)

while t_f==True:
	s = move(a,x)
	a = s
	slist.append(a)	
	
	if slist[-1] > slist[-2] or slist[-1] == slist[-2]:
		t_f = False
	# print(slist)
	# print(s)

print(min(slist))

Submission status: Time Limit Exceeded

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

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

print(n)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
n,k = input().split(" ")
n = int(n)
k = int(k)
min = 1000000000000000001
abs0 = 0
if n == 0:
    print(0)
elif k == 1:
    print(0)
elif n == 1:
    print(1)
else:
    for i in range(1000000000):
        abs0 = abs(k - n)
        n = abs0
        if abs0 < min:
            min = abs0
        elif min == abs0:
            print(abs0)
            break

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int,input().split())
if K == 1 or N == 0 or N == K:
    print(0)
    exit()
elif K == 0:
    print(N)
    exit()
elif N == 1 or N == K-1 or K == N-1:
    print(1)
    exit()
else:
    l = []
    ans = N
    for i in range(max(N,K)):
        ans = abs(ans - K)
        l.append(ans)
        if i >= 10:
            if l[0] == l[2]:
                print(min(l))
                exit()
    print(min(l))

Submission status: Time Limit Exceeded

Solution code:
n, k = list(map(int, input().split()))
# global_min = None
results = set([n])

while True:
    # print(n)

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

    if n == 0:
        print(n)
        break

    # detect loop
    if n in results:
        print(min(results))
        break
    else:
        results.add(n)

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

    # if (not global_min) or (n < global_min):
    #     # update value iteratively
    #     global_min = n

# print('loop ended n:', n)

Submission status: Time Limit Exceeded

Solution code:
a = input()
numbers = a.split()
for i in range(len(numbers)):
    numbers[i] = int(numbers[i])
a = numbers[0]
storage = []
while len(storage) == len(set(storage)):
    a = a-numbers[1]
    if a < 0:
        a = -(a)
    storage.append(a)
storage.sort()
print(storage[0])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

if K == 1:
    N = 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())
arr = []
a = abs(N-K)
if N%K==0: print (0)
else:
    while True:
        c = abs(N-K)
        if c > a: break
        a = c
        N = c
    print(a)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

var = N % K
if var == 0:
        print(0)
elif var == K-1 or var == 1:
        print(1)
else:
        while N > K // 2:
                N = abs(N-K*(N//K))
        print(N)

Submission status: Time Limit Exceeded

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

min = N
while True:
    if K == 1:
        print(0)
        break

    N = abs(N - K)
    if  (N in num)  == True:
        print(min)
        break
    else:
        num.append(N)
        if N < min:
            min = N

Submission status: Time Limit Exceeded

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

if N % K ==0:
  print(0)
  exit()
  
ans = N
for i in range((N//K)+1):
  N = abs(N-K)
  ans =min(ans,N)
  
  
print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

now_N = 10 ** 19
while True:
  N = min( abs( N - K ), now_N )
  if now_N == N:
    break
  now_N = N
print( now_N )

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
li = []
li.append(N)
aoki = N
while aoki <= min(li):
    aoki = abs(min(li) - K)
    li.append(aoki)
print(min(li))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

if K == 1:
    N = 0

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

Submission status: Time Limit Exceeded

Solution code:
"""
青木君は任意の整数 x

に対し、以下の操作を行うことができます。

操作: x
を x と K

の差の絶対値で置き換える。

整数 N
の初期値が与えられます。この整数に上記の操作を 0 回以上好きな回数行った時にとりうる N

の最小値を求めてください。
制約
"""
n,k=map(int,input().split())
"""if n>k:
    x=n-k
else:
    x=n
"""
x=n
r=min(x,abs(x-k))
for ii in range(max(k,x)):
    x=min(x,abs(x-k))
#    print(r,x,abs(x-k))
    if r==min(x,abs(x-k)):
        break
    else:
        r=min(x,abs(x-k))

print(r)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
# n, k = map(int, "2 6".split())

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def solve(n, k):
    occurence = set()

    diff = n % k

    if diff == 0:
        print(diff)
        return 
    else:
        while n not in occurence:
            if k == 1:
                print(0)
                return

            occurence.add(n)

            n = abs(n - k)

            if n == 0:
                print(n)
                return

    min_value = min(occurence)
    print(min_value)


if __name__ == '__main__':
    nk = input().split()

    n = int(nk[0])
    k = int(nk[1])

    solve(n, k)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

if N % K ==0:
  print(0)
  exit()
  
ans = N
num = N//K
for i in range(num+1):
  N = abs(N-K)
  ans =min(ans,N)
  
  
print(ans)

Submission status: Time Limit Exceeded

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


def func(x):
    x=abs(x-k)
    while(x not in num):
        num.append(x)
        x=abs(x-k)
    return min(num)
if n==0 or k==1:
    print(0)
else:
    print(func(n))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys

N,K=map(int,input().split())
ZET=N
b=(10**18)
c=N
b1=N
for i in range(int(b)):
  if K==1:
    b1=0
    break
  if K==2:
    if N%2==0:
      b1=0
    else:
      b1=1
    break
  c=abs(b1-K)
  if int(b)<=c:
    break
  b1=c
  b=c
print(b1)

Submission status: Time Limit Exceeded

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

while judge == True:
    ans = N - K
    ans = abs(ans)
    N = ans

    ans = N - K
    ans = abs(ans)
    mem = ans
    if N > mem:
        pass
    else:
        judge = False


print(N)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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


while N!=0:
  if N>=abs(N-K):
    N = abs(N-K)
  elif N==0:
    break
  else:
    break
            
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()
while abs(n-k)<n:
    n=abs(n-k)
    
    if n==0:
        print(n)
        sys.exit()
else:
    print(n)

Submission status: Time Limit Exceeded

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

if n == k:
  print(0)
  exit()
  
if n > k:
  if n%k == 0:
    print(0)
    exit()
  else:
    m = n
    while m > 0:
      m -= k
    if abs(m) < n%k:
      print(-m)
      exit()
    else:
      print(n%k)
      exit()

if n < k:
  if n > abs(n-k):
    print(abs(n-k))
    exit()
  else:
    print(n)
    exit()

Submission status: Time Limit Exceeded

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

n = int(n)
k = int(k)
temp = float("inf")
while 1:
    ans = abs(n - k)
    if n < ans:
        break
    n = ans
    
print(n)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

var = N % K
if var == 0:
        print(0)
elif var == K-1 or var == 1:
        print(1)
else:
        while N > math.ceil(K / 2):
                N = abs(N-K)
        print(N)

Submission status: Time Limit Exceeded

Solution code:
import fractions
N, K = map(int,input().split())
N = fractions.gcd(N,K)
min = N
for i in range(10**8):
    N = abs(N-K)
    if min > N:
        min = N
    if N == 0:
        min = 0
        break
print(min)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys

N,K=map(int,input().split())
ZET=N
b=(10**18)
c=N
b1=N
for i in range(int(b)):
  if K==1:
    b1=0
    break
  c=abs(b1-K)
  if int(b)<=c:
    break
  b1=c
  b=c
print(b1)

Submission status: Time Limit Exceeded

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

while abs(N-K)<N:
    N=abs(N-K)
    nums.append(N)
print(min(nums))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
A=[N]
for i in range(0,10**18):
  A.append(abs(A[i]-K))
  if A[i-1]>A[i] and A[i]<A[i+1]:
    break
print(A[i])

Submission status: Time Limit Exceeded

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

def gcd(a,b):
  a, b = max(a, b), min(a, b)
  while b!=0:
    a,b=b,a%b
  return a

def gcd2(a, b):
  while True:
    if a > b:
      if a % b == 0:
        return 0
      else:
        a = abs(a-b)
    elif a == b:
      return 0
    else:
      return min(a, abs(a-b))

ans = gcd2(N, K)
if ans == K:
  print(0)
else:
  print(ans)

"""
if N < K:
  print(min(N, abs(N-K)))
elif N == K:
  print(0)
else:
  ans = gcd(N, K)
  if ans == K:
    print(0)
  else:
    print(ans)
"""

Submission status: Time Limit Exceeded

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

var = N % K
if var == 0:
        print(0)
elif var == K-1 or var == 1:
        print(1)
else:
        while N > math.ceil(K // 2):
                N = abs(N-K)
        print(N)

Submission status: Time Limit Exceeded

Solution code:
nk = input().split(" ")
n = int(nk[0])
k = int(nk[1])
if n >= 0 and n <= 1000000000000000000 and k >= 1 and k <= 1000000000000000000:
    if n % k != 0:
        temp = abs(n - k)
        output = list()
        value = 0
        output.append(temp)
        n = temp
        while temp != value:
            value = abs(n - k)
            output.append(value)
            n = value
        print(min(output))
    else:
        print(0)
else:
    print()

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def solve():
    N, M = map(int, input().split())
    
    if M == 1:
        return 0
    
    n = N
    c = 0
    while True:
        n = n-M
        c = (abs(n) - M)
        
        if c <= 0:
            break

    return min(abs(n), abs(c))

print(solve())

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

print(mn)

Submission status: Time Limit Exceeded

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

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

second = N + K

N = abs(N)

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

list_a = sorted(list_a)

print(list_a[0])

Submission status: Time Limit Exceeded

Solution code:
def solve(n, k):
    occurence = set()

    while n not in occurence:
        if k == 1:
            print(0)
            return

        occurence.add(n)

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

    min_value = min(occurence)
    print(min_value)


if __name__ == '__main__':
    nk = input().split()

    n = int(nk[0])
    k = int(nk[1])

    solve(n, k)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys

N,K=map(int,input().split())
ZET=N
b=(10**18)
c=N
b1=N
for i in range(int(b)):
  if K==1:
    b1=0
    break
  c=abs(b1-K)
  if int(b)<c:
    break
  b1=c
  b=c
print(b1)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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