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

Submission status: Wrong Answer

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

if K == 1:
    print(0)
    exit()
else:
    for i in range(100000000):
        k = abs(N-K)
        if k < N:
            N = k                
print(N)

Submission status: Wrong Answer

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

mn = n

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

print(mn)

Submission status: Wrong Answer

Solution code:
x, k = map(int, input().split())
result = x%k
if result == 0 :
    print(0)
elif k > result :
    print(k - result)
else :
    print(result)

Submission status: Wrong Answer

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

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


before = abs(N - K)
N = N % K
while True:
    ze = abs(N - K)
    if ze >= before:
        print(before)
        exit(0)
    before=ze
    N=ze



# ans = [abs(N-K)]
# for i in range(100):
#     ans.append(abs(ans[i]-K))
# print(min(ans))

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 n % k == 0:
    print(0)
elif n % 2 == 0 and k % 2 == 0:
    print(2)
elif n > k:
    print(1)
else:
    print(n)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
if n<=k:
    print(n)
elif k==0:
    print(n)
elif n%k==0:
    print(n)
else:
    t=n//k
    s=(t+1)*k-n
    r=n-t*k
    print(min(r,s))

Submission status: Wrong Answer

Solution code:
N, M = map(int,input().split())
if N*2<=M:
    print(N)
elif M == 1:
    print(0)
else:
    yo = N % M
    print(M-yo)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
N,K = list(map(int, input().split()))
dup = [N]
M = N
N = N % K
while True:
    N = abs(N-K)
    if ( N < M ):
        M = N
    if N in dup:
        break
    else:
        dup.append(N)

print(M)

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 or N == 0 or N % K == 0:
    print(0)
elif N*2 < K:
    print(N)
elif N < K < 2*N:
    print(K-N)
else:
    print(gcd(N, K))

Submission status: Wrong Answer

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

else:
    a =abs(math.floor(n/k)*k-n)
    b = abs(math.ceil(n/k)*k-n)
    if a < b:
        print(a)
        
    else:
        print(b)

Submission status: Wrong Answer

Solution code:
import math

N,K = map(int,input().split())
a = N%K
b = abs(N-math.ceil(N/K)*K)
print(min(a,b))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
import math
if N > K:
    s = math.ceil(N / K)
    print(abs(N - s * K))
else:
    print(min(N, abs(N - K)))

Submission status: Wrong Answer

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

nnn = n % k

base = n
for i in range(100):
    xxx = abs(nnn - k)
    if xxx < n:
        base = xxx
    
    nnn = xxx

print(base)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

elif N % K != 0:
    print(1)

elif N % K == 0:
    print(0)

else:
    print(2)

Submission status: Wrong Answer

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

remain = n % k
if( k==1):
  print(0)
elif( remain == 0 ):
  print(0)
else:
  print( min(abs( remain - k), abs(k-remain) ))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

min_val = abs(n-k)
while min_val != 0:
    if min_val > abs(min_val -k):
        min_val=abs(min_val -k)
    else:
        print(min_val)
        exit()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
if n%k == 0:
    print(0)
elif n%2 == 0 and k%2 == 0:
    print(2)
else:
    tmp = n % k
    print(min(tmp, k-tmp))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline

N, K = map(int, read().split())
if N == K:
    print(0)
elif N > K:
    N %= K
    print(min(N, abs(N - K)))
else:
    print(min(K, abs(N - K)))

Submission status: Wrong Answer

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

s=n//k
x=abs(n-(s*k))

print(x)

Submission status: Wrong Answer

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

# process/output
if K == 1:
    print(0)
else:
    n = N
    while n > K:
        n = n//K + n % K

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

Submission status: Wrong Answer

Solution code:

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

if n == 0:
    print(k)
else:
    n = n%k
    print(min(n,k-n))

Submission status: Wrong Answer

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

if N%K == 0:
    print(0)
elif K%N==0:
    print(N)
else:
    print(1)

Submission status: Wrong Answer

Solution code:
import math

data = input().split()

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

div = math.floor(n/k)
n2 = n - div*k
#print(div,n2)
one = abs(n2-k)
two = abs(one-k)

if one<two:
	print(one)
