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: Wrong Answer

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

Submission status: Wrong Answer

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

d = int(N/K)
if d == 0:
    print N
else:
    if N%K == 0:
        print abs(N - K*d)
    else:
        print abs(N - K*(d+1))

Submission status: Wrong Answer

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

MOD = 10**9
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors

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

if N < K:
    print(N)
else:
    amari1 = N//K
    amari2 = N//K+1
    ans = min(abs(N-amari1*K), abs(N-amari2*K))
    print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import fractions
N,K = map(int, input().split())
if K == 1 or N == 0:
  print(0)
else:
  print(fractions.gcd(N,K))

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
m = int(n/k)
pos_min = n - m*k
min_one = abs(pos_min -k)
print(min(pos_min,min_one))

Submission status: Wrong Answer

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

a=int(n/k)
b=n-k*a
c=abs(n-k*(a+1))
print(min(b,c))

Submission status: Wrong Answer

Solution code:
N,K=list(map(int,input().split()))
num=int(N/K)
a=N-K*num
b=N-K*(num+1)
if(a<0):
  a*=-1
if(b<0):
  b*=-1
if(a<b):
  print(a)
else:
  print(b)

Submission status: Wrong Answer

Solution code:
import math

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

a = math.ceil(n/k)
b = math.floor(n/k)

print(min(abs(n-(k*a)),abs(n-(k*b))))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
if n<k:
    print(n)
else:
    print(min(n%k,k-(n%k)))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
if N >= K:
    print(N%K)
else:
    print(max(N,K-N))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if N < K and abs(N-K) > N:
  print(N)
else:
  print(N % K)

Submission status: Wrong Answer

Solution code:
# 最大公約数，最小公倍数
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


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

if k == 1 or n == 1:
    print(0)
else:
    print(gcd(k, n))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
if n < k:
    print(n)
elif n == k:
    print(0)
else:
    val = n // k
    ans = n - k * val
    ans = min(ans, abs(n-(k*(val+1))))
    print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

x = gcd(max(k, n), min(k, n))
if x == k:
    print(0)
else:
    print(x)

Submission status: Wrong Answer

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

if n % k == 0:
    print("0")
else:
    sub = abs(n - k)
    print(n % sub)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

print(min(abs(N-K*int(N/K)),abs(N-K*math.ceil(N/K)),N))

Submission status: Wrong Answer

Solution code:
# 最大公約数
def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a


N, K = map(int, input().split())
if K == 1:
    print(0)
    exit()
print(gcd(N, K))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import fractions
N,K = map(int, input().split())
if K == 1:
  print(0)
else:
  print(fractions.gcd(N,K))

Submission status: Wrong Answer

Solution code:
from fractions import *

N, K = map(int, raw_input().split())
print gcd(N, K) if N>0 else 0

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
from fractions import gcd
ans = gcd(N,K)


print(ans)
#print(*ans,sep=' ')

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def gcd(p, q):
    if p < q:
        p, q = q, p
    while q > 0:
        p, q = q, p%q
    return p


n, k = map(int, input().split())
if n%k == 0:
  print(0)
else:
  print(gcd(n, k))

Submission status: Wrong Answer

Solution code:
from fractions import gcd
def main():
    n, k = map(int, input().split())
    x = gcd(n, k)
    if x == k:
        print("0")
    else:
        print(x)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
from fractions import gcd
g=gcd(N,K)
print(0 if g==K else g)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
mod = n % k
if mod < k:
    print(k-mod)
else:
    print(mod)

Submission status: Wrong Answer

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a%b)

def main():
    n,k = inpm()
    if n == k or n%k == 0:
        print(0)
        return
    print(gcd(n,k))
    
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = n//k
b = n%k
if a==0:
    print(b)
else:
    print(min(a, b))

Submission status: Wrong Answer

Solution code:
def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a

#import sys
#input = sys.stdin.readline
def main():
    N, K = map( int, input().split())
    if N > K:
        if N%K == 0:
            print(0)
            return
    print( gcd(K,N))
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

while K != 0:
    N,K = K,N%K

print(N)

Submission status: Wrong Answer

Solution code:
import math

from sys import exit, setrecursionlimit
setrecursionlimit(10**6)

def array2D(row, col ,val=0):
    return [[val]*col for i in range(row)]

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

a1 = x//k
a2 = math.ceil(x/k)
print(min(x, abs(x-a1*k), abs(x-a2*k)))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
from fractions import gcd

