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: Runtime Error

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

# inputs = [1000,10]
# 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/b < b:
		break
	else:
		x=x*b



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

while t_f==True:
	s = move(x,b)
	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: Runtime Error

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

Submission status: Runtime Error

Solution code:
N, K = (int(x) for x in input().split())
x = N
memo = {}

if N == K:
    x = 0
elif N > K and N % K == 0:
    x = 0
elif N < K and K % N == 0:
    x = N
else:
    last = N
    for n in range(10 ** 18):
        x = abs(last - K)
        if x not in memo:
            last = x
            memo[n] = last
        else:
            break
            print(last)

Submission status: Runtime Error

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

prev = None

n = n % k
print(min(n, k % n))

Submission status: Runtime Error

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

# inputs = [1000,10]
# 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/b < b
		break
	else:
		x=x*b



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

while t_f==True:
	s = move(x,b)
	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: Runtime Error

Solution code:
N, K = map( int, input().split() )
 
now_N = 10 ** 19
while True:
  N = min( abs( N % K ),abs( K - N ),, now_N )
  if now_N == N:
    break
  now_N = N
print( now_N )

Submission status: Runtime Error

Solution code:
k=int(input(''))
r=[]
def detr(n):
    sn=str(n)
    j=0
    for a in sn:
        j=j+1
    
    if j==1:
        return True

    ok=1
    for a in range(j-1):
        if int(sn[a])-int(sn[a+1])==0 or int(sn[a])-int(sn[a+1])==1 or int(sn[a])-int(sn[a+1])==-1:
            ok=ok*1
        else:
            ok=ok*0
            break
    
    if ok==1:
        return True
    else:
        return False

lis=[]
x=1
c=0
while len(lis)<k:
    if detr(x)==True:
        lis.append(x)
        if x>=10:
            c=c+1
    
    if c<=2:
        x=x+1
    else:
        c=0
        x=x+9

print(lis[-1])

Submission status: Runtime Error

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

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

def loops (x, K, mins):
    if x < K:
        ab = swap(x, K)
        return min(ab, x)
    elif x % K == 0:
        return 0
    else:
        x = swap(x, K)
        if x < mins:
            mins = x
            x = loops(x, K, mins)
            return x
        else:
            return mins

N, K = getInput()
x = N
print(loops(x, K, x))

Submission status: Runtime Error

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

inputs = [1000,10]
# 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/b < b
		break
	else:
		x=x*b



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

while t_f==True:
	s = move(x,b)
	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: Runtime Error

Solution code:
import numpy as np
import sys

tmp = list(map(int, input().split()))

N = tmp[0]
K = tmp[1]


# i = N % K

if N < K:
    print(N)
elif N == K:
    print(0)    
else:



    solve = [0] * K

    for i in range(0, K-1):
        solve[i] = N % (K - i)

    solve.reverse()

    for iter in range(0,K-1):
        if solve[iter] == 0:
            continue
        else:
            print(solve[iter])
            break
    print(0)




    # print(solve[1])


    # if K == 1:
    #     print(solve[0])
    # else:
    #     print(solve[1])

Submission status: Runtime Error

Solution code:
import sys
import math

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

if N < K:
    print(N)
elif N == 0:
    print(0)
elif N == K:
    print(0)