else:
	print(two)

Submission status: Wrong Answer

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

t1 = int(N/K)
t2 = t1 + 1
t3 = t1 + 2
t4 = t1 - 1
t5 = t1 - 2
ans1 = abs(N-K*t1)
ans2 = abs(N-K*t2)
ans3 = abs(N-K*t3)
ans4 = abs(N-K*t4)
ans5 = abs(N-K*t5)
print(min([ans1,ans2,ans3,ans4,ans5]))

Submission status: Wrong Answer

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

print(N%K)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8 ff=unix


def gcd(a, b):
    """最大公約数
    """
    while b:
        a, b = b, a % b
    return a


def solv(N, K):
    if N % K == 0:
        return 0
    return gcd(N, K)


if __name__ == "__main__":

    # N K
    #
    N, K = map(int, input().split())
    ans = solv(N, K)

    print(ans)

Submission status: Wrong Answer

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


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

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math

if __name__ == '__main__':
    s = input().rstrip().split(' ')
    N,K = int(s[0]), int(s[1])
    cand1 = abs(N - K*math.ceil(N/float(K)))
    cand2 = abs(N - K*math.floor(N/float(K)))
    print(min(cand1, cand2))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import collections
import itertools as it
import math
import numpy as np

#  = input()
#  = int(input())
N, K = map(int, input().split())
#  = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()

if N > K:
    print(min(N%K, abs(N%K-K)))
else:
    print(N)

Submission status: Wrong Answer

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

if N>K:
  y = int(N/K)
  x = N - y*K
else:
  x = N 

while True:
  x_next = abs(x-K)
  if x_next>x:
    ans = x
    break
  else:
    x = x_next
print(ans)

Submission status: Wrong Answer

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

if N == K:
    print(0)
elif N > K:
    x, y = divmod(N, K)
    if y == 0:
        print(y)
    else:
        print(abs(y-K))
else:
    print(N)

Submission status: Wrong Answer

Solution code:
from fractions import gcd


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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

k = N//K
N = N - k*K
print(N)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
if n<k:
    print(n)
elif k==0:
    print(n)
elif n%k==0:
    print(0)
else:
    t=n//k
    s=(t+1)*k-n
    r=n-t*k
    print(min(r,s))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

before = abs(N - K)
N = N % K
while True:
    ze = abs(N - K)
    if ze >= before:
        print(before)
        exit(0)
    before=ze
    N=ze

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#n=int(input())
n,k=map(int,input().split())
#l=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]
ans=0
import fractions
if n>=k:
    if n%k==0:
        ans=0
    else:
        ans=fractions.gcd(n,k)
else:
    ans=fractions.gcd(n,k)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np

def main():
    N, K = map(int, read().split())
    ans = N
    if N <= K:
        ans = N // K
    ans = min(ans, K - ans)
    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
r = n % k
a = k -r
if a < 0:
	a += k
print(a)

Submission status: Wrong Answer

Solution code:
import numpy as np

def main():
    n, k = map(np.int64, input().split(" "))
    a1 = np.int64(np.floor(n / k))
    a2 = np.int64(np.floor(n / k + 1))
    b1 = abs(n - a1 * k)
    b2 = abs(n - a2 * k)
    #print(n - a1 * k, n - a2 * k)
    ans = np.min([b1, b2])
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

ans = n%k

if ans < abs(ans-k):
    ans = abs(ans-k)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

done = []
now = n

