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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if __name__ == '__main__':
    main()

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())
# lst = []
# leastnum = n

if n >= k:
    # nからずっとkを引く あまりと同じ 10 -3 - 3 - 3 == 10 - (3 * 3) == 10 % 3
    # ループに入るときの数字がn % kで ループする数数は固定のk からn % k を引いた数になるのでそれを比較
    x = n % k
    print(min(x, k - x))
else:
    # 最初から負の数になるから スタートが上のループと同じ
    # print(n)じゃだめ n:2 k:100 なら  98 2 98 2だが n:2 k:3 なら　1 2 1 2 だから
    print(min(n, k - n))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
amari=n%k
mainasu=n-(n//k+1)*k
if amari<abs(mainasu):
  print(amari)
else:
  print(abs(mainasu))

Submission status: Accepted

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

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


print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

a = N%K
b = K-N%K

ans = min(a,b)
print(ans)

Submission status: Accepted

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

x = N%K
ans = min(abs(x-K),x)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
n, k = map(int, input().split())
if n <= k:
    print(min(n, k - n))
else:
    t = (n - k - 1) // k + 1
    n = n - t * k
    print(min(n, k - n))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

a = int(n%k)
b = abs(int(k - n%k))
print(min(a, b))

Submission status: Accepted

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

rem = n%k
res = min(rem, abs(k-rem))
print(res)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
n%=m
if 2*n>m:
    print(m-n)
else:
    print(n)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
cl1 = input().split()
cl1 = [int(s) for s in cl1]
x = cl1[0]
k = cl1[1]

a = x % k
b = -(x % k - k)
print(min(a,b))

#517061501112

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect
import collections
import sys

sys.setrecursionlimit(100000)
input = sys.stdin.readline
ACMOD = 1000000007
INF = 1 << 62


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


def llmi(n):
    return [lmi() for _ in range(n)]


N, K = lmi()
tmp = N % K

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

Submission status: Accepted

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

x=abs(n-a*k)
y=abs(n-b*k)
z=abs(n-c*k)

print(min(x,y,z))

Submission status: Accepted

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

ans = N%K

if ans < abs(ans-K):
    print(ans)
else:
    print(abs(ans-K))

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = (int(x) for x in input().split())
i = n % k
if i > (k / 2):
  ans = abs(i - k)
else:
  ans = abs(i)
print(ans)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time
import random


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def main():
    N, K = MI()
    v = min(N % K, abs(N % K - K))
    print(v)
    # a = N

    # for i in range(100):
    #     N = random.randint(0, 10**5)
    #     K = random.randint(0, 10**5)
    #     a = N
    #     ans = 10**19
    #     for j in range(10**5):
    #         a = abs(a - K)
    #         ans = min(ans, a)

        # if v == ans:
        #     print('Yes')

        # for j in range(10**5):
        #     if N >= K:
        #         N = N % K
        #     else:
        #         K = K % N
        # print(N, K)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

N,K = map(int, sys.stdin.readline().strip().split())
a = N % K
print(min(a, K - a))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def readInts():
    return list(map(int,input().split()))
    
n,k = readInts()
a = n%k
ans = min(a, k-a)
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import Counter, deque

INF = float('inf')


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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:

def resolve():
    N, K = map(int, input().split())
    t = N%K
    return print(min(t, K-t))

if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

a = N%K

print(min(a,K-a))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

N, K = getInput()
t = N % K 
print(min(t, K -t))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

def main():
    n, k = [int(_) for _ in input().split()]
    
    m = abs(n - n // k * k)
    while True:
        ans = abs(m - k)
        if m <= ans:
            print(m)
            sys.exit()
        m = abs(m - k)
main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = (int(x) for x in input().split())
if n > k:
  n -= k*(n // k)
ans = min(n, k-n)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

a = N % K

if a - K >= 0:
  b = a - K
  
else:
  b = K - a
  
print(min(a,b))

Submission status: Accepted

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

Submission status: Accepted

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

main()

Submission status: Accepted

Solution code:
def absolute(N,K):
    return abs(N-K)
    
N,K=map(int,input().split()) 

valuelist = [N%K,absolute(N%K,K)]
     
print(min(valuelist))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def absolute(N,K):
    return abs(N-K)
    
N,K=map(int,input().split())
 
N=N%K
valuelist = []
 
for i in range (0,2):
    N=absolute(N,K)
    valuelist.append(absolute(N,K))
      
print(min(valuelist))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

min_val = N
memory = [N]
n = N
while True:
    if n > K*2:
        n = n % K
        memory.append(n)
        if min_val > n:
            min_val = n
        continue
    n = abs(K-n)
    if n in memory:
        break
    memory.append(n)
    if min_val > n:
        min_val = n
    if min_val == 0:
        break
print(min_val)

Submission status: Accepted

Solution code:
def absolute(N,K):
    return abs(N-K)
    
N,K=map(int,input().split())
 
N=N%K
valuelist = []
 
for i in range (0,2):
    i+=1
    N=absolute(N,K)
    valuelist.append(absolute(N,K))
            
print(min(valuelist))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if(x >= K):
    t=N%K
    x=min(t,abs(t-K))
else:
    x=min(x,K-x)

print(x)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7 # 998244353
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n, k = map(int,input().split())
    n %= k
    print(min(n, k - n))
resolve()

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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


def solve(N: int, K: int):
    # if N > K:
    print(min(N % K,abs(N % K-K)))
    # else :
    #     N = K - N
    #     print(min(N % K,abs(N % K-K)))

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    solve(N, K)

if __name__ == '__main__':
    main()

Submission status: Accepted

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()
  a = n % k
  b = k - a
  print(min(a, b))

import math
import fractions
import collections
import itertools
from functools import reduce
main()

Submission status: Accepted

Solution code:
def absolute(N,K):
    return abs(N-K)
    
N,K=map(int,input().split())

N=N%K
valuelist = [N%K]

for i in range (0,3):
    i+=1
    N=absolute(N,K)
    valuelist.append(absolute(N,K))
        
print(min(valuelist))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def absolute(N,K):
    return abs(N-K)
    
N,K=map(int,input().split())
N=N%K
valuelist = [N%K]

for i in range (0,3):
    i+=1
    N=absolute(N,K)
    valuelist.append(absolute(N,K))
    
    
print(min(valuelist))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import os
import math

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, K = il()
    q, mod = divmod(N, K)
    print(min(mod, abs(K - mod)))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(saisyou)

Submission status: Accepted

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

def replace(N,K):
    a = N%K
    return min(a,abs(a-K))

print(replace(N,K))

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

from math import ceil

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

Submission status: Accepted

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

def main():
  n, k = map(int, input().split())
  print(min(n%k, abs(n%k-k)))
  pass

if __name__=='__main__':
  main()

Submission status: Accepted

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

a = N%K

b = abs(a-K)

while a > b:
    a = b
    b = abs(b-K)

print(a)

Submission status: Accepted

Solution code:
import sys
import math

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

pre=abs(n-math.floor(n/k)*k)

while 1:
    if pre <= abs(pre-k):
        print(pre)
        sys.exit()
    pre = abs(pre - k)

Submission status: Accepted

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

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

Submission status: Accepted

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

K = N%X
k = X - K

if K == 0:
  print(0)
elif K >= k:
  print(k)
else:
  print(K)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math

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

pre=abs(n-(math.floor(n/k)*k))

while 1:
    if pre <= abs(pre-k):
        print(pre)
        sys.exit()
    pre = abs(pre - k)

Submission status: Accepted

Solution code:
import sys
import math

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

pre=abs(n-n//k*k)

while 1:
    if pre <= abs(pre-k):
        print(pre)
        sys.exit()
    pre = abs(pre - k)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# C
import math
def my_round(val, digit=0):
    p = 10 ** digit
    return (val * p * 2 + 1) // 2 / p

n, k = map(int, input().split())
s = n / k
print(min(n%k,k-(n%k)))

Submission status: Accepted

Solution code:
X = list(map(int, input().split()))
N = X[0]
K = X[1]
t = N % K
s = K - t
if t <= s:
  print(t)
else:
  print(s)

Submission status: Accepted

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

if __name__ == "__main__":
    N, K = MI()
    print(min(N%K, K-N%K))

Submission status: Accepted

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

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:
    r=n%k
else:
    r=n
print(min(r,k-r))

Submission status: Accepted

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

if l >= abs(l-k):
    print(abs(l-k))
else:
    print(l)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
nums = list(map(int, input().split()))
n = nums[0]
k = nums[1]

n  = n % k

print(min(n, k - n))

Submission status: Accepted

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

a = abs(N%K)
b = abs(N%K - K)

if a < b:
    print(a)
else:
    print(b)

Submission status: Accepted

Solution code:
x, y = map(int, input().split())
tmp = 10**20
k = x // y
x = abs(x-y*k)
while tmp >= x:
    tmp = x
    x = abs(x-y)
print(tmp)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
 
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
#このように書いておくことですぐに使える

def main():
    #mod=10**9+7  これは必要なのかわからない
    N,K=MI()
    a=N%K     #kで割ったあまり
    b=K-N%K   #kで割ったあまりがkより小さい時はKとの差を求める
    print(min(a,b))
 
main()

Submission status: Accepted

Solution code:
def c_replacing_integer():
    N, K = [int(i) for i in input().split()]
    return min(N % K, K - (N % K))

print(c_replacing_integer())

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:
import sys
import math

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

pre=n-n//k*k

while 1:
    if pre <= abs(pre-k):
        print(pre)
        sys.exit()
    pre = abs(pre - k)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

be=n%k
af=abs(k-be)

print(be if be<=af else af)

Submission status: Accepted

Solution code:
import sys

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

tmp = N

if N >= K:
    if N % K == 0:
        print(0)
        sys.exit()
    else:
        N = N % K

while True:
    #print(N)
    if abs(N - K) < tmp:
        N = abs(N - K)
    else:
        print(tmp)
        sys.exit()
    tmp = N

Submission status: Accepted

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

a = 0
b = 0
c = 0

a = n//k
b = abs(n-k*(a+1))
c = abs(n-k*a)
if b < c:
	print(b)
elif c < b:
	print(c)

Submission status: Accepted

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

if n >= k:
    n %= k

print(min(n, k-n))

Submission status: Accepted

Solution code:

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

while True:
  if abs(min-k)>=min:
    break
  else:
    min=abs(min-k)

print(min)

Submission status: Accepted

Solution code:
a = list(map(int, input().split()))
if a[0] == 0 or a[1] ==1:
  print(0)
else:
  if a[0]%a[1] > a[1]/2:
    print(a[1] - a[0]%a[1])
  else:
    print(a[0]%a[1])

Submission status: Accepted

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

if t > (K -t):
    print(K-t)
else:
    print(t)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(300000)

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


N, K = MI()

if N == K:
    print(0)
elif N > K:
    N = N - (N // K) * K
    if K < 2 * N:
        print(abs(N - K))
    else:
        print(N)
elif K < 2 * N:
    print(abs(N - K))
else:
    print(N)

Submission status: Accepted

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

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

Submission status: Accepted

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

a = N % K 

print(min(a, K - a))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n, m = (int(x) for x in input().split())

a = n % m
b = n / m
if abs(a-m) > a:
    print(a)
else:
    print(abs(a-m))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a,b=map(int,input().split())
ans=[a,a%b]
for i in range(10000):
    ans.append(abs(ans[i]-b))
print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# coding : utf-8

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

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

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
N,K= list(map(int,input().split()))
a = int(N/K)
a1 = a*K
a2 = (a+1)*K
if N % K ==0:
    Num = 0
else:
    if abs(N%K) < abs((N%K)-K) :
        Num = abs(N%K)
    else:
        Num =  abs((N%K)-K)
    
print(Num)

Submission status: Accepted

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

ans = N%K

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

Submission status: Accepted

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