ans = gcd(N,K)
if K == 1: ans = 0
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
INF = float("inf")


def GCD(a, b):
    if a == 0:
        return b
    else:
        return GCD(b % a, a)


def solve(N: int, K: int):
    gcd = GCD(N, K)
    if gcd == K:
        print(0)
    else:
        print(gcd)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    solve(N, K)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
from math import ceil
N, K = map(int, input().split())
v = ceil(N / K)
ans = abs(N - v*K)
print(min(N, ans))

Submission status: Wrong Answer

Solution code:
def gcd(a,b):
  if b==0:
    return a
  else:
    return gcd(b,a%b)

n,k=map(int,input().split())
g=gcd(n,k)
if k==1:
  print(0)
else:
  print(g)

Submission status: Wrong Answer

Solution code:
from fractions import gcd


def main():
    n, k = map(int, input().split())
    print(gcd(n, k) if 1 < k else 0)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()

if K == 1:
	print(0)
else:
	print(gcd(N, K))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def gcd(x, y):
    while y:
        x, y = y, x % y
    return x

if K == 1:
    print(0)

else:
    print(gcd(N, K))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
def f(x,y):
    if x<y:x,y = y,x
    if y==0:return x
    return f(y,x%y)
gcd = f(N,K)
if N%K==0:
    print(0)
else:
    print(gcd)

Submission status: Wrong Answer

Solution code:
import sys
from fractions import gcd
input = sys.stdin.readline
N, K = map(int, input().split())
g = gcd(N, K)
if N % K: print(g)
else: print(0)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
N,K=map(int, input().split())
ans=gcd(N,K)
if K==1 or N==0:
    ans=0
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=list(map(int, input().split()))
x=n//k
print(abs(n-k*x))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

n,k=mii()

print(n%k)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

print(n % k)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
# K > 1 かつ　N > Kの場合は、N - N // K * K をスタートにできる
if K > 1:
  N = N - N // K * K
else:
  N = 0
minimum = N
count = 0
#print(N)
while count <= N / 4:
  N = abs(N - K)
  minimum = min(N, minimum)
  count += 1
print(minimum)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
# K > 1 かつ　N > Kの場合は、N - N // K * K をスタートにできる
if K > 1:
  N = N - N // K * K
else:
  N = 0
minimum = N
count = 0
#print(N)
while count < N:
  N = abs(N - K)
  minimum = min(N, minimum)
  count += 1
