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(" "))
a = n % k
b = (k - a) % k
#print(str(a) + " " + str(b))
ans = min(a, b)
print(ans)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(1000000)


def dfs(g: {}, v: int, path_list: [], added: {}):
    path_list.append(v)
    for next_v in g[v]:
        # # next_v が探索済だったらスルー
        if next_v in path_list:
            continue
        # 距離３なら追加可能か確認して次へ
        if len(path_list) == 3:
            if path_list[0] not in g[next_v]:
                dic_a = added.setdefault(path_list[0], set())
                dic_a.add(next_v)
                dic_b = added.setdefault(next_v, set())
                dic_b.add(path_list[0])
        # 距離1なら複製して次へ
        elif len(path_list) == 1:
            copied = [v]
            dfs(g, next_v, copied, added)
        # 距離2なら複製して次へ
        elif len(path_list) == 2:
            copied = path_list.copy()
            dfs(g, next_v, copied, added)


def main():
    n, k = map(int, input().split())
    if k == 1:
        print(0)
        return

    y = n // k
    x = n - k*y

    x1 = x
    x2 = abs(x - k)
    print(min(x1, x2))



main()

Submission status: Accepted

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

a = n // k
b = a + 1

print(min(abs(n-(k*a)), abs(n-(k*b))))

Submission status: Accepted

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

a = N - K
if a < 0:
  a += K
b = a % K
if K-b < b:
  b = K-b
print(b)

Submission status: Accepted

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

if N >= K:
    N = N % K

print(min(K - N, N))

Submission status: Accepted

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

n %= k

