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 = list(map(int,input().split()))
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

if n > k:
    N = n
    K = k
    A = []
    a = 100
    while a != 0:    
        a = N - K*(N//K)
        N = K
        K = a
        A.append(a)
    if len(A) ==  1:
        print("0")
    else:
        print(A[-2])
else:
    print(gcd(n,k))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
ans = [n,k]
if k ==1:
    print(0)
else:
    ans.append(abs(n-k))
    ans.append(abs(ans[2]-k))
    print(min(ans))

Submission status: Wrong Answer

Solution code:
i = list(map(int, input().split()))
N = i[0]
K = i[1]

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

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
d = N
# for i in range(N//K + 1):
#     d = d - K
sumk = (N//K)*K
d = N - sumk

d2 = N
if (d*(-1)-K)<0:
    d2 = (d*(-1)-K)*(-1)
else :
    d2 = (d*(-1)-K)
d3 = N
if (d2-K)<0:
    d3 = (d2-K)*(-1)
else :
    d3 = (d2-K)
ans = min([(d*(-1)), d2, d3])
print(ans)

Submission status: Wrong Answer

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

if (N%K) <=(K/2):
	x=N%K
else:
	x=N-N%K
	
print(x)

Submission status: Wrong Answer

Solution code:

import collections


class Solution:
    def solve(self, n, k):
        if n < k:
            return n
        else:
            rem = n % k
            return min(rem, abs(rem-k))


sol = Solution()

[n, k] = list(map(int, input().strip().split()))
print(sol.solve(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)

print(0)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
if k == 0 or 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:
def solution():
    n, m = map(int, input().split())
    if n < m:
        print(n)
        return
    ans = n % m
    ans = min(ans, n)
    print(min(ans, abs(ans-m)))

solution()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


if abs(N-K) == 1:
    print(1)
elif N <= K/2:
    print(N)
elif K/2 < N < K:
    print(abs(N-K))
elif N % K == 0:
    print(0)
else:
    if abs(K - N%K) == 1:
        print(1)
    else:
        ans = N%K
        print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a=n/k
if a >1:
    b=n%k
    if b==0:
        print(0)
    else:
        print(abs(b-k))
else:
    print(n)
    exit()

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
    
    if N < K:
        return N
    else:
        return N//K

print(solve(N, K))

Submission status: Wrong Answer

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

if N==0:
    print(0)
cand0=abs(N-K)
cand4=abs(cand0-K)
N=N%K
cand1=abs(N-K)
cand2=abs(cand1-K)
print(min(N,cand0,cand4,cand1,cand2))

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
i=1
z=[0]*2
saisyo=abs(a-b)
if saisyo>a:
  saisyo=a
u=a%b
w=(a-u)/b
z[0]=a-(w*b)
z[1]=abs(a-(w+1)*b)
r=int(min(z))


c=[saisyo,r]
print(min(c))

Submission status: Wrong Answer

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

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

if val1 >= val3:
    print(val3)
elif val1 >= val2:
    print(val2)
else:
    print(val1)

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

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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


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

Submission status: Wrong Answer

Solution code:
import fractions
import sys
N,K = map(int,input().split())
L = []
L.append(N)
i = 0
if N == 0:
    print(0)
    sys.exit(0)
if K == 1:
    print(0)
    sys.exit(0)
Q = 0
while True:
  L.append(abs(N-K))
  N = abs(N-K)
  i += 1
  if i > 10**5:
    Q = min(L)
    break
if N%2 == 0 and K % 2 == 0:
    print(min(2,N,Q))
else:
    print(min(abs(N-K),N,Q))

Submission status: Wrong Answer

Solution code:
def solve():
    N, M = map(int, input().split())
    a = abs(N-M)
    
    if a == 0:
        return 0
    
    if M == 1:
        return 0
        
    n = N
    while n > abs(n-M) and n != 0:
        n = min(n, abs(n-M))
    
    return min(n, abs(n-a))

print(solve())

Submission status: Wrong Answer

Solution code:
n, k = [int(i) for i in input().split()]
l = []
ans = n
while True:
  if n <= k:
    l.append(ans)
    break
  elif ans > 0:
    ans = ans - k
    l.append(ans)
  else:
    break

print(abs(min(l)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def gcd(m, n):
  if n == 0:
    return m

  return gcd(n, m % n)

ans = min(gcd(n, k), abs(gcd(n, k) - k))
print(ans)

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 N==0:
        a = K
        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 ==0:
  print(0)
  exit()
elif N <0:
  N = abs(N - K)

mod = N % K

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

print(be if be<=af else af)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#coding:utf-8
import math
n, k = map(int, input().split())

if n < k:
    print(n)
elif k == 1 or (k % 2 == 0 and n % 2 == 0):
    print(0)
elif (k % 2 == 0 and n % 2 == 1) or (n % 2 == 0 and k % 2 == 1):
    print(1)

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)
elif n < k:
    print(min(abs(n-k),n))
else:
    print(gcd(n,k))

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(min(n % k,abs(n % k - k)))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
if k == 1:
    print(0)
elif k == 2:
    if n % 2 == 0:
        print(0)
    else:
        print(1)
elif k == 3:
    if n%3 == 0:
        print(0)
    elif n%3 == 1:
        print(1)
    else:
        print(2)
elif k == 4:
    if n%4 == 0:
        print(0)
    elif n%4 == 1:
        print(1)
    elif n%4 == 2:
        print(2)
    else:
        print(3)
elif k == 5:
    if n%5 == 0:
        print(0)
    elif n%5 == 1:
        print(1)
    elif n%5 == 2:
        print(2)
    elif n%5 == 3:
        print(3)
    else:
        print(4)
else:
    while n > k:
        n = n-k
    if n < k-n:
        print(n)
    else:
        print(k-n)

Submission status: Wrong Answer

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

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 

cnt = 0
ans = x
while cnt<100:
  x_next = abs(x-K)
  ans = min(ans, x_next)
  cnt += 1

print(ans)

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()
if n == 0:
    print("0")
    exit()
while(1):
    
    ans = abs(ans-k)
    if ans == n:
        break
    if ans == 1 or 0:
        break
print(ans)

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

ans = N - K
f = 1
if(ans <= 0):
    print(N)
else:
    while f==1:
        if(ans <= 0):
            f = 0
        else:
            ans = ans - K
    print(abs(ans))

Submission status: Wrong Answer

Solution code:
def count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count

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

def input_list_str():
  return list(map(str, input().split()))

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

# 2で割り切れる回数
def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c

# 素因数分解
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

def main():
  n,k = input_list()
  if n == k:
    print(0)
    exit()
  if n > k:
    if n % 2 == 0 and k % 2 == 0:
      print(0)
      exit()
    if n % k == 0:
      print(0)
      exit()
    print(1)
  else:
    print(n)
  
import math
import fractions
import collections
import itertools
from functools import reduce
main()

Submission status: Wrong Answer

Solution code:
import math

#n,k
n,k = (int(x) for x in input().split())

a = math.floor(n / k)
b = n - (a*k)

#print(b)

c = abs(k-b)

if b < c:
    ans = b
else:
    ans = c

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
if k > n:
  print(n)
else:
  for i in range(n):
    if n < k:
      break
    else:
      n, k = k, n%k
      if k == 0:
        break
  print(n)

Submission status: Wrong Answer

Solution code:
def main():
    n, m = map(int, input().split())
    n = abs(n - (n // m) * m)
    print(n)

if __name__ == '__main__':
    main()

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=N%K
    N=int(N/a)
    K=int(K/a)
    mi=N
    t=N
    for i in range(100):
        t=abs(t-K)
        mi=min(mi,t)
    print(mi*a)

Submission status: Wrong Answer

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

if n < abs(n-k):
    pass

elif n > k:
    a = n // k
    n = n - a*k


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

print(n)

Submission status: Wrong Answer

Solution code:
def main():
    n,k = map(int,input().split(' '))
    n = n % k
    if(n != 0):
        key = 0
        data = []
        while(key == 0):
            check = abs(n - k)
            for per in data:
                if(check == per):
                    key = 1
            if(key == 0):
                data.append(check)
                n = check
            else:
                key = 1
    else:
        data = [1]
    print(min(data))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

print(n)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
ZET=N
b=0
c=N
b1=N
for i in range(1000000000000000000):
  c=abs(b1-K)
  if b>c:
    break
  b1=c
  b=c
print(c)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

def function(N,K):
    pre =N
    if N > K*10:
        return 0



    while(1):    
        # print('N',N)
        N = abs(N-K)
        if pre == N:
            return pre

        if pre< N:
            return pre
        pre = N 

print(function(N,K))

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=N%K
    N=int(N/a)
    K=int(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())
if n >= k:
    print(min(n%k,k-n%k))
elif n == 0:
    print(n)
else:
    print(min(n,n-k))

Submission status: Wrong Answer

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

print(f)

Submission status: Wrong Answer

Solution code:
import math

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

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

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


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

a = 0
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())
kari=int(N/K)
N=N-K*kari
print(min(N,abs(N-K)))

Submission status: Wrong Answer

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

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(min(n,k-abs(n-k),abs(n-k)))

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
#coding:utf-8
import sys,os
from collections import defaultdict, deque
from fractions import gcd
from math import ceil, floor
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7

    N,K = LMIIS()
    count = 0
    pre = 10**18+1
    x = N
    while True:
        pre = x
        x = N % K
        x,K = max(x,K),min(x,K)
        if pre == x or K == 0:
            break
    print(x)



    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

print(f)

Submission status: Wrong Answer

Solution code:
def solution():
    n, m = map(int, input().split())
    if n < m:
        print(n)
        return
    ans = n % m
    print(min(ans, abs(ans-m)))

solution()

Submission status: Wrong Answer

Solution code:
hoge = input().split()
N = int(hoge[0])
K = int(hoge[1])

if N / K > pow(10,18):
  N = N / pow(10,17)
if N / K > pow(10,17):
  N = N / pow(10,16)
if N / K > pow(10,16):
  N = N / pow(10,15)
if N / K > pow(10,15):
  N = N / pow(10,14)
if N / K > pow(10,14):
  N = N / pow(10,13)
if N / K > pow(10,13):
  N = N / pow(10,12)
if N / K > pow(10,12):
  N = N / pow(10,11)
if N / K > pow(10,11):
  N = N / pow(10,10)
if N / K > pow(10,10):
  N = N / pow(10,9)
if N / K > pow(10,9):
  N = N / pow(10,8)
if N / K > pow(10,8):
  N = N / pow(10,7)
if N / K > pow(10,7):
  N = N / pow(10,6)
if N / K > pow(10,6):
  N = N / pow(10,5)
if N / K > pow(10,5):
  N = N / pow(10,4)
if N / K > pow(10,4):
  N = N / pow(10,3)
if N / K > pow(10,3):
  N = N / pow(10,2)
  
_min = N
while 1:
  if _min <= abs(_min - K):
    break
  else:
    _min = abs(_min - K)

print(int(_min))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, M = map(int,input().split())
if N == 0 or M == 1:
    print(0)
    exit()
else:
    a = N//M
    N -= a
    print(min(N,abs(N-M)))

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

Submission status: Wrong Answer

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

if N >= K:
  print(N % K)
else:
  print(N)

Submission status: Wrong Answer

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

if N < K:
    print(N)
else:
    print(N%K)

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)
    if a <= ans:
        ans = a

print(str(ans))

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))
d=abs(n-k*(a-1))
print(min(min(b,c),d))

Submission status: Wrong Answer

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

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 N==0:
        a = K
        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())

ans = 0
if N ==K:
    ans = 0
elif N > K:
    if N%K == 0:
        ans = 0
    else:
        ans = N//K
else:
    ans = min(N, K-N)

print(ans)

Submission status: Wrong Answer

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

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:
# 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=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())

f = N // K
e = abs(N - (f + 1) * K)
bbb = abs(N-f*K)
if abs(N - f * K) > e:
  print(e)
else:
  print(abs(N-f))

Submission status: Wrong Answer

Solution code:
x = list(map(int,input().split(" ")))
n = x[0]
k = x[1]
import math
l = abs(n % k)
print(l)

if l > k//2:
    print(k-l)
else:
    print(l)

Submission status: Wrong Answer

Solution code:

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

if n == 0:
    print(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())
left = 0
right = 10**19

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

import sys
sys.setrecursionlimit(10**6)
ii = lambda: int(input())
mis = lambda: map(int,input().split())
lmis = lambda: list(mis())
INF = float('inf')

def main():
    n,k=mis()
    ans=n
    dis=abs(ans-k)
    if n>=k:
        print(n%k)
        exit()
    else:
        while ans>dis:
            ans=dis
            dis=abs(ans-k)
    print(ans)



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
if N >= K:
    if N%K == 0:
        print(0)
        exit()
    else:
        print(0)
        exit()
else:
    A = [N]
    M = N 
    N = abs(N-K)
    A.append(N)
    while N != M:
        N = abs(N-K)
        A.append(N)
    print(min(A))

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):
        d = int(N/K)
        a = N%K
        N = N - (d*K)

    print(abs(N-K))

