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: Accepted

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


if n > k:
    n = n%k
    ans = min(k-n,n)  
else:
    ans = min(k-n,n)

print(ans)

Submission status: Accepted

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

if N > K:
    N = N%K
# elif N < K:
#     print(N)
#     sys.exit()

ans = [N]
for _ in range(100):
    ans.append(abs(N-K))
    N = abs(N-K)

print(min(ans))

Submission status: Accepted

Solution code:
L=list(map(int, input().split()))
N=L[0]
K=L[1]
m0=0
m1=0
m2=0
m3=0
m=0

if N==K:
    print(0)
else:
    m0=N
    m1=abs(N % K)
    m2=abs((N % K) - K)
    m3=abs(N-K)
    m=min(m0,m1,m2,m3)
    print(m)

Submission status: Accepted

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

amari = n%k

if abs(amari-k) < amari:
    print(abs(amari-k))
else:
  print(amari)

Submission status: Accepted

Solution code:

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

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

set_list = {N}
len_list = -1

while len(set_list) != len_list:
    # print(set_list)
    len_list = len(set_list)
    if N >= M:
        set_list.add(N%M)
        N = N%M
    else:
        set_list.add(abs(N-M))
        N = abs(N-M)

    # print(set_list, len_list, N%M)
print(min(set_list))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
answer = 0
if k >= n:
  gap = k-n
  if gap > n:
    answer = n
  else:
    answer = gap
else:
  sho = n // k
  if n % k == 0:
    dec = sho - 1
  else:
    dec = sho
  n = n - k*dec
  if k >= n:
    gap = k-n
    if gap > n:
      answer = n
    else:
      answer = gap
print(answer)

Submission status: Accepted

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



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#162.C
N,K = map(int,input().split())
a = N//K
if abs(K*(a+1)-N)<abs(K*a-N):
    ans = abs(N - (a+1)*K)
else:
    ans = abs(N-a*K)

print(ans)

Submission status: Accepted

Solution code:
N, K = [int(i) for i in input().split()]
r = N % K
ans = min(r, abs(r-K))
print(ans)

Submission status: Accepted

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

if N//K==0:
    if abs(N-K) > N:
        print(str(N))
    else:
        print(str(abs(N-K)))
elif N%K==0 or N==0:
    print("0")
elif K==2:
    print("1")
else:
    if a < b:
        print(str(a))
    else:
        print(str(b))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

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

if N < K:
    print(min(N, K - N))
    exit()

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

Submission status: Accepted

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

print(min(abs(m-k), abs(n-k), n) if m!=0 else m)

Submission status: Accepted

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

Submission status: Accepted

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

ans1 = N % K
ans2 = abs(ans1 - K)

if ans1 < ans2:
    print(ans1)