n = abs(n)

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def resolve():
    N, K = map(int, input().split())
    ans = N-(N//K)*K
    if ans > K/2:
        ans = abs(ans - K)
    print(ans)
resolve()

Submission status: Accepted

Solution code:
N, K = list(map(int, input().split()))
x = N // K
result = N - (K * x)
print(min(result, abs(result - K)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

alist = []

tmp = abs(n % k)
for i in range(10000):
    alist.append(tmp)
    tmp = abs(tmp - k)
    
print(min(alist))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if n % k == 0:
    print(0)
elif n < k < 2 * n:
    print(k-n)
elif n * 2 <= k:
    print(n)
else:
    print(min(abs(n - k * (n // k)), abs(n - k * (n // k + 1))))

Submission status: Accepted

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

while True:
    n = abs(x1 - k)
    x2 = abs(n - k)
    if x1 >= n and n <= x2:
        print(n) 
        sys.exit()
    else:
        x1 = n

Submission status: Accepted

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

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

Submission status: Accepted

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

def solve():
    global n, k
    n = n - (k * (n // k))
    pre = n
    while True:
        n = min(n, abs(n-k))
        if n == pre:
            break
        pre = n
    return n

print(solve())

Submission status: Accepted

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

n = N % K

ans = min(n,K-n)

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin, stdout

N, K = map(int,stdin.readline().rstrip().split())

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n, k = map(int, input().split())
    c = abs(n % k)
    r = min(c, k - c)
    print(r)

Submission status: Accepted

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

t1 = n%k
t2 = abs(t1-k)

print(min(t1,t2))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

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

Submission status: Accepted

Solution code:
# import sys
# import math
# import itertools
# from collections import deque
# from collections import defaultdict
# import heapq
# import copy
# import bisect
# import numpy as np
# from scipy.special import comb

# def my_input(): return sys.stdin.readline().rstrip()

def main():
    N, K = map(int, input().split())
    dt = N//K
    ans = min(abs(N - dt * K), abs(N - (dt+1)*K))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict

n,k = map(int, input().split())
dic = defaultdict(int)
prev = []
if n%k==0:
    print(0)
    exit()

    


while True:
    if n > k:
        m = n//k
        ab=n-m*k
    else:
        ab = abs(n-k)
    if dic[ab] == 0:
        prev.append(ab)
        dic[ab] = 1
        n = ab
    else:
        prev.sort()
        break

print(prev[0])

Submission status: Accepted

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

A = N%K

if A >= K/2:
    A = K - A

print(A)

Submission status: Accepted

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

mod1 = n%k
mod2 = abs(n%k - k)

print(min(mod1, mod2))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

P = N % K
Q = K - P

if P > Q:
  print(Q)
else:
  print(P)

Submission status: Accepted

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

"""
while N > K:
  N = abs(N - K)
  #print(N)

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

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

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

"""
while N > K:
  N = abs(N - K)
  #print(N)

print(min(N, abs(N-K)))
"""
"""
m = -(-N//K)
print(abs(N - m*K))
"""

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

Submission status: Accepted

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

Submission status: Accepted

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


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
se_ = set([N])
a = N % K
se_.add(a)
while True:
    a = max(K-a,a-K)
    if a in se_:
        break
    se_.add(a)

print(min(se_))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
def f(n, k):
    if n % k == 0:
        print(0)
    else:
        n1 = n % k
        n2 = abs(n1 - k)
        print(min(n1, n2))

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

Submission status: Accepted

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

if N > K:
    N = N % K

ans = min(N, K - N)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
x = N%K
check = [x]

if not x == 0:
    while True:
        x = abs(x-K)
        if x in check:
            break
        check.append(x)
print(min(check))

Submission status: Accepted

Solution code:
# For taking space seperated integer variable inputs.
def invr():
    return(map(int, input().split()))


n, k = invr()
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 - n//k * k, -1 * (n - k * (n//k + 1))))

Submission status: Accepted

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

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

lst = []
lst.append(a)
lst.append(b)
print(min(lst))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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=[]
tmp=N%K

for i in range(10**18):
    tmp=abs(tmp-K)
    #print(tmp)
    #print(i)
    if i == 0:
        ans.append(tmp)
    elif tmp>ans[i-1]:
        ans.append(tmp)
        break
    else:
        ans.append(tmp)

print(min(ans))

Submission status: Accepted

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

N = s[0]
K = s[1]

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

Submission status: Accepted

Solution code:
def main():
        N, K = map(int, input().split())
        # K: fixed
        ans = 0
        r = N%K

        if N%K==0:
                ans = 0
        else:
                ans = min(r, abs(r-K))

        print("{}".format(ans))

if __name__=="__main__":
        main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

    print(ans)


if __name__ == "__main__":
    main()

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:
n,k = map(int,input().split())
ans =[k]
mod = n%k 
ans.append(mod)

ans.append(k - mod) 
print(min(ans))

Submission status: Accepted

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

N_set = set([])
N_set.add(float('inf'))
N_new = N

while (N_new > 0 and N_new < min(N_set)):
    N_set.add(N_new)
    if N_new >= K:
        N_new = N_new % K
    else:
        N_new = K - N_new
 
N_set.add(N_new)

ans = min(N, min(N_set))

print(ans)

Submission status: Accepted

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

if n <= k//2:
  print(n)
elif n < k:
  print(k-n)
elif n == k:
  print(0)
else:
  plus = n-n//k*k
  minus = abs(n-n//k*k-k)
  print(min(plus,minus))

Submission status: Accepted

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

x = abs(n%k)
y = abs(n%k - k)
print(min(x,y))

Submission status: Accepted

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

Submission status: Accepted

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n, k = map(int, input().split())
    mod = n % k
    print(min(mod, k - mod))


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Accepted

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

N = N % K

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if N % K == 0:
    res = 0
elif N % K > K / 2:
    res = K - (N % K)
else:
    res = N % K

print(res)

Submission status: Accepted

Solution code:
from math import ceil
N, K = map(int, input().split())


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

Submission status: Accepted

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

if(n <= k):
    ans = min(abs(k-n),n)
else:
    m = n//k
    ans = min(abs(n-m*k),abs(n-(m+1)*k))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans1=N%K
ans2=K-ans1

if ans1<=ans2:
    print(ans1)

else:
    print(ans2)

Submission status: Accepted

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

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

Submission status: Accepted

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

# もっと簡単に書けばこうか でもなぜ上は通らない？
print(min(n%k, k-(n%k)))

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

loop = False
nmin = 1e+18

if n > k:
    n = n%k
while not loop:
    n = abs(n-k)
    if nmin > n:
        nmin = n
    else:
        loop = True
        print(nmin)
        break

Submission status: Accepted

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

Submission status: Accepted

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



if n>k:
        a=n//k
        b=n-a*k
        print(min(b,abs(n-(a+1)*k), abs(2*n-(a+1)*k)))
else:
        print(min(n,k-n))

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

Submission status: Accepted

Solution code:
#k=int(input())
n,k=map(int,input().split())
#hl=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]
t=n%k
print(min(t,k-t))

Submission status: Accepted

Solution code:
def solve(string):
    n, k = map(int, string.split())
    n %= k
    return str(min(n, abs(n - k)))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

Solution code:
def solve(string):
    n, k = map(int, string.split())
    n %= k
    while n > abs(n - k):
        n = abs(n - k)
    return str(n)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

Solution code:
def main():
    N, K = list(map(int, input().split()))
    m = N % K
    m = min(m, abs(K-m))
    print(m)
 
if __name__ == '__main__':
    main()

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

if List[0]%List[1] >= abs(List[0]%List[1] - List[1]):
    print(abs(List[0]%List[1] - List[1]))
else:
    print(abs(List[0]%List[1]))

Submission status: Accepted

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

import sys
sys.setrecursionlimit(10**7)
from pprint import pprint as pp
from pprint import pformat as pf
# @pysnooper.snoop()
#import pysnooper # debug

import math
#from sortedcontainers import SortedList, SortedDict, SortedSet # no in atcoder
import bisect


if __name__ == '__main__':
    x, k = list(map(int, input().split()))
    m = math.inf
    if x > k:
        x = x % k
    for _ in range(3):
        x = abs(x - k)
        m = min(x, m)
    print(m)


    #print('\33[32m' + 'end' + '\033[0m') # debug

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

main()

Submission status: Accepted

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

s = N % K

print(min(s, K-s))

Submission status: Accepted

Solution code:
N,K = map(int,(input().split()))
flg=0
first_mod=0
while(flg==0):

  if(first_mod==0):
    N=N%K
    first_mod=1
    
  new_N=abs(N-K)

  if(new_N>N):
    flg=1
  else:
    N=new_N
print(N)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

diff_abs_list = []
diff_list = []
judge = 0
count = 0

if N <= K:
    answer = min(abs(N - K), abs(abs(N - K) - K)) 
elif N % K == 0:
    answer = 0
else:
    while True:
        N = N % K
        judge = N -K
        N = abs(N - K)
        diff_abs_list.append(N)

        if judge < 0:
            break
        judge = 0
        count += 1
    answer = min(diff_abs_list[count -1], diff_abs_list[count])

print(answer)

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 = list(map(int, input().split()))
print(min(N%K, K-(N%K)))

Submission status: Accepted

Solution code:
N, K = (int(i) for i in input().split())
print(min((N-K*(N//K)),abs((N-K*(N//K+1)))))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
import sys
from collections import Counter
from itertools import permutations, combinations
from fractions import gcd
#from math import gcd
from math import ceil, floor
import bisect
sys.setrecursionlimit(10 ** 6)
inf = float("inf")

def input():
    return sys.stdin.readline()[:-1]

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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


def main():
    n, k = map(int, readline().rstrip().split())
    print(min(n % k, k - n % k))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

a = N % K

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
n %= k
visited = set()
ans = n
while True:
  visited.add(n)
  n = abs(n-k)
  ans = min(ans, n)
  if n in visited:
    break
print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

N=N-(N//K)*K
while True:
    N_new=abs(N-K)
    if N_new>=N:
        print(N)
        break
    N=N_new

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

m = 10**18
n = n % k
while True:
    n = min(n, abs(n-k)) 
    if n < m:
        m = n
    else:
        break

print(m)

Submission status: Accepted

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

x = n % k
y = abs(x - k)

print(min(x, y))

Submission status: Accepted

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

r = x%k
ans = min(abs(r-k),r)
print(ans)

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 = [int(hoge) for hoge in input().split()]
print(min(N%K,K-N%K))

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 = map(int,input().split())
X = N % K
ans = min(K-X,X)
print(ans)

Submission status: Accepted

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

a = n // k

if n - k * a <= -(n - k * (a + 1)):
    print(n - k * a)

else:
    print(-(n - k * (a + 1)))

Submission status: Accepted

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

print(min(a,b))

Submission status: Accepted

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

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

Submission status: Accepted

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

num = n // k
print(min(abs(n-num*k),abs(n-(num+1)*k)))

Submission status: Accepted

Solution code:
import math
def py():
    print("Yes")
def pn():
    print("No")
def iin():
    x = int(input())
    return x

neko = 0
nya = 0
nuko = 0

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

Submission status: Accepted

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

if M==1:
    print(0)
    exit()
t1= N//M
t2 =t1+1

print(min(N-M*t1,M*t2-N))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from sys import stdin
import math
from decimal import Decimal

n, k = [Decimal(x) for x in stdin.readline().rstrip().split()]
print(int(min(abs(n-math.floor(n/k)*k), abs(n-math.ceil(n/k)*k))))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#入力をN Kで受ける
N,K = map(int,input().split())
#N,Kの場合分けで最小値算出
if N <= K / 2:
    print(N)
elif N < K:
    print(K-N)
else:
    if N - N // K * K <=K /2:
        print(N - N // K * K)
    else:
        print((1 + N // K) * K - N)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N ,K = map(int,input().split())
ans = N
while (1):
    # x > Kの時
    if ans > K:
        if abs(ans - K) > ans:
            break
        else:
            ans = ans % K
    # x < Kの時
    elif ans <= K:
        if abs(ans - K) > ans:
            break
        else:
            ans = abs(ans - K)
print(ans)

Submission status: Accepted

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

if k == 1:
    n = 0
else:  
    while abs(n - k) < np:
        i = 1
        if k ** i < n:
            while k ** i < n :
                i += 1
            n = n - k ** (i - 1)
        np = n
        n = abs(n - k)
print(n)

Submission status: Accepted

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

if k == 1:
    n = 0
else:
    if abs(n - k) > np:
        print(n)
        exit()
    
    while abs(n - k) < np:
        i = 1
        if k ** i < n:
            while k ** i < n :
                i += 1
            n = n - k ** (i - 1)
        np = n
        n = abs(n - k)
print(n)

Submission status: Accepted

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

def f(a,b):
    s = a//b 
    a = a-s*b
    ans = min(a,abs(a-b))
    return ans


if(n%k==0):
    print(0)
else:
    print(f(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, K = map(int, input().split())
num = N//K
delta = abs(N-num*K)
if delta > abs(delta-K):
    print(abs(delta-K))
else:
    print(delta)

Submission status: Accepted

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

else:
    num = N//K
    delta = abs(N-num*K)
    if delta > abs(delta-K):
        print(abs(delta-K))
    else:
        print(delta)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#  =========     /\       /|    |====/|
#      |        /  \       |    |   / |
#      |       /____\      |    |  /  |
#      |      /      \     |    | /   |
#  ========= /        \  =====  |/====|  
#  code

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,k = map(int,input().split())
    print(min(n%k,-n%k))
    return

#Solve
if __name__ == "__main__":
    solve()

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

n=n%k

print(min(n,k-n))

Submission status: Accepted

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

s = N % K
t = K - s

number = [s,t]
print(min(number))

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
A=[N]
if N%K==0:
    print(0)
elif N<K:
    print(min(abs(N-K),N))
else:
    a=N//K
    x=N-K*a
    print(min(x,abs(K-x)))

Submission status: Accepted

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

Submission status: Accepted

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

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

ans = N

tmp = N%K

if tmp > abs(tmp-K):
    print(abs(tmp-K))
else:
    print(tmp)

Submission status: Accepted

Solution code:
def abs(a):
    if a < 0:
        return -a
    return a

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

    if n < k:
        if n > abs(n-k): 
            print(abs(n-k))
        else:
            print(n)
    else:
        print(n)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = lambda: sys.stdin.readline().strip()

def main():
    N,K = map(int,input().split())
    ans = N%K
    print(ans if ans<abs(ans-K) else abs(ans-K))

main()

Submission status: Accepted

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

Submission status: Accepted

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

a = N % K

if a <= abs(a-K):
    print(a)
else:
    print(abs(a-K))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

n = n % k

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