Submission status: Wrong Answer

Solution code:
hoge = input().split()
N = int(hoge[0])
K = int(hoge[1])

if N / K > pow(10,18):
  N = N / pow(10,17)
if N / K > pow(10,17):
  N = N / pow(10,16)
if N / K > pow(10,16):
  N = N / pow(10,15)
if N / K > pow(10,15):
  N = N / pow(10,14)
if N / K > pow(10,14):
  N = N / pow(10,13)
if N / K > pow(10,13):
  N = N / pow(10,12)
if N / K > pow(10,12):
  N = N / pow(10,11)
if N / K > pow(10,11):
  N = N / pow(10,10)
if N / K > pow(10,10):
  N = N / pow(10,9)
if N / K > pow(10,9):
  N = N / pow(10,8)
if N / K > pow(10,8):
  N = N / pow(10,7)
if N / K > pow(10,7):
  N = N / pow(10,6)
if N / K > pow(10,6):
  N = N / pow(10,5)
if N / K > pow(10,5):
  N = N / pow(10,4)
if N / K > pow(10,4):
  N = N / pow(10,3)
if N / K > pow(10,3):
  N = N / pow(10,2)
  
_min = N
while 1:
  if _min <= abs(_min - K):
    break
  else:
    _min = abs(_min - K)