print(minimum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# import math
# import statistics
# import itertools
# a=int(input())
# b=input()
# c=[]
# for i in a:
#     c.append(int(i))
N,K= map(int,input().split())
# f = list(map(int,input().split()))
# g = [int(input()) for _ in range(N)]
# h = []
# for i in range(a):
#     h.append(list(map(int,input().split())))
# a = [[0] for _ in range(H)]#nizigen
mi=N
count=0
while True:
    if N%K==0:
        print(0)
        exit()
    N=abs(N-K)
    if N<=mi:
        mi=min(mi,N)
        count+=1
    else :
        print(mi)
        exit()

Submission status: Time Limit Exceeded

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

while n >= 0:
    n = n - k
    if n >= 0:
        min = n
    else:
        fin = abs(n)

if min < fin:
    print(min)
else:
    print(fin)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys

n,k = map(int,input().split())
x = k+(n%k)
x = n
min = sys.maxsize

while True:
    if x%k == 0:
        min = 0
        break

    x = abs(x-k)
    if x == min :break
    elif x < min : min = x

print(min)

Submission status: Time Limit Exceeded

Solution code:
import sys

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

while True:
    if x%k == 0:
        min = 0
        break

    x = abs(x-k)
    if x == min :break
    elif x < min : min = x

print(min)

Submission status: Time Limit Exceeded

Solution code:
n,k =map(int,input().split())
def p(n):
        if n > abs(n-k):
                return True
        else:
                return False
while p(n):
        n = abs(n-k)

print(n)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
ans = 10**10
while True:
    temp = N % K
    if temp < ans:
        ans = temp
    elif temp == ans:
        break

ans = min(abs(ans-K), ans)
print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
i=1
while True:
  if n-i*k<=0:
    m=min((i*k-n),(n-(i-1)*k))
    print(m)
    break
  i+=1

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
def manipulate(x, k):
	return abs(x - k)
	

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

while n > k / 2:
	if k == 1:
		n = 0
		break
		
	n = manipulate(n % k, k)
	
print(n)

Submission status: Time Limit Exceeded

Solution code:
def manipulate(x, k):
	return abs(x - k)
	

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

while n > k / 2:
	if k == 1:
		n = 0
		break
		
	if k == 0:
		break
		
	n = manipulate(n, k)
	
	
print(n)

Submission status: Time Limit Exceeded

Solution code:
def manipulate(x, k):
	return abs(x - k)
	

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

while n > k / 2:
	if k == 1:
		n = 0
		break
		
	n = manipulate(n, k)
	
	
print(n)

Submission status: Time Limit Exceeded

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

def operation(x, k):
    return abs(x - k)

while True:
    nn = operation(n, k)
    if nn >= n:
        ans = n
        break
    else:
        n = nn

print(ans)

Submission status: Time Limit Exceeded

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

n = x[0]
k = x[1]
y = n

while True:
    temp_y = y

    if temp_y % k == 0:
        y = 0
        break

    y = abs(y - k)
    if temp_y < y:
        y = temp_y
        break
    else:
        continue

print(y)

Submission status: Time Limit Exceeded

Solution code:
def main():
  N, K = map(int,input().split())
  sett = set()
  ans = float('inf')
  if N%K==0:
    print(0)
    return
  while N not in sett:
    sett.add(N)
    N = abs(N-K)
    ans = min(ans,N)
  print(ans)
  return 
main()

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int,input().split())
sett = set()
ans = float('inf')
while N not in sett:
  sett.add(N)
  N = abs(N-K)
  ans = min(ans,N)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, k = map(int, input().split())
    minv = n
    for _ in range(100000000):
        if abs(minv - k) < abs(minv):
            minv = abs(minv - k)
        else:
            print(minv)
            exit()

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N, K = list(map(int, input().split()))
if N >= K:
  ans = N
  for i in range(int(N/K)):
    ans = ans - K
  print(min(ans, max(ans-K, K-ans)))
else:
  print(min(N, max(N-K, K-N)))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
dic = defaultdict(int)
n,k = readInts()
if k == 1:
    print(0)
elif n == 0:
    print(0)
elif n == 1:
    print(1)
else:
    while True:
        if dic[n]:
            break
        else:
            dic[n] = 1
            n = abs(n-k)
    m = float('inf')
    for k,v in dic.items():
        m = min(m,k)
    print(m)

Submission status: Time Limit Exceeded

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

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

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

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

if K==1:
  print(0)
  exit()
  
s = N
for i in range(10**9):
  s = min(s,abs(N-K))
  N -= K
print(s)

Submission status: Time Limit Exceeded

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

if K==1:
  print(0)
  exit()
  
s = N
for i in range(10**18):
  s = min(s,abs(N-K))
  N -= K
print(s)

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:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n, k = map(int, input().split())
    r = n
    nums = set()
    while n not in nums:
        r = min(r, n)
        nums.add(n)
        n = abs(n - k)
    print(r)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n, k = map(int, input().split())
    r = n
    nums = set()
    while True:
        n = abs(n - k)
        if n in nums:
            break
        else:
            r = min(r, n)
            nums.add(n)
    print(r)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

x = N

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

Submission status: Time Limit Exceeded

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

if N == 0 or K == 1:
    N = 0
elif N == K:
    N = 0
elif N % K == 0:
    N = 0
else:
    while N != 0:
        m = abs(N - K)
        if N < m:
            break
        else:
            N = m

print(N)

Submission status: Time Limit Exceeded

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

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

current = N

while True:
    prev = current
    current = abs(current - K)
    next = abs(current - K)
    if prev == next:
        print(min(current, next))
        break

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

i = 0

while True:
    if (N+i) % K == 0 or (N-i) % K == 0:
        break
    i += 1

print(i)

Submission status: Time Limit Exceeded

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

if K == 1:
  res = 0
elif K == 2:
  res = K % 2
else:
  diff = abs(N-K)
  while diff < N:
    N  = diff
    diff = abs(N-K)
 
  res = N
  
  
print(res)

Submission status: Time Limit Exceeded

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

if n%k==0:
  mi = 0
  loop = False

while loop:
  loop = False
  if mi > abs(n-k):
    loop = True
    n = abs(n-k)
    mi = n

print(mi)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int, input().split())
if N % K == 0:
    print(0)