else:
    a = math.floor(N / K)

    ans1 = abs(N - (K * a))
    ans2 = abs(N - (K * (a + 1)))
    ans3 = abs(N - (K * (a - 1))

    print(min(ans1, ans2, ans3))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

ave = sum(A)/N

c = 0
for i in range(N):
    if A[i] >= ave:
        c += 1

if c >= M:
    print "Yes"
else:
    print "No"

Submission status: Runtime Error

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

print(min(n % k, k % n))

Submission status: Runtime Error

Solution code:


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

if N > K:
    N = N - K*(N//K)

if N=K:
    print(0)
else:
    print(K-N)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
if n % k ==0:
    print(0)
else:
    l = [n]
    p = n//k
    c = n-(p*k)
    half = k//2
    while c > half
        c = abs(c-k)
    l.append()
    print(min(l))

Submission status: Runtime Error

Solution code:
N =int(input())
K = int(input())
if N>K:
    while N>0:
        N=N-K
        if N<=0:
            print(abs(N))
else:
    print(N)

Submission status: Runtime Error

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

def func(n, k):
    if n == k:
        return print(0)
    elif n > k:
        if n % k == 0:
            return print(0)
        else:
            func(n%k, k)
    else:
        if k % n == 0:
            print((k-n)//n)
        else:
            print(k//n)

func(n, k)

Submission status: Runtime Error

Solution code:
import sys


def get():
    return list(map(int, input().split()))


a = get()
b = a[0]
c = a[1]
if b < c:
    if abs(b - c) < b:
        print(abs(b - a))
        sys.exit(0)
    else:
        print(b)
        sys.exit(0)
else:
    if a[0] % a[1] == a[1] or a[0] % a[1] == 0:
        print(0)
        sys.exit(0)
    else:
        if abs(a[0] % a[1] - a[1]) < abs((a[0] % a[1] - a[1]) % a[1]):
            if abs(a[0] % a[1]) < abs(a[0] % a[1] - a[1]):
                print(abs(a[0] % a[1]))
                sys.exit(0)
            else:
                print(abs(a[0] % a[1] - a[1]))
                sys.exit(0)
        else:
            if abs(a[0] % a[1]) < abs((a[0] % a[1] - a[1]) % a[1]):
                print(abs(a[0] % a[1]))
                sys.exit(0)
            else:
                print(abs((a[0] % a[1] - a[1]) % a[1]))
                sys.exit(0)

Submission status: Runtime Error

Solution code:
n, m = (int(x) for x in input().split())
num_point = [int(x) for x in input().split()]
count = 0

for n in num_point:
    if n > (sum(num_point) / (4 * m)):
        count += 1

if count < m:
    print("No")
else:
    print("Yes")

Submission status: Runtime Error

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:
    border = math.floor(N / K);
    N = N - K * border;
        
flag = True;
while flag == True:
    N = abs(N - K);
    if(N <= min):
        min = N;
    else:
        flag = False;
    
print(min);

Submission status: Runtime Error

Solution code:
a,b=input().split()
a=int(a)
b=int(b)
y=[x]
f=0
for i in range (10**18):
    x=b-a
    if x==0:
        break
    elif x<0:
        y.append(x*(-1))
        a=x*(-1)
    else :
        y.append(x)
        a=x
    if i>0:
        if int(y[i-1]) <= int(y[i+1]) and int(y[i-1]) <=int(y[i+1]):
            f=y[i-1]
            break

print(f)

Submission status: Runtime Error

Solution code:
n,m=map(int,input())
N=n
x=0
for i in range(n):
    x=N-m
    x=abs(x)
    N=min(n,N,x)

print(N)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
a,b=input().split()
a=int(a)
b=int(b)
y=[x]
f=0
for i in range (10**18):
    x=b-a
    if x==0:
        break
    elif x<0:
        y.append(x*(-1))
        a=x*(-1)
    else :
        y.append(x)
        a=x
    if i>0:
        if int(y[i-1]) <= int(y[i+1]):
            f=y[i-1]
            break

print(f)

Submission status: Runtime Error

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

c=abs(n-k)
d=abs(c-k)
if(c+d==k):
    if(c>d):
        print(d)
    else:
        print(c)

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

if(n>k):
    a=n-k*(n//k)
    b=k*(n//k+1)-n
    if(a>b):
        print(b)
    else:
        print(a)

Submission status: Runtime Error

Solution code:
def get():
    return list(map(int, input().split()))


a = get()
b = a[0]
c = a[1]
if b < c:
    if abs(b - c) < b:
        print(abs(b - a))
    else:
        print(b)
else:
    if a[0] % a[1] == a[1] or a[0] % a[1] == 0:
        print(0)
    else:
        if abs(a[0] % a[1] - a[1]) < abs((a[0] % a[1] - a[1]) % a[1]):
            if abs(a[0] % a[1]) < abs(a[0] % a[1] - a[1]):
                print(abs(a[0] % a[1]))
            else:
                print(abs(a[0] % a[1] - a[1]))
        else:
            if abs(a[0] % a[1]) < abs((a[0] % a[1] - a[1]) % a[1]):
                print(abs(a[0] % a[1]))
            else:
                print(abs((a[0] % a[1] - a[1]) % a[1]))

Submission status: Runtime Error

Solution code:
a,b=input().split()
a=int(a)
b=int(b)
y=[x]
f=0



for i in range (10**18):
    x=b-a
    if x==0:
        break
    elif x<0:
        y.append(x*(-1))
        a=x*(-1)
    else :
        y.append(x)
        a=x
    if i>0:
        if int(y[i-1]) <= int(y[i+1]):
        f=y[i-1]
        break

print(f)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

while N-K >= 0:
	num1 = N-K
	N = N-K
    
num2 = abs(num1-K)
if num1 < num2:
  print(num1)
else:
  print(num2)

Submission status: Runtime Error

Solution code:
n, k = (int(each) for each in input().split())
if n > k:
    a = n%k
    print(min(abs(a-k), a))
else:
    k = k%n+n+n
    b = min(abs(n-k), k-abs(n-k))
    if b == 0:
        print(n)
    else:
        print(b)

Submission status: Runtime Error

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

if n < k:
  print(n)
else:
  temp = int(n/k)
  remain = n - (k*temp)
  if remain > 0:
    last = abs(remain-k)
  	print(min(remain, last))
  else:
  	print(remain)

Submission status: Runtime Error

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

x = n
# 最初の差分だけは繰り返せるので余りを使って短縮
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: Runtime Error

Solution code:
N,K =  map(int, input().split())
i=1
A[0]=N
if K!=1:
    while True:
        A[i]=abs(A[i]-K)
        if A[i] in A:
            break
        i+=1
    print(min(A))
else:
    print(0)

Submission status: Runtime Error

Solution code:
import numpy as np
import fractions
N, K = [int(i) for i in input().split(' ')]

if K % N == 0:
	result = N
elif N % K == 0:
	result = 0
elif fractions.gcd(N, K) == 1 and N > K:
	result = 1
elif fractions.gcd(N, K) == 1 and K > N:
	result = K-N if N > K - N else N
else:
  result = fractions.gcd(N, K)
  
print("{}".format(result))

#lis = np.array([int(n) for n in input().split(' ')])
#print("{} {}".format(sum(lis) / 4 / int(M), lis[int(M)-1]))

#count = sum(sum(lis) / 4.0 / int(M) <= lis)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split(' '))
if 2 * N < K:
    return N
else:
    a = N // K
    mm = min(abs(N - a * K), abs(N - (a + 1) * K))
    return mm

Submission status: Runtime Error

Solution code:
import numpy as np

tmp = list(map(int, input().split()))

N = tmp[0]
K = tmp[1]


# i = N % K

solve = [0] * K

for i in range(0, K-1):
    solve[i] = N % (K + i)

solve.reverse()

if K == 1:
    print(solve[0])
else:
    print(solve[1])

Submission status: Runtime Error

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

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

cnt = gcd(n, k)

if n == 1 or k == 1:
    print(0)
else:
    a = max((n % k, k % n))
    print(min(a, gcd(n, k)))

Submission status: Runtime Error

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

def okikae(t, k):
    if k==0:
        print(t)
    elif t%k==0:
        print("0")
    elif int(k/2) >= t:
        print(t)
    else:
        okikae(abs(t-k), k)

okikae(N, K)

Submission status: Runtime Error

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

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

def loops (x, K):
    if x < K:
        ab = swap(x, K)
        return min(ab, x)
    elif x % K == 0:
        return 0
    else:
        x = swap(x, K)
        x = loops(x, K)
        return x

N, K = getInput()
x = N
print(loops(x, K))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
if min(n, k) ==1:
    print("0")
else:
    if n < k:
        n, k = k, n
    while n % k != 0:
        n, k = k, n % k
    print(k)

Submission status: Runtime Error

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

if k == 1 or n == 0:
    print(0)
    exit()
print(math.gcd(n, k))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
a,b=input().split()
a=int(a)
b=int(b)
y=[x]
f=0
for i in range (10**18):
    x=b-a
    if x==0:
        break
    elif x<0:
        y.append(x*(-1))
        a=x*(-1)
    else :
        y.append(x)
        a=x
    if int(y[i+1]) <= int(y[i+2]):
        f=y[i+1]
        break

print(f)

Submission status: Runtime Error

Solution code:
a=list(map(int, input().split()))
l=[a[0]]
if a[0]==1:
    print(0)
    exit()
elif a[0]==0:
    print(0)
    exit()
elif a[1]==1:
    print(0)
    exit()
def d(a,b):
    if a<b:
        if min(l)>b-a:
            l.append(b-a)
            d(b-a,b)
        else:
            print(min(l))
            exit()
    elif b<a:
        if min(l)>a-b:
            l.append(a-b)
            d(a-b,b)
        else:
            print(min(l))
            exit()
    else:
        print(0)
        exit()
d(a[0],a[1])

Submission status: Runtime Error

Solution code:
N,K = list(map(int,input().split()))
result = []
if N > K:
    N = N % K
else :
    K = K % N

for i in range(4):
    N = abs(N-K)
    result.append(N)
while (1):
    if(result[-1] == result[-3]):
        if(result[-2] == result[-4]):
            break
    N = abs(N-K)
    result.append(N)
print(min(result))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
a=list(map(int, input().split()))
l=[a[0]]
if a[0]==1:
    print(0)
    exit()
elif a[0]==0:
    print(0)
    exit()
elif a[1]==1:
    print(0)
    exit()
def d(a,b):
    if a<b:
        if min(l)>b-a:
            l.append(b-a)
            print(l)
            d(b-a,b)
        else:
            print(min(l))
            exit()
    elif b<a:
        if min(l)>a-b:
            l.append(a-b)
            print(l)
            d(a-b,b)
        else:
            print(min(l))
            exit()
    else:
        print(0)
        exit()
d(a[0],a[1])

Submission status: Runtime Error

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

Submission status: Runtime Error

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

x = n
x %= abs(n-k)
# もう一回ｍが出てきたら終了
m = abs(n-k)
while True:
  # 最小値を比較して更新
  if m > abs(x-k):
    m = abs(x-k)
  if abs(x-k) > x:
    # 差が大きくなったら終了
    break
  else:
    # 最小値が1回目の場合はxを更新して続行
    x = abs(x - k)

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

Submission status: Runtime Error

Solution code:
def main():
  n, k = map(int, input().rstrip().split())
  
  if n % k <= k / 2:
	print(n % k)
  else:
    print(k - (n % k))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split(' '))
if 2N < K:
    print(N)
else:
    a = N // K
    print(min(abs(N - a * K), abs(N - a * (K + 1))))

Submission status: Runtime Error

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

k = int(N/K)
if (N - K*k) > abs(N - K*(k+1)):
    print(abs(N - K*(k+1))
else:
    print(N - K*k)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
import sys

def main():

	p = -1
	m = 1
	N,K = map(int, input().split())
	
	div = int(N/K)
	N = N/div*K
	int(N)
	while m >= 0 and p <= 0:
		if N >= 0 and N-K <= 0:
			m = N-K
		if N <= 0 and N-K >= 0:
			p = N-K
		N = N-K
	
	if abs(m) < p:
		N = abs(m)
	else:
		N = abs(p)
	
	print(abs(N))
	
if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

k = int(N/K)
if (N - K*k) > abs(N - K*(k+1)):
    print(abs N - K*(k+1))
else:
    print(N - K*k)

Submission status: Runtime Error

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
        if i in range (0, 1000000):
            value = abs(n - k)
            output.append(value)
            n = value
        print(min(output))
    else:
        print(0)
else:
    print()

Submission status: Runtime Error

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

flag=0
if(n>k):
    flag=1
    temp=n
    n=k
    k=temp

if(k%n==0):
    if(flag==1):
        print(0)
    else:
        print(n) 
    exit()


a=int(k/n)
b=a+1
if(flag==1):
    if(k-n*a<=n*b-k):
        print(k-n*a)
    else:
        print(n*b-k)
else:
    c=abs(n-k)
    d=abs(c-k)
    if(c>d):
        print(d)
    else:
        print(c)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split(' '))
if 2N < K:
    return N
else:
    a = N // K
    return min(abs(N - a * K), abs(N - a * (K + 1)))

Submission status: Runtime Error

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

k = int(N/K)
if N - K*k > abs N - K*(k+1):
    print(abs N - K*(k+1))
else:
    print(N - K*k)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
x = n
min_n = n
rlist = []
while x>0:
    if (k == 1 or k==2):
        rlist = [0]
        break
    rlist.append(abs(x-k))
    x = abs(x-k)
    min_n = min(rlist)
    if (min_n == abs(x-k)):break
    if (min_n == 0):break
    if(len(set(rlist)) != len(rlist)):
        break

print(min(rlist))

Submission status: Runtime Error

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 n % k == 0:
    print(0)
elif n < k:
    print(min(n, abs(n - k)))
else:
    exit(1)
    print(1)

Submission status: Runtime Error

Solution code:
import math
first_input = list(map(lambda x:int(x), input().split(" ")))
#second_input = list(map(lambda x:int(x), input().split(" ")))

N = first_input[0]
K = first_input[1]

#print(int(N/K))
if N>K:
    first_min_N = N - K*math.floor(N/K)

print(min(first_min_N,abs(first_min_N-K)))

Submission status: Runtime Error

Solution code:
a=list(map(int, input().split()))
l=[a[0]]
if a[0]==1:
    print(0)
    exit()
elif a[0]==0:
    print(0)
    exit()
elif a[1]==1:
    print(0)
    exit()
def d(a,b):
    if a<b:
        if min(l)>b-a:
            l.append(b-a)
            d(b-a,a)
        else:
            print(min(l))
            exit()
    elif b<a:
        if min(l)>a-b:
            l.append(a-b)
            d(a-b,b)
        else:
            print(min(l))
            exit()
    else:
        print(0)
        exit()
d(a[0],a[1])

Submission status: Runtime Error

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

if (n < k):
    print(k % n)
elif (n >= k):
    if (l == 0):
        print(0)
    else:
        print(abs(k - n))

Submission status: Runtime Error

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: Runtime Error

Solution code:
min1=0
min2=0
n,k=list(map(int,input().split()))
if n<k:
    print(n)
    break
min1=n%k
min2=abs((n%k)%k)
if min1>min2:
    print(min2)
elif min1<min2:
    print(min1)
elif min1==min2:
    print(min1)

Submission status: Runtime Error

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: Runtime Error

Solution code:
a=list(map(int, input().split()))
l=[a[0]]
if a[0]==1 or a[1]==1:
    print(0)
    exit()
elif a[0]==0:
    print(0)
    exit()
def d(a,b):
    if a<b:
        if min(l)>b-a:
            l.append(b-a)
            d(b-a,a)
        else:
            print(min(l))
            exit()
    elif b<a:
        if min(l)>a-b:
            l.append(a-b)
            d(a-b,b)
        else:
            print(min(l))
            exit()
    else:
        print(0)
        exit()
d(a[0],a[1])

Submission status: Runtime Error

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)

Submission status: Runtime Error

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: Runtime Error

Solution code:
N, K = map(int, input().split())
N = abs(N - K)
while N > 100:
    N = round(N / N[0])
while N > 0:
    N = (N - K) 
print(abs(N))

Submission status: Runtime Error

Solution code:
def get():
    return list(map(int, input().split()))


a = get()
b = a[0]
c = a[1]
if b < c:
    if abs(b - c) < b:
        print(abs(b - a))
    else:
        print(b)
else:
    if a[0] % a[1] == a[1] or a[0] % a[1] == 0:
        print(0)
    else:
        if abs(a[0] % a[1] - a[1]) < abs((a[0] % a[1] - a[1]) % a[1]):
            print(abs(a[0] % a[1] - a[1]))
        else:
            print(abs((a[0] % a[1] - a[1]) % a[1]))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

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

    x = abs(n-k)

    if x == 0:
        print(0)
    elif k % n == 0:
        print(n)
    elif x < k:
        print(k%x)
    else:
        print(x%k)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
x = n
min_n = n
rlist = []
while x>0:
    if (k == 1 or k==2):
        rlist = [0]
        break
    rlist.append(abs(x-k))
    x = abs(x-k)
    min_n = min(rlist)
    if (min_n == abs(x-k)):break
    if (min_n == 0):break
print(min(rlist))

Submission status: Runtime Error

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 n % k == 0:
    print(0)
elif n < k:
    exit(1)
    print(min(n, abs(n - k)))
else:
    print(1)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
a=list(map(int, input().split()))
l=[a[0]]
if a[0]==1 or a[1]==1:
    print(0)
    exit()
elif a[0]==0:
    print(a[1])
    exit()
elif a[1]==0:
    print(a[0])
    exit()
def d(a,b):
    if a<b:
        if min(l)>b-a:
            l.append(b-a)
            d(b-a,a)
        else:
            print(min(l))
            exit()
    elif b<a:
        if min(l)>a-b:
            l.append(a-b)
            d(a-b,b)
        else:
            print(min(l))
            exit()
    else:
        print(0)
        exit()
d(a[0],a[1])

Submission status: Runtime Error

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

#桁が大きい時の処理
while(int(len(str(N))) > 2):
    N = int(N - K * int(N / 2));
    print("N=");
    print(N);
    print("K=");
    print(K);

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

k = math.gcd(N, K)

if K == 1:
    print(0)
else:
    print(k)

Submission status: Runtime Error

Solution code:
min1=0
min2=0
n,k=list(map(int,input().split()))
min1=n%k
min2=abs((n%k)k)
if min1>min2:
    print(min2)
elif min1<min2:
    print(min1)
elif min1==min2:
    print(min1)

Submission status: Runtime Error

Solution code:
a=list(map(int, input().split()))
l=[a[0]]
if a[0]==1 or a[1]==1:
    print(0)
    exit()
def d(a,b):
    if a<b:
        if min(l)>b-a:
            l.append(b-a)
            d(b-a,a)
        else:
            print(min(l))
            exit()
    elif b<a:
        if min(l)>a-b:
            l.append(a-b)
            d(a-b,b)
        else:
            print(min(l))
            exit()
    else:
        print(0)
        exit()
d(a[0],a[1])

Submission status: Runtime Error

Solution code:
N=int(input())
K=int(input())
l=abs(N//K)
p=K*l
for i in range(1,10000):
	if abs(N-(K*(l+1)))<abs(N-p):
		p=p+1
print(abs(N-p))

Submission status: Runtime Error

Solution code:

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

#M=N

#for i in range(100000):
#    M = min(N,M)
#    N = abs(N-K)

#print(M)


k = math.gcd(N, K)

if K == 1:
    print(0)
else:
    print(k)

Submission status: Runtime Error

Solution code:
# 初期入力
N,K = (int(x) for x in input().split(" "))

#
zeroika = N//K
N1 =abs(N-K*zeroika) #N1がマイナスになって初めてABS、そこまではKの倍数単調に減る
    N =N1
    N1 =abs(N-K)
    #print(N1,end=" ")
print(N)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
i = 1
lis = []
if K==1:
    print(0)
else:
    while i < N:
        N = abs(N-4)
        lis.append(N)
        i += 1
    print(min(lis))

Submission status: Runtime Error

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

ans = n
all = [n]
m = abs(n - k)

if n == 0 or n == 1 or k == 1 or n == 2*k or k == 2*n:
  ans = 0
else:
  all = []
  for _ in range(m):
    n = abs(n - k)
    all.append(n)

  
print(min(ans, min(all)))

Submission status: Runtime Error

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

#桁が大きい時の処理
while(int(len(str(N))) > 2):
    N = int(N - K * (N / 2));

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

Submission status: Runtime Error

Solution code:
N=int(input())
K=int(input())
l=abs(N//K)
p=K*l
for i in range(1,10000):
    if abs(N-(K*(l+1)))<abs(N-p):
        p=p+1
print(abs(N-p))

Submission status: Runtime Error

Solution code:
x,y=list(map(int,input().split()))
h=[]
while x>y:
    x-=y
    if (x-y)!=0:
        t=abs(x-y)
        h.append(t)
    else:
        u=(x)
        h.append(u)
print(min(h))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
x = n
i = 0
c = max(n, k) / min(n, k)
if k == 1:
    print("0")
else:
    while i <= c:
        x = abs(x - k)
        i += 1
    print(min(n, x))

Submission status: Runtime Error

Solution code:
a = int(input())
b = int(input())
c = abs(a - b)
d = abs(c-b)
print(d)

Submission status: Runtime Error

Solution code:
a=list(map(int, input().split()))
l=[a[0]]
if a[0]==1 or a[1]==1:
    print(0)
    exit()
def d(a,b):
    if a<b:
        if min(l)!=b-a:
            l.append(b-a)
            d(b-a,a)
        else:
            print(b-a)
            exit()
    elif b<a:
        if min(l)!=a-b:
            l.append(a-b)
            d(a-b,b)
        else:
            print(a-b)
            exit()
    else:
        print(0)
        exit()
d(a[0],a[1])

Submission status: Runtime Error

Solution code:
# coding: utf-8
 
a=list(map(int,input().split()))

#読み込み
N = a[0]
M = a[1]
pa =[]

while True:
    N = abs(N-M)
    pa.append(N)
    for i in pa:
        if pa[i+1] > pa[i]:
            break

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

Submission status: Runtime Error

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