else:
    print(ans2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from fractions import gcd
N, K = map(int, input().split())
if (N > K):
    N = N % K
print(min(N, K - N))
exit(0)

Submission status: Accepted

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

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


else:
    print(min(amari,k-amari))

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

Submission status: Accepted

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

val1 = N % K
val2 = K - val1

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

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest


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

    if n == 0:
        print("0")
        return

    if k == 1:
        print("0")
        return

    # いつ最小になるか = 桁が変わる前後のタイミング。
    q, mod = divmod(n, k)
    # print(q, mod)
    # 7 と　4　の場合：1　あまり 3。

    # 3 から 一回だけ引いてみる。
    minus_edge = abs(mod - k)

    print(str(min(mod, minus_edge)))




class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """7 4"""
        output = """1"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """2 6"""
        output = """2"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """1000000000000000000 1"""
        output = """0"""
        self.assertIO(input, output)

if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
def min(a, b):
    if a >= b:
        res = a - b
    elif a < b:
        res = b - a
    return res

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

min_num = float('inf')
res_list = []
while(1):
    res = min(n, k)
    if res < min_num:
        min_num = res
    if res == 0:
        break
    if res in res_list:
        break
    n = res
    res_list.append(res)

print(min_num)

Submission status: Accepted

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

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

	for i in range(100000):
		a = min(a,abs(a-b))
	return a
if n == 0:
	print(0)
elif n ==1:
	if k == 1:
		print(0)
	else:
		print(1)
elif k == 1:
	print(0)
else:
	print(process(n,k))

Submission status: Accepted

Solution code:


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

A = N % K

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

Submission status: Accepted

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

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

#while n > abs(n-k):
#    n = abs(n-k)
while 2*n > k:
    
    if n > k:
        n = n%k
    elif n <= k:
        n = k-n

print(n)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

if n % k == 0:
    print(0)
else:
    ans1 = n % k
    ans2 = k - ans1

    print(min(ans1,ans2))

Submission status: Accepted

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

aaans = abs(N-K)

if N == K:
    ans = 0
elif a == 1:
    ans = N
else:
    ans = (a*K)-N
    
aa = (N//K)
if N == K:
    aans = 0
elif aa == 1:
    aans = N
else:
    aans = N - (aa*K)

aaaans = min(ans,aans)
print(min(aaans,aaaans))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
a = []
if n % k == 0:
  print(0)

elif n // k == 0 :
  if -(n - k) > n :
    print(n)
  else :
    print(-(n - k))

else :
  if n % k > (n % k) - k :
    print(-((n % k) - k))
  else :
    print(n % k)

Submission status: Accepted

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

  
if abs(c)>abs(d):
    print(str(abs(d)))
else:
    print(str(abs(c)))

Submission status: Accepted

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

x = min(N%M, M-N%M)
print(x)

Submission status: Accepted

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

if n % k == 0:
    print('0')
else:
    ans = n % k
    if ans > abs(ans - k):
        ans = abs(ans - k)
    print(ans)

Submission status: Accepted

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

flag = True

min = N

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

print(ans)

Submission status: Accepted

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

f = N // K
e = abs(N - (f + 1) * K)

if abs(N - f * K) > e:
  print(e)
else:
  print(abs(N - f * K))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# スペース区切りの整数の入力
N, K = map(int, input().split())

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

Submission status: Accepted

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

min = N



while True:
  if N >= K:
    N = N % K
  else:
    N = K - N

  if N < min:
    min = N
  elif N == min:
    break
  
print(min)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

# N = 1000000000
# K = 1

def func(N,K):
    prev = N 

    while(1):
        N = abs(N-K)
        if prev<=N:
            return prev
        prev = N


def fun(N,K):
    N = (N % K) 
    return N  

S=fun(N,K)
T = func(S,K)
# print('S',S)
# print('T',T)
print(T)

Submission status: Accepted

Solution code:
# coding: utf-8

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

remainder = N % K
another_N = remainder

while(N >= another_N):
    N = another_N
    another_N = abs(N - K)
    
print(N)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/python3 
n,k = map(int,input().split())
n = abs(n-k) % k
if 2*n > k:
    n = abs(n-k)
print(n)

Submission status: Accepted

Solution code:

def resolve():
    import sys
    input = sys.stdin.readline
    n, k = [int(x) for x in input().rstrip().split()]
    n_set = set()
    while n not in n_set:

        n_set.add(n)
        if n == 0:
            break

        n = abs(k-n)
        if k < n:
          n = n - k*(abs(k-n)//k)
        # print(n)
        # print(n_set)

    print(min(n_set))

resolve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if amari == 0:
    result = 0
else:
    if amari>= (K-amari):
        result = K-amari
    else:
        result = amari
print(int(result))

Submission status: Accepted

Solution code:
#N 初期値　K 差とれるやつ
N, K = map(int, input().split())
X = N%K
ans=min(X, K-X)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

if K > N :
    print( min(N, K-N) )
else :
    temp = N//K
    N = N-(temp*K)
    print( min(N, K-N))

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
N=N%K
while(1):
    m=N
    N=abs(N-K)
    if(m<=N):
        print(m)
        exit()

Submission status: Accepted

Solution code:
def main(n: int, k: int):
    m = min(n, n % k)

    print(min(m, abs(m - k)))


if __name__ == "__main__":
    n, k = map(int, input().split())
    main(n, k)

Submission status: Accepted

Solution code:
def replaceInt(N, M):
    N = abs(N-M)
    return N
    
N, M = [int(i) for i in input().split()]

if N == M :
    output = 0
elif N < M :
    if N <= M / 2:
        output = N
    else:
        output = abs(M-N)
else :
    if M == 1:
        output = 0
    else :
        N = N % M
        output_candidates = []
        while True:
            N = replaceInt(N,M)
            if N in output_candidates:
                break
            output_candidates.append(N)
        output = min(output_candidates)
        
print(output)

Submission status: Accepted

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

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

if n == 0 or n == k:
    print(0)
elif k == 1:
    print(0)
else:
    if n > k:
        r = n % k
        if r == 0:
            print(0)
        else:
            n_1 = abs(r - k)
            n_2 = abs(n_1 - k)
            print(min(n_1, n_2))
    else:
        n_1 = abs(n - k)
        n_2 = abs(n_1 - k)
        print(min(n_1, n_2))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys

N, K = map(int, input().split())
if K == 1:
  print(0)
  sys.exit()
  
if N / K > 1:
  N = N - int(N / K) * K
a = abs(N-K)
while abs(a - K) < a:
  a = abs(a-K)

print(int(a))

Submission status: Accepted

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

if N <= K:
    print(min(K-N, N))
# elif K == 1:
#     print(0)
else:
    print(min(N % K, K - N % K))
    # a = N // K
    # b = ceil(N / K)
    # c = N % K
    # print(a, b, c)
    # print(abs(c-K), abs(c))

    # x = N // (N - K)
    # y = x + 1
    # z = x - 1
    # print(x, y, z)
    # a = N - x * (N-K)
    # b = N - y * (N-K)
    # c = N - z * (N-K)
    # print(a, b, c)
    # ans = min(abs(a), abs(b), abs(c))
    # print(ans)

# if N >= K:
#     ans = min(abs(a), abs(b))
# # K > N:
# else:
#     ans = min(K-N, N)
# print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

    if K == 1:
        print(0)
        return

    N = N % K

    while abs(N - K) < N:
        N = abs(N - K)

    print(N)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

loop = True
last2 = 0
last1 = 0
current = N % K
while loop:
  current = abs(K - current)

  if (current == last2):
    loop = False
    break
  else:
    last2 = last1
    last1 = current
    continue

result = last1 if (last1 < last2) else last2
print(result)

Submission status: Accepted

Solution code:
n, k = [int(i) for i in input().strip().split()]
print(min(n % k, abs(n % k - k), n))

Submission status: Accepted

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


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

    div = N // K
    print(min(abs(N - K * div), abs(N - K * (div + 1))))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
mi = n
while mi >= n and mi > 0:
    if n < mi:
        mi = n
    # n = abs(n - k)
    # print(mi)
    if int(mi/k) > 1:
        mi = abs(mi - (k*int((mi/k))))
        n = mi
        continue
    n = abs(n - k)

print(mi)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
while N>100000000000000000*K:
  N=N-100000000000000000*K
while N>100000000000000*K:
  N=N-100000000000000*K
while N>1000000000000*K:
  N=N-1000000000000*K
while N>10000000000*K:
  N=N-10000000000*K
while N>100000000*K:
  N=N-100000000*K
while N>1000000*K:
  N=N-100000*K
while N>1000*K:
  N=N-1000*K
while N>100*K:
  N=N-100*K
while N>10*K:
  N=N-10*K
while N>K:
  N=N-K

N2=N-K
if N2<0:
  N2=-N2

if N<N2:
  print(N)
else :
  print(N2)

Submission status: Accepted

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

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

Submission status: Accepted

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

def calc_next_x(n,k):
    if n > k:
        return x % k
    else:
        return abs(n-k)

x_prev = 10 ** 18 + 1
x = n
while x < x_prev:
    x_prev = x
    
    x = calc_next_x(x, k)

print(min(x, x_prev))

Submission status: Accepted

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

div =N//K
N=N-div*K

if K!=1:
    while True:
        N=abs(N-K)
        tmp=N
        if abs(N-K)>tmp:
            print(tmp)
            exit(0)
else:
    print(0)

Submission status: Accepted

Solution code:
import numpy as np
N, K = map(int, input().split())
ans = min(
    (N - (N//K)*K), np.abs(N - (N//K + 1)*K), np.abs(np.abs(N - (N//K + 1)*K) - K)
    )
print(ans)

Submission status: Accepted

Solution code:
N, K = input().split()
s = int(N) % int(K)
t = s - int(K)
if t < 0:
  t = t * (-1)
  
print(min(s, t))

Submission status: Accepted

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

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

print(N)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s=input().split()

N=int(s[0])
K=int(s[1])

if N > K/2 and N < K:
    print(abs(N-K))

if N <= K/2:
    print(N)

if N==K:
    print(0)



if N > K:

    


    if N >2*K:
        L=N//K
        N=N-L*K
    else:
        N=N-K
    

    if N > K/2 and N < K:
        print(abs(N-K))

    if N <= K/2:
        print(N)

    if N==K:
        print(0)

Submission status: Accepted

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

mod = n % k
candidate = abs(mod - k)

print(min(mod, candidate))

Submission status: Accepted

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

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

Solution code:
N, K = map(int, input().split())
if N == 0:
    print(N)
elif int(K / N) > 1:
    print(N)
else:
    amari = N % K
    mainasu_noatai = abs(amari - K)
    if (amari > mainasu_noatai):
    	print(mainasu_noatai);
    else:
        print(amari)

Submission status: Accepted

Solution code:
import sys
a = [int(i) for i in input().split()]
N = a[0]
K = a[1]

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


def resZ(i, K):
    return abs(i - K)

ab = resZ(N, K)
# print(ab)
if N < ab:
    print(N)
    # print('end')
    sys.exit()

N = N % K
while True:
    ab = resZ(N, K)
    # print(ab)
    if N < ab:
        print(N)
        # print('end')
        sys.exit()

    N = ab
# def

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

mod = n % k
kari = mod - k

if abs(mod) < abs(kari) :
    print(abs(mod))
else:
    print(abs(kari))

Submission status: Accepted

Solution code:
NK = input().split()
N = int(NK[0])
K = int(NK[1])
if N%K<K-N%K:
    print(N%K)
else:
    print(K-N%K)

Submission status: Accepted

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

Submission status: Accepted

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

if N==0 or K==1:
    print(0)
    exit()

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

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
#L = list(map(int,input().split()))
#a = max(n,k)%min(n,k)


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

Submission status: Accepted

Solution code:
#coding: utf-8

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

N1 = N % K
N2 = abs(N1-K)
if N == 0:
    print(0)
elif N1<N2:
    print(N1)
else:
    print(N2)

Submission status: Accepted

Solution code:
from sys import stdin
import sys

N, K = [int(x) for x in stdin.readline().rstrip().split()]
ans = N
temp = 0

if N % K == 0:
    print("0")
    sys.exit()
N = N % K
while N != K:
    # print(ans, abs(N-K), abs(temp-K))
    N = abs(N-K)
    # print(ans, abs(N-K), abs(temp-K))
    if abs(N-K) == temp:
        print(ans)
        sys.exit()
    temp = N
    if N < ans:
        ans = N

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def judge(n, k, pre):
  if n > k:
    sa = n - k * (n // k)
  else:
    sa = k - n
  
  if sa > pre:
    return pre
  else:
    return judge(sa, k, sa)
    

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

print(judge(N, K, N))

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

    dist = abs(n-k)

    if n < dist:
        print(n)
        exit()

    if n == dist:
        print(0)
        exit()
                
    print(abs((n // k + 1) * k - n))
    exit()


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

else:
  p = (n+k)%k
  q = abs(p-k)
  if p > q:
    print(q)
  else:
    print(p)

Submission status: Accepted

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

Submission status: Accepted

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

if n > float(k/2):
    n = abs(n-k)

print(n)

Submission status: Accepted

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

ans1 = N%K
ans2 = K - N%K

print min(ans1, ans2)

Submission status: Accepted

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

if N>=K:
    amari = N % K
    X = K - amari
    if X > amari:
        x = amari
    else :
        x = X
else:
    if N >= (K-N):
        x = K-N
    else:
        x = N        


print(x)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

t = n

if n % k == 0:
  print(0)
else:
  if n < k:
    a = abs(n - k)
    b = abs(a-k)
    if a < b:
      print(a)
    else:
      print(b)
  else:
  	x = n % k
  	print(k - x)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

main()

Submission status: Accepted

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

Submission status: Accepted

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

    r = N%K
    if r == 0:
        print(0)
    else:
        if 2*r > K:
            print(abs(r - K))
        else:
            print(r)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

diff = n - k
n = n - (diff // k) * k

while n > k:
    n = n - k

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

print(min(n,k-n))

Submission status: Accepted

Solution code:
import math

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

while True:
    if min(n, k) == 0:
        break
    if n <= k/2:
        break
    m = max(n, k) % min(n, k)
    
    if m < n:
        n = m
    else:
        break

print(n)

Submission status: Accepted

Solution code:
in1 = input().split()
in1num = [int(x) for x in in1]
N = in1num[0]
K = in1num[1]
#print(N, K)

#while abs(N-K) < N:
#  N = abs(N-K)
#print(N)

q, mod = divmod(N, K)
#print(q, mod)
if mod == 0:
  print(0)
else:
  if K - mod < mod:
    print(K-mod)
  else:
    print(mod)

Submission status: Accepted

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

M = abs(K - N%K)
print(min(M, N%K))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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