while True:
  if now in done:
    break
  done.append(now)
  if now == 0:
    break
  if now%k == 0:
    done.append(0)
    break
  nex = abs((k - (now//k)*k) - now)
  now = nex

print(str(min(done)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if N < K:
    Ans = N
else:
    Ans = min(abs((N%K)-K),N%K)

print(Ans)

Submission status: Wrong Answer

Solution code:

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

if k == 1 or n == 1:
    print(0)
else:
    n = n%k
    print(min(n,k-n))

Submission status: Wrong Answer

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


hk=(int(k/2) if k%2==0 else int(k/2)+1)


if hk<=n<=k:
    n=abs(n-k)
elif n>k:
    a=int(n/k)
    n=n-a*k
    if hk<=n<=k:
        n=abs(n-k)
print(n)

Submission status: Wrong Answer

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

tmp1 = 0
if N > K:
    tmp, m = divmod(N, K)
    if m != 0:
        tmp1 = N % (tmp + 1)
else:
    tmp = abs(N - K)
    tmp1 = N % K

print(tmp1)

Submission status: Wrong Answer

Solution code:
n,k = (int(i) for i in input().split())
m = n%k
l =abs(n-k)
r = abs(m-k)
if m !=0:
	print(min(l,r))
else:
	print(0)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
count = n // k
abs_val = abs(n-k)
ans = n - k * count

if abs(ans - abs_val) < ans:
    print(abs(ans - k))
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n,k = nm()

if n < k:
  a = n
  b = k
else:
  a = k
  b = n


while a!=0:
  x = b%a
  b = a
  a = x


print(b)

Submission status: Wrong Answer

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

print(n%k)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
if(N < K):
  print(N)
  exit()
if(N % K == 0):
  print(0)
  exit()
print(min(N % K, abs(N % K - K)))

Submission status: Wrong Answer

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

c = int(nk[0]/nk[1])
x = nk[0] - nk[1]*c
p = abs(x-nk[1])
print(x if x < p else p)

Submission status: Wrong Answer

Solution code:
import math

N,K = map(int,input().split())
a = N%K
b = abs(N-math.ceil(7/4)*4)
print(min(a,b))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
while(1):
	A=N%K
	if N<K:
		print(min(N,abs(N-K)))
		exit()
	elif A==0:
		print(0)
		exit()
	else:
		print(A)
		exit()

Submission status: Wrong Answer

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

x = N
if x < K:
    print(x)
elif x == K:
    print(0)
else:
    print(min(x % K, abs(x % K - K)))

Submission status: Wrong Answer

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

if n <=k :
    print(min(n,abs(n-k)))
else:
    n = n-int(n/k)*k
    print(min(n,abs(n-k)))

Submission status: Wrong Answer

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

m = math.floor(n/k)

print(min(abs(n-m*k), abs(n-(m+1)*k)))

Submission status: Wrong Answer

Solution code:
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') #無限大
def gcd(a,b):return fractions.gcd(a,b) #最大公約数
def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数
def iin(): return int(sys.stdin.readline()) #整数読み込み
def ifn(): return float(sys.stdin.readline()) #浮動小数点読み込み
def isn(): return sys.stdin.readline().split() #文字列読み込み
def imn(): return map(int, sys.stdin.readline().split()) #整数map取得
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) #整数-1map取得
def fmn(): return map(float, sys.stdin.readline().split()) #浮動小数点map取得
def iln(): return list(map(int, sys.stdin.readline().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def fln(): return list(map(float, sys.stdin.readline().split())) # 浮動小数点リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 # 2点間の距離
def m_add(a,b): return (a+b) % MOD
def print_list(l): print(*l, sep='\n')
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def No(): print('No')

def sieves_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime

N,K = imn()


if N == 0 or N % K == 0:
    print(0)
else:
    
    print(fractions.gcd(N,K))

Submission status: Wrong Answer

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

if N==0:
    print(0)

N=N%K

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
N, K = [int(x) for x in input().strip().split()]
if N == 0 or K == 1:
    print(0)
    exit()
if N >= K:
    N -= (N - K) // K
N = min(N, abs(N-K))
if N == K:
    print(0)
    exit()
if N >= (K / 2):
    print(K - N)
    exit()
print(N)

Submission status: Wrong Answer

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

if K > N:
    print(N)
else:
    t1 = int(N/K)
    t2 = t1 + 1
    t3 = t1 + 2
    t4 = t1 - 1
    t5 = t1 - 2
    ans1 = abs(N-K*t1)
    ans2 = abs(N-K*t2)
    ans3 = abs(N-K*t3)
    ans4 = abs(N-K*t4)
    ans5 = abs(N-K*t5)
    print(min([ans1,ans2,ans3,ans4,ans5]))

Submission status: Wrong Answer

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

if N < K:
    ans = N
elif N == K or K == 1:
    ans = 0
else:
    a = N % K
    ans = abs(a - K)

print(str(ans))

Submission status: Wrong Answer

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

if n < k:
    print(n)
else:
    tmp = n % k
    while(True):
        prev_n = tmp
        tmp = abs(tmp - k)
        if prev_n <= tmp:
            n = prev_n
            break
    print(n)

Submission status: Wrong Answer

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

if N % K == 0:
  print(0)
elif N % 2 != 0 or K % 2 != 0:
  print(1)
else:
  print(2)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

remain = n % k

if( remain == 0 ):
  print("0")
else:
  print( min(abs( remain - k), abs(k-remain) ))

Submission status: Wrong Answer

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

if n > k:
    print(min(n % k, n % (n - k)))
elif n == k:
    print(0)
elif n < k:
    print(min(n, k - n))

Submission status: Wrong Answer

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

import math
ans = min( abs( n - math.ceil(n/k)*k ), abs( n - math.floor(n/k)*k))
print(ans)

Submission status: Wrong Answer

Solution code:
from fractions import gcd


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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
I=lambda:list(map(int,input().split()))
n,k=I()
if n%k==0 or k%n==0:
    print(0)
else:
    print(abs(n-k))

Submission status: Wrong Answer

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

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

elif N % K % 2 == 1:
    print(1)

elif N % K == 0 or N == K:
    print(0)

else:
    print(2)

Submission status: Wrong Answer

Solution code:
## coding: UTF-8
from decimal import *
from itertools import permutations, combinations,combinations_with_replacement,product
s = input().split()
t = [int(p) for p in s]

N = t[0]
K = t[1]

p = int(N/K)
a = N - p * K
b = abs(a - K)
#print('N:{}, K:{}, p:{}, a:{}, b:{}'.format(N,K,p,a,b))
print(min(a,b))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
left = 0
right = 10**19

while right - left >1:
  middle = (right + left)//2
  if middle < abs(middle - k):
    left = middle
  else:
    right = middle
print(left)

Submission status: Wrong Answer

Solution code:
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') #無限大
def gcd(a,b):return fractions.gcd(a,b) #最大公約数
def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数
def iin(): return int(sys.stdin.readline()) #整数読み込み
def ifn(): return float(sys.stdin.readline()) #浮動小数点読み込み
def isn(): return sys.stdin.readline().split() #文字列読み込み
def imn(): return map(int, sys.stdin.readline().split()) #整数map取得
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) #整数-1map取得
def fmn(): return map(float, sys.stdin.readline().split()) #浮動小数点map取得
def iln(): return list(map(int, sys.stdin.readline().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def fln(): return list(map(float, sys.stdin.readline().split())) # 浮動小数点リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 # 2点間の距離
def m_add(a,b): return (a+b) % MOD
def print_list(l): print(*l, sep='\n')
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def No(): print('No')

def sieves_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime

N,K = imn()


if N % K == 0:
    print(0)
else:
    
    print(fractions.gcd(N,K))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if n==k:
    ans = 0
elif n<k:
    ans = n
else:
    ans = k-(n%k)

if n>k and k==1:
    ans -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()

a = N // K
b = ceil(N / K)
# print(a, b)
c,d = N-a, N-b
# print(c,d)
# print(N-c,N-d)
if N >= K:
    ans = min(c, d, N-c, N-d)
# K > N:
else:
    ans = min(K-N, N)
print(ans)

Submission status: Wrong Answer

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

if n < abs(n - k):
    print(n)
elif n % k == 0:
    print(0)
elif n % k != 0:
    print(1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
# int(input()) # 入力が1つ
n, k = map(int, input().split()) # 入力が複数

# if k == 1:
#     print(0)
# else:
ans = n % k
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

remain = n % k

if( remain == 0 ):
  print(0)
else:
  print( min(abs( remain - k), abs(k-remain) ))

Submission status: Wrong Answer

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

elif n%k==0:
    print(0)
else:
    t=n//k
    s=(t+1)*k-n
    r=n-t*k
    print(min(r,s))

Submission status: Wrong Answer

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

N = K - (N % K) if N < K else (N % K)

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y) 
if n != k:
    print(gcd(max(n,k), min(n,k))) 
else:
    print(0)

Submission status: Wrong Answer

Solution code:
N,k = [int(x) for x in input().split()]
if N < k:
  print(N)
else:
  print(min(N%k,k-N%k))

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

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

if N < K:
    print(N)
    exit()

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

Submission status: Wrong Answer

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


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

    if N == K:
        print(0)
    elif N < K:
        print(N)
    else:
        amari = N % K
        if abs(amari - K) > amari:
            print(amari)
        else:
            print(abs(amari - K))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

ans=N%K

if ans==0:
  print(0)
elif ans>ans-K:
  print(ans-K)
else:
  print(ans)

Submission status: Wrong Answer

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

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 or N == 0 or N % K == 0:
    print(0)
elif N < K:
    print(N)
else:
    print(gcd(N, K))

Submission status: Wrong Answer

Solution code:
## coding: UTF-8
from decimal import *
from itertools import permutations, combinations,combinations_with_replacement,product
s = input().split()
t = [int(p) for p in s]

N = t[0]
K = t[1]

p = int(N/K)
a = N - p * K
b = abs(a - K)
print('N:{}, K:{}, p:{}, a:{}, b:{}'.format(N,K,p,a,b))

Submission status: Wrong Answer

Solution code:
x,y = map(int,input().split())
print(x%y)

Submission status: Wrong Answer

Solution code:
n, k = (int(i) for i in input().split())
if k > n:
    print(n)
elif n % k == 0:
    print(0)
else:
    q = n % k
    q = abs(q-k)
    print(min(k, q))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
import math
N,K = list(map(int,input().split()))

if N<= K:
    print(min(N,K-N))
else:
    N = N - math.floor(N/K)*K 
    print(min(N, K-N))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = 10**19
if n >= k:
    a=n//k
b=n%k
c=abs(b-k)
l=[n,a,b,c]
print(min(l))

Submission status: Wrong Answer

Solution code:
import numpy as np
N,K = map(int,input().split())
x = np.roots([-K,N])[0].astype('int64')
print(min(abs(N - K * x),abs(N - K *(x+1))))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K=[int(i) for i in input().split(" ")]
proc=lambda x:abs(x-K)
n=int(N/K)
a=N-n*K
b=abs(a-K)
if a>b:
    print(b)
else:
    print(a)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
while(1):
	A=N%K
	if N<K:
		print(min(N,abs(N-K)))
		exit()
	elif A==0:
		print(0)
		exit()
	else:
		print(1)
		exit()

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
min_kakeru = int(n/k)
max_kakeru = int(n/k) + 1
a1 = abs(n - min_kakeru * k)
a2 = abs(n - max_kakeru * k)

if n == 0:
  print(0)
elif a1 < a2:
  print(a1)
else:
  print(a2)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

sub = int(N / K)
val1 = N - K * sub
val2 = K - val1

if val1 > val2:
    print(val2)
else:
    print(val1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if N > K:
    if (N%K) != 0:
        ans = N%(N%K)
    else:
        ans = 0
else:
    ans = N
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split(' '))
diff = N % K
if N < K:
    print(N)
elif diff == 0:
    print(0)
else:
    print(min(K-diff,diff))

Submission status: Wrong Answer

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

s1 = n//k
if s1 == 0:
    print(n)
else:
    print(abs(n - k*(s1+1)))

Submission status: Wrong Answer

Solution code:
import numpy as np
N,K = list(map(int, input().split()))
if N==K:
    print(0)
if N>K:
    N=N%K

if 2*N>=K:
    print(K-N)
else:
    print(N)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
if n == 0:
    ans = n
elif n == 1 or k == 1:
    ans = 0
else:
    ans = min(n % k, k - n % k)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
if n<k:
    print(n)
elif n==k:
    print(1)
elif n%k==0:
    print(0)
else:
    t=n//k
    s=(t+1)*k-n
    r=n-t*k
    print(min(r,s))

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
c = int(n/k)
print(min(abs(n-c*k), abs(n-(c+1)*k)))

Submission status: Wrong Answer

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

min_0 = N
min_1 = abs(N-m*K)
min_2 = abs(min_1-K)

print(min(min_0, min_1, min_2))