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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

d = n // k
if d == 0:
    print(n)
else:
    ans = min(abs(n - d * k), abs(n - (d + 1)* k))
    print(ans)

Submission status: Wrong Answer

Solution code:

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

if n == 0 or k == 0:
    print(max(n, k))
    exit()
if n < k:
    ans = n
elif n % k == 0:
    ans = 0
else:
    ans = k % (n % k)

print(ans)

Submission status: Wrong Answer

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

if k == 1:
    n = 0
else:
    n = n % (k/2)

s = int(n)

print(s)

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
    p = (k * ((n//k) + 1)) - n
    print(min(p, q))

Submission status: Wrong Answer

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

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

if n == 0:
    print(0)
elif k == 1:
    print(1)
elif n == k:
    print(0)
else:
    if n > k:
        r = n % k
        if r == 0:
            print(0)
        else:
            print(abs(r - k))
    else:
        n = abs(n - k)
        n = abs(n - k)
        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 or n == 1:
        print(0)
    elif n % k == 0:
        print(0)
    else:
        ans = gcd(n, k)
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys,math,copy,queue,itertools,bisect
from fractions import gcd
sys.setrecursionlimit(10**7)
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
_LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
INF = 10**18
MOD = 10**9 + 7
DD = ((1,0),(0,1),(-1,0),(0,-1))
N,K = LI()
if N == 1 or K == 1:
    print (0)
    exit (0)
if N == 0:
    print (0)
    exit(0)
N = N % K
while True:
    NN = abs(N - K)
    if NN >= N:
        print (N)
        exit (0)
    N = NN

Submission status: Wrong Answer

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

for i in range(N+1):
    try:
        if ((N-i) / K).is_integer():
            ans = i
            break
        elif ((N+i) / K).is_integer():
            ans = i
            break
    except:
        break

print(ans)

Submission status: Wrong Answer

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

if k == 1:
    print(0)
    exit()
    
def gcd(a,b):
  while b!=0:
    a,b=b,a%b
  return a

tmp = gcd(n, k)
low = 10**19

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

print(low)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
cnt = int(N / K) + 1

print(min(abs(N - (K * cnt)), abs(N - (K * (cnt-1)))))

Submission status: Wrong Answer

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

a = int(N / K)

b = abs(N - K * a)
c = abs(N - K *(a + 1))

print(min(b,c))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math

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

num=math.floor(n/k)
be=abs(n-num*k)
af=abs(n-num*k-k)

print(be if be<=af else af)

Submission status: Wrong Answer

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

if n == 0:
    print(k)
elif k == 1 or n == k:
    print(0)
elif n > k:
    r = n % k
    print(k-r)
elif n < k:
    print(k%n)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

a = N - (N // K)*K

if N < K:
        print(N)
elif N == K:
        print(0)
else:
        m = 1000000000000000000
        for i in range(100):
                m = min(m, abs(a - K)) 
        print(m)

Submission status: Wrong Answer

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

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:
    seimin = N - (N//K) * K
    print(min(seimin, abs(seimin-K)))
else:
    print(min(N, N-K))

Submission status: Wrong Answer

Solution code:
li = list(map(int, input().split()))
n = li[0]
k = li[1]
if n % k <= abs(n - k):
    print(n % k)
else:
    print(abs(n - k))

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())  #複数の数字
print(N%K)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
nm = list(map(int,input().rstrip().split()))
n = nm[0]
k = nm[1]
ans  = n
result = 0
for i in range(100):
    temp = ans
    ans = abs(ans - k)
    if ans > temp :
        result = temp
        if result == 0 or result == 1 :
            break
    elif ans < temp :
        result = ans
        if result == 0 or result == 1 :
            break
    else:
        result = temp
        if result == 0 or result == 1 :
            break
print(result)

Submission status: Wrong Answer

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

ans = float('inf')
base = N
diff = K
while True:
    d = abs(base - diff)
    mn = min(d, ans)
    if ans <= mn:
        break
    else:
        base = min(K, d)
        ans = mn

print(ans)

Submission status: Wrong Answer

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

# ユークリッドの互除法
# a = b * q + r 
# b = b'* r + r' ...

a = N
b = K
if a < b:
  r = b - a
else:
  r = a % b
ret = 0
while r != 0:
  a = b
  b = r
  r = a % b
  if r == 0:
    break
  ret = r
print( ret )

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from math import ceil,floor
N,K = map(int,input().split())
n = N/K
ans1=abs(N-floor(n)*K)
ans2=abs(ceil(n)*K-N)
print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:
a=list(map(int, input().split()))
n=a[0]
k=a[1]
def gcd(a,b):
    if(b>a):
        a,b=b,a
    i=b
    while i>0:
        if(a%i==0 and b%i==0):
            return i
        i-=1
if n==1 or k==1:
    print(0)
    exit()
else:
    print(gcd(n,k))

Submission status: Wrong Answer

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

while 1:
    if (N%2==0)and(K%2==0)and(N>=K):
        a = 0
        break
    if K==1:
        a = 0
        break
    if K==0:
        a = N
        break
    a = N
    N = abs(N-K)

    if c==0:
        c += 1
        continue
    if a<N:
        break
print(a)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
N , M = (map(int , input().split()))
a = -(-N//M)
if a == 1:
    ans = N
else:
    ans = (a*M)-N
print(ans)

Submission status: Wrong Answer

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

if n < k:
    print(n)
else:
    s = math.ceil(n / k)
    ans = abs(n - s  * k)
    print(ans)

Submission status: Wrong Answer

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

ans = fractions.gcd(N,K)
print(ans)

Submission status: Wrong Answer

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

if K == 1:
    print(0)

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

print(N)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

if n > k:
    tmp = n%k
    print(min(tmp,abs(tmp-k)))
    exit()

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

if n == 0:
    print(0)

Submission status: Wrong Answer

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

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:
    ans = 0
    if N > K:
        ans = N % K
        ans = min(ans, abs(K-ans))
    else:
        if N == abs(N-K):
            ans = 0
        else:
            ans = N
    print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
small = (N + K) // K
print(min(N, abs(N - small * K), abs(N - (small + 1) * 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())

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

print(ans)

Submission status: Wrong Answer

Solution code:
# coding:utf-8
import math
import fractions

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

if K==0:
    print("N")
elif N==0 or N%K==0:
    print("0")
else:
    a=fractions.gcd(N,K)
    N/=a
    K/=a
    mi=N
    t=N
    for i in range(2):
        t=abs(t-K)
        mi=min(mi,t)
    print(mi*a)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
a = n
a = a-(int(a/k)*k)

while True:
    b = abs(a-k)
    if a < b:
        print(a)
        break
    else:
        a = b
        continue

Submission status: Wrong Answer

Solution code:
import sys,math,copy,queue,itertools,bisect
from fractions import gcd
sys.setrecursionlimit(10**7)
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
_LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
INF = 10**18
MOD = 10**9 + 7
DD = ((1,0),(0,1),(-1,0),(0,-1))
N,K = LI()
if N == 1 or K == 1:
    print (0)
    exit (0)
if N == 0:
    print (K)
    exit(0)
N = N % K
while True:
    NN = abs(N - K)
    if NN >= N:
        print (N)
        exit (0)
    N = NN

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
    p = (k * ((n//k) + 1)) - k
    print(min(p, q))

Submission status: Wrong Answer

Solution code:
#C
import math
n, k = map(int, input().split())
s = round(n * 10 / k, -1) / 10
print(int(abs(n-(s*k))))

Submission status: Wrong Answer

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

if N < K:
    print(N)
    sys.exit(0)
else:
    ans = N % K
    ans = min(ans, abs(K-ans))
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
l=(n//k)*k
while abs(n-l) < n:
  n = abs(n-l)
print(n)

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 fractions import gcd
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 functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))
ZIP = lambda: zip(*(MAP() for _ in range(n)))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N, K = MAP()

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

if N < K:
    print(str(N))
elif N%K==0:
    print("0")
else:
    if N-(K*count) > abs(N-(K*(count+1))):
        print(str(abs(N-(K*(count+1)))))
    else:
        print(str(N-(K*count)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

result = abs(N - K)

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

Submission status: Wrong Answer

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


if n < k:
    print(n)
else:
    n = n % k
    if abs(n - k) < n:
        print(abs(n - k))
    else:
        print(n)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
NS = [int(_) for _ in input().split()]

N = NS[0]
K = NS[1]

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

def solve(N, K):

    if K == 1:
        return 0
        
    bef = rep(N, K)
    
    while(True):
        a = rep(bef, K)
        if bef <= a:
            bef = a
        else:
            bef = a
            break
    return bef

print(solve(N, K))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

if n > k:
    tmp = n%k
    print(min(tmp,abs(tmp-k)))
    exit()

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

Submission status: Wrong Answer

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

if N==0:
    print(0)

N=N%K
cand1=abs(N-K)
cand2=abs(cand1-K)
print(min(N,cand1,cand2))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
if N > K**2:
    N = K**2
elif K > N**2:
    K = N**2

while abs(N-K) < N:
    N = min(abs(N-K), N)
print(N)

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
    p = (k * (n//k) + 1) - k
    print(min(p, q))

Submission status: Wrong Answer

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

mod = N % K

print(min(mod, abs(mod + K)))

Submission status: Wrong Answer

Solution code:
from math import ceil,floor
N,K = map(int,input().split())
n = N/K
ans1=N-floor(n)*K
ans2=ceil(n)*K-N
print(min(ans1,ans2))

Submission status: Wrong Answer

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

a = N - (N // K)*K

if N < K:
        print(N)
else:
        m = 1000000000000000000
        for i in range(100):
                m = min(m, abs(a - K)) 
        print(m)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K=map(int, input().split())
m=(N//K)
if m>=1:
    tmp=abs(N-K*m)
    res=min(tmp,abs(tmp-K))
else:
    res=abs(N-K)
print(res)

Submission status: Wrong Answer

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

if k == 1:
    print(0)
    exit()
    
def gcd(a,b):
  while b!=0:
    a,b=b,a%b
  return a

print(gcd(n, k))

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 = min(N % K, tmp)

print(tmp1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

    print(ans)
    return

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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:
    seimin = N - (N//K) * K
    print(min(seimin, abs(seimin-K)))
else:
    print(N)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split(' '))
x = N - int(N/K) * K
x1 = K - x
if x > x1:
  print(x1)
else:
  print(x)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if n == 0:
    print(k)
elif k == 1 or n == k:
    print(0)
elif n > k:
    r = n % k
    print(k-r)
elif n < k:
    print(k%n)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
import math
kk = k * math.ceil(n/k)
print(min(abs(n-k), kk-n,n))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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 and (N - K) >= K:
    N -= (N - K) // K
if N >= K:
    N = abs(N-K)

if N >= (K / 2):
    print(K - N)
    exit()
print(N)

Submission status: Wrong Answer

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

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

if n >= k:
  n = n % k

for i in range(10**18):
  if abs(k-n) < n:
    n = abs(k-n)
  else:
    break

print(n)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
if N == 0:
    print(0)
else:
    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,open(0).read().split())
print(N%K)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


def main(n, k):
    amari = n % abs(n - k)

    if amari == k:
        return 0
    else:
        return min(amari, n)


print(main)

# main(7,4)
# main(2,6)
# main(1000000000000000000 ,1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, K = input().split()
N = int(N)
K = int(K)
if N>=K:
    m = N//K
    print(min(N-m*K, abs(N-(m+1)*K)))
else:
    print(N)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
if N < K:
    print(N)
elif N == K:
    print(N-K)
else:
    cnt = N//K
    ans = abs(N - K*(cnt+1))
    print(ans)

Submission status: Wrong Answer

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

if N <= K:
    print(N)
    sys.exit(0)
else:
    ans = N % K
    ans = min(ans, abs(K-ans))
print(ans)

Submission status: Wrong Answer

Solution code:
nm = list(map(int,input().rstrip().split()))
n = nm[0]
k = nm[1]
ans  = n
result = 0
for i in range(50):
    temp = ans
    ans = abs(ans - k)
    if ans > temp :
        result = temp
    elif ans < temp :
        result = ans
    else:
        result = temp
print(result)

Submission status: Wrong Answer

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


if n>k:
    large=(n-(k*(n//k-1)))-k
    small=k-(n-k*(n//k))
    print(large,small)
    print(min(large,small))
elif n==k:
    print(0)
else:
    print(min(n,abs(n-k)))

Submission status: Wrong Answer

Solution code:
N,K=list(map(int,input().split()))
num=int(N/K)
a=N-K*num
b=N-int(K*(num+1))
if(a<0):
  a*=-1
if(b<0):
  b*=-1
c=10000000000000000000
if(num>0):
  c=N-K*(num-1)
  if(c<0):
    c*=-1
d=N-K*(num+2)
if(d<0):
  d*=-1
if(c<=a and c<=b and c<=d):
  print(c)
elif(a<=b and a<=c and a<=d):
  print(a)
elif(b<=a and b<=c and b<=d):
  print(b)
elif(d<=a and d<=b and d<=c):
  print(d)

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 = min(N % K, tmp)

print(tmp1)

Submission status: Wrong Answer

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

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

else:
    print(n%(abs(n-k)))

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math

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

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

Submission status: Wrong Answer

Solution code:
from decimal import Decimal, ROUND_HALF_UP

input_line = [int(i) for i in input().split()]
n = input_line[0]
k = input_line[1]

x = Decimal(str(n / k))
xx = x.quantize(Decimal('0'), rounding=ROUND_HALF_UP) 
print(abs(n - (xx * k)))

Submission status: Wrong Answer

Solution code:
import itertools
import math
import fractions

n,k = map(int,input().split())
ans = n
if k == 1:
    print("0")
    exit()
while(1):
    
    ans = abs(ans-k)
    if ans == n:
        break
    if ans == 1:
        break
print(ans)

Submission status: Wrong Answer

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

print(n%k)

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    syo = int(N / K)    # 桁の件
    amari = N % K
    tmp = K * (syo + 1) - N

    if amari <= tmp:
        print(amari)
    else:
        print(tmp)

main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
if N%K==0:
    print(0)
    exit()
kari=int(N/K)
N=N-K*kari
while True:
    if N<abs(N-K):
        print(N)
        exit()
    else:
        N=abs(N-K)

Submission status: Wrong Answer

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

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

ans = 0

if N!=K and K>1:
    if N < K:
        ans = min(N,abs(N-K))
    else:
        ans = gcd(N,K)
if N==K:
    ans = 0
print(ans)

Submission status: Wrong Answer

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

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 n==0:
	print(k)
elif one<two:
	print(one)
else:
	print(two)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
def gcd(x, y):
    while y != 0:
        x, y = y, x % y
    return x


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

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

print(gcd(n, k))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
if n >= k:
    print(min(n % k, k - (n % k)))
else:
    print(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 = 0

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

if n < k:
    print(n)
else:
    dis = n % k
    print(min(dis,abs(dis - k)))

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:
    seimin = N - (N//K) * K
    print(min(seimin, abs(seimin-K)))
else:
    print(N)

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())
if N < K:
    print(N)
else:
    cnt = N//K
    ans = abs(N - K*(cnt+1))
    print(ans)

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:
N,K = map(int,input().split())

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

Submission status: Wrong Answer

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

ans1 = n%k
print(ans1)
if ans1 > k-ans1:
  ans1 = k-ans1
print(ans1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
if N%K==0:
  ans=0
else:
  if N%2==0:
    ans=min(N,abs(N-K))
  else:
    ans=1
print(ans)

Submission status: Wrong Answer

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

def f(x,k):
    return (abs(x-k) < x)

n = n // k

while(f(n,k)):
    n = abs(n-k)

print(n)

Submission status: Wrong Answer

Solution code:
def gcd(m, n):
    r = m % n
    return gcd(n, r) if r else n

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

Submission status: Wrong Answer

Solution code:
import math                                                                     
 
n,k = map(int,input().split())
df = abs(n - k)
 
if k == 1:
    print(0)
    exit(0)
 
for i in range(0,n):
    if df > abs(df - k): 
        df = abs(df - k)
 
print(df)