print(_min)

Submission status: Wrong Answer

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

ABS_1=abs(N-K)
ABS_2=abs(ABS_1-K)

if N%2==0:
    if K%2==0 or K==1:
        if ABS_1<N:    
            print(0)
        else:
            print(N)
        
    else:
        if ABS_1<N:
            print(1)
        else:
            print(N)
else:
    if K%2==0:
        if ABS_1<N:
            print(1)
        else:
            print(N)
    else:
        if ABS_1<N or K==1:
            print(0)
        else:
            print(N)

Submission status: Wrong Answer

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 k == 1:
    print(0)
else:
    print(cnt)

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:
        ans = N
    print(ans)

Submission status: Wrong Answer

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

for i in range(10000000000000000000):
    value=N%K
    if value<K:
        break    
    N=abs(N-K)

print(value)

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

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

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
def gcd(a,b):#a<=b
    while a!=0:
        a,b = b%a,a
    return b

if n > k:
    a = n
    n = k
    k = a

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):
    if(n%k==0):
        print(0)
    else:
        print(min((n%k),(n//k+1)*k-n))
elif(n==k):
    print(0)
elif(n==0):
    print(k)
else:
    if(k%n==0):
        print(n)
    else:
        if(k-n)<=n:
            print(k-n)
        else:
            print(n)

Submission status: Wrong Answer

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