elif N > K:
    while N > 0:
        hako1 = N
        N -= K
        hako2 = abs(N)
    if hako1 > hako2:
      print(hako2)
    else:
      print(hako1)
else:
    if N >= abs(N-K):
        print(abs(N-K))
    else:
        print(N)

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
A=0
for a in range(100000000000000):
    if N%K==0:
        print(0)
        break
    if N<K:
        if abs(N-K)<N:
            print(abs(N-K))
            break
        else:
            print(N)
            break
    tugi=abs(N-K)
    N=tugi
    A=tugi
    tugi=abs(N-K)
    B=tugi
    N=tugi
    tugi=abs(N-K)
    if A==tugi:
        if B<A:
            print(B)
            break
        else:
       	    print(A)
            break

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
A=0
for a in range(100000000000000):
    tugi=abs(N-K)
    N=tugi
    A=tugi
    tugi=abs(N-K)
    B=tugi
    N=tugi
    tugi=abs(N-K)
    if A==tugi:
        if B<A:
            print(B)
            break
        else:
       	    print(A)
            break

Submission status: Time Limit Exceeded

Solution code:
n, k = list(map(int, input().split()))
loop = True
while loop:
    if n > abs(n - k):
        if n > k:
            n = min(n, n % k)
        elif k > n:
            n = min(n, k % n)
    else:
        loop = False

print(n)

Submission status: Time Limit Exceeded

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

newn = abs(n-k)

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

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
setlist = set()
tmp = N
ans = N
setlist.add(tmp)
while True:
  tmp = abs(tmp-K)
  ans = min(tmp,ans)
  if tmp in setlist:
    break
  setlist.add(tmp)
print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N, K = input().split(' ')
N0 = int(N)
K = int(K)
N1 = abs(N0-K)
while N1 < N0:
  N0 = N1
  N1 = abs(N1-K)
else:
  print(N0)

Submission status: Time Limit Exceeded

Solution code:
N, K = input().split(' ')
N = int(N)
K = int(K)
N1 = abs(N-K)
NB = [N, N1]
while NB[-1] < NB[-2]:
  NN = abs(NB[-1]-K)
  NB.append(NN)
else:
  print(NB[-2])

Submission status: Time Limit Exceeded

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

while True:
  if K == 1:
    answer = 0
    break
  if N <= abs(N - K):
    answer = N
    break
  else:
    N = abs(N - K)

print(answer)

Submission status: Time Limit Exceeded

Solution code:
def abc161_C(N,K):
    if K == 1:
        return print(0)
    Min = 0
    diff_list = [N]
    while 1:
        diff = abs(N - K)
        N = diff
        if diff in diff_list:
            return print(min(diff_list))
        elif diff < K/2:
            diff_list.append(diff)
            return print(min(diff_list))
        else:
            diff_list.append(diff)
    
N,K = map(int,input().split())
abc161_C(N,K)

Submission status: Time Limit Exceeded

Solution code:
def abc161_C(N,K):
    if K == 1:
        return print(0)
    Min = 0
    diff_list = [N]
    while 1:
        diff = abs(N - K)
        N = diff
        if diff in diff_list:
            return print(min(diff_list))
        elif diff == 0:
            return print(0)
        else:
            diff_list.append(diff)
            
    
N,K = map(int,input().split())
abc161_C(N,K)

Submission status: Time Limit Exceeded

Solution code:
def abc161_C(N,K):
    if K == 1:
        return print(0)
    Min = 0
    diff_list = [N]
    while 1:
        diff = abs(N - K)
        N = diff
        if diff in diff_list:
            return print(min(diff_list))
        else:
            diff_list.append(diff)
            
    
N,K = map(int,input().split())
abc161_C(N,K)

Submission status: Time Limit Exceeded

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

if K == 1:
    print(0)
    sys.exit()
while True:
    x = abs(N - K)
    if x > N:
        break
    N = min(x, N)
print(N)

Submission status: Time Limit Exceeded

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

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

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

print(n)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

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

print(n)

Submission status: Time Limit Exceeded

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

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

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

print(n)

Submission status: Time Limit Exceeded

Solution code:
n,k = [int(i) for i in input().split()]
temp = 0
cont = 0
a = 0
if n%k==0: print(0)
elif k%n==0: print(n)
else:
    while True:
        n = abs(n-k)
        if temp <= n:
            a=temp
            cont+=1
        if cont>=1e3: break
        temp=n
    
    print(temp)