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:
import math

def main():
    s = input().split()
    n = int(s[0])
    k = int(s[1])
    
    if n<=(k/2):
        print(n)
        return
    
    n = n - (n//k)*k
    if n<= k/2:
        print(n)
    else:
        print(abs(n-k))

main()

Submission status: Accepted

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

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

elif N == K:
    print(0)

else:
    N = K + (N - K) % K
    N = abs(N - K)

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

    elif N == K:
        print(0)

Submission status: Accepted

Solution code:
import os

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


if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
##161 C
N,M= map(int, input().split())
amari= N % M
if(amari<=(M/2)):
    print(amari)
else:
   amari = abs(amari-M)
   print(amari)

Submission status: Accepted

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

one = n
while n > k:
    n = n % k
    one = n
two = abs(n-k)

print(min(one,two))

Submission status: Accepted

Solution code:
import math

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


def check(K,N ):
    min = 0
    abs_num = 0

    N = N%K

    OLD_ABS =N
    NEW_ABS = abs(K-N)

    while NEW_ABS < OLD_ABS:
        OLD_ABS = N #OK
        N = NEW_ABS ##OK
        NEW_ABS =abs(K-N)
    #    print("N_NEW",NEW_ABS,"OLS_ABS",OLD_ABS,"N",N)
    print(N)     
            
K,N = nyu()
check(K,N)
#print(S)

Submission status: Accepted

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

def fun(n, k):
    a = n%k
    return min(a, k-a)

print(fun(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:
import sys

sys.setrecursionlimit(10 ** 6)


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

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

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

Submission status: Accepted

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

def replace(a,b):
    if a < abs(a - b):
        return a
    else:
        return replace(abs(a - b), b)

if K == 0:
    print(N)
elif N % K == 0:
    print(0)
else:
    print(replace(N%K, K))

Submission status: Accepted

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

Submission status: Accepted

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

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

N %= K

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

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

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

Submission status: Accepted

Solution code:
import numpy as np
N, K  = map(int, input().split())

m = np.mod(N,K)

while m > abs (m-K):
  m = abs (m-K)

print(m)

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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
x=N
if N>K:
    x=N%K
inLoop=True
while inLoop:
    x=abs(K-x)
    if x<=K/2:
        inLoop=False
print(x)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

while True:
  if abs(x-k) < x:
    x = abs(x-k)
  else:
    break
print(x)

Submission status: Accepted

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

pre_value = n+1
tmp = n
a = n // k 
tmp = n - a*k 
while tmp < pre_value:
    pre_value = tmp
    tmp = abs( tmp - k)
    
print(pre_value)

Submission status: Accepted

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


def solve(N: int, K: int):
    n = N
    k = K
    mi = n//k
    ma = mi+1
    res1 = abs(n - k*mi)
    res2 = abs(n-k*ma)
    print(res1 if res1 < res2 else res2)


# 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:
import math
l=list(map(int,input().split()))
n,k=l[0],l[1]
while n>k:
  s=n//k
  n=n-(k*s)
if n==0:
  print('0')
else:
  if n<math.fabs(n-k):
    print(int(n))
  else:
    print(int(math.fabs(n-k)))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math

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

while True:
  if abs(tmp - K) < tmp:
    tmp = abs(tmp - K)
  else:
    break
 	
print(tmp)

Submission status: Accepted

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

buf = i[0]%i[1]
if buf > i[1] - buf:
  buf = i[1] - buf
print(buf)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import numpy as np
N, K = list(map(int, input().split(' ')))
x = N
min_x = x
while True:
    x1 = x % K if x >= K else np.abs(x - K)
    if x1 == min_x:
        break

    if x1 < min_x:
        min_x = x1
    
    x = x1
print(min_x)

Submission status: Accepted

Solution code:
#coding:utf-8

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

r1 = n % k
r2 = abs(k - r1)

ans = min(r1,r2)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#coding:utf-8

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

r1 = n % k
r2 = abs(k - r1)

ans = min(r1,r2)

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

Submission status: Accepted

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

Submission status: Accepted

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

def debug(arg):
    if __debug__:
        pass
    else:
        import sys
        print(arg, file=sys.stderr)

def main():
    N, K = map(int, open(0).read().split())
    debug([N, K])
    r = N % K
    a = min(r, K - r)
    print(a)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
if n>=k:
    n%=k
res=10000000000000000000000
for i in range(3):
    res=min(n,res)
    n=abs(n-k)
print(res)

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

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

Submission status: Accepted

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

Submission status: Accepted

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

x = N % K
y = abs(x - K)

print(x if x < y else y)

Submission status: Accepted

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

mo = n%k

mok = abs(mo - k)

if mo <= mok:
    print(mo)
else:
    print(mok)

Submission status: Accepted

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

Submission status: Accepted

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

NmodK = N % K

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

Submission status: Accepted

Solution code:
n, k = list(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())
ans = min(k-(n % k), (n % k))
print(ans)

Submission status: Accepted

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

import sys
# import math
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product                # product(iter, repeat=n)
# from itertools import accumulate             # accumulate(iter[, f])
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from copy import deepcopy                    # to copy multi-dimentional matrix without reference


def main():
    mod = 1000000007                  # 10^9+7
    inf = float('inf')
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():  return int(input())
    def mi():  return map(int, input().split())
    def mi_0(): return map(lambda x: int(x)-1, input().split())
    def lmi(): return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():  return list(input())
    
    n, k = mi()
    if n <= k:
        print(min(n, k-n))
    else:
        n -= (n // k) * k
        print(min(n, k-n))
    

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split()) 
if N < K:
    print(min(N, abs(N-K)))
elif (N >= K) & (N % K == 0):
    print(0)
elif (N >= K) & (N % K != 0):
    r = N % K
    print(min(r, abs(r - K)))
else:
    pass

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
a = N // K
value = abs(N - a * K)
if K == 1:
    print(0)
    exit()
while True:
    if abs(value - K) > abs(value):
        break
    value = abs(value - K)
print(value)

Submission status: Accepted

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

a = n % k
b = k - a

print(min(a, b))

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

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

mod = N % K


a = abs(mod - K)

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

Submission status: Accepted

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

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

Submission status: Accepted

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

amari = n % k

if amari == 0:
    ans = 0

else:
    if amari <= k/2:
        ans = amari
    else:
        ans = -(amari - k)

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
a = N % K
b = abs(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,K - (N % K)))

Submission status: Accepted

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

Submission status: Accepted

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

x = n%k
y = k-x

print(min([x,y]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# C - Replacing Integer
# https://atcoder.jp/contests/abc161/tasks/abc161_c

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

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

# Nの数が大きすぎるとタイムアウトする
# 求める最小値がN/Kの余りかK-(N/K)の余りであることを利用して解く。

# 7%3 -> 1
# 3-(7%3) -> 2

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:
import sys

n, k = map(int, sys.stdin.readline().split())

mod = n % k

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
n,k = map(int,input().split())
n %= k
print(min(n,np.abs(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 = (int(x) for x in input().split())
a = N%K
if a >= K-a:
  print(K-a)
else:
  print(a)

Submission status: Accepted

Solution code:
import numpy as np
import math as mt
N,K=map(int, input().split())
while(1):
    if abs(N-K)<N:
        a,b=max(N,K),min(N,K)
        N=a-b*(a//b)
    else:
        print(N)
        break

Submission status: Accepted

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

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

Submission status: Accepted

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

if N > K:
  N = N % K

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

Submission status: Accepted

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

if n>k:
  n = n%k

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

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    N, K = list(map(int,_in[0].split(' ')))  # type:list(int)
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    x = N % K
    if abs(x-K) < x:
        cnt = abs(x-K)
    else:
        cnt = x
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(cnt)

Submission status: Accepted

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

if N % K  == 0:
    print("0")
else:
    val = N % K
    if val < abs(val-K):
        print(val)
    else:
        print(abs(val-K))

Submission status: Accepted

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

kpn=n//k

print(min(n-k*kpn,abs(n-k*(kpn+1))))

Submission status: Accepted

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

Submission status: Accepted

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

amari = N % K;

hu = -1* (amari - K);

max_N = min(amari,hu);

print(max_N);

Submission status: Accepted

Solution code:
from collections import deque, Counter as cnt
from collections import defaultdict as dd
from operator import itemgetter as ig
from bisect import bisect_right as bsr
from math import factorial, ceil, floor
import sys
sys.setrecursionlimit(1000000)

# お約束
args = None
INF = float("inf")
MOD = int(1e9 + 7)


def input(*ps):
    if type(ps[0]) is list:
        return [input(*ps[0][:-1]) for _ in range(ps[0][-1])]
    elif len(ps) == 1:
        return ps[0](next(args))
    else:
        return [p(next(args)) for p in ps]


def nlist(n, v):
    if not n:
        return [] if type(v) is list else v
    return [nlist(n[1:], v) for _ in range(n[0])]

# エントリーポイント


def main():
    N, K = input(int, int)
    ans = [N % K]
    for _ in range(100):
        ans.append(abs(ans[-1] - K))
    print(min(ans))
    return


if __name__ == '__main__':
    args = iter(sys.stdin.read().split())
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
ans = n%k
if (abs(ans-k) < ans):
	ans = abs(ans-k)
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:
InputIntList = lambda : list(map(int, input().split()))
InputStrList = lambda : list(input().split())

N, K = InputIntList()

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

Submission status: Accepted

Solution code:
from sys import stdin
import numpy as np


############################################

# read data for n sequences.
temp = stdin.readline().split()
N, K = np.asarray(temp).astype(int)

# K==1の場合
if K == 1:
    print(0)
    
elif N <= K:
    a = np.abs(N-K)
    if a < N:
        print(a)
    else:
        print(N)
else:
    rem = N%K
    print(np.abs(rem - K))

Submission status: Accepted

Solution code:
from collections import *
from heapq import *
from itertools import *
#from fractions import gcd

from decimal import *
import copy
from bisect import *
import time
import sys
import math

input=lambda :sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**7)
def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))

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

Submission status: Accepted

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

Submission status: Accepted

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

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

num1=N//K
num2=K*(num1+1)-N
num3=N-K*(num1)

print(min(num2,num3))

Submission status: Accepted

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

a = []
a.append(N)

if N % K == 0:
    print(0)
else:
    N = N % K
    while True:
        if abs(N - K) not in a:
            a.append(abs(N - K))
            N = abs(N - K)
        else:
            print(min(a))
            break

Submission status: Accepted

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

def dif(n, k):
    if abs(n - k) <= n:
        return dif(abs(n-k), k)
    else:
        return n

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

Submission status: Accepted

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



if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

import sys
input = sys.stdin.readline

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

a = N // K

print(min([N, K, abs(N-K*a), abs(N-K*(a+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()))

n -= k * (n//k)

print(min(n, abs(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())
Answer = min(abs(N-((N//K)*K)),abs(N-((N//K+1)*K)))
print(Answer)

Submission status: Accepted

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

if n >= k:
    n -= k * (n // k)      

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

Submission status: Accepted

Solution code:
from sys import stdin

n,m=map(int,input().split())
n%=m
while abs(n)>abs(n-m):
    n=abs(n-m)
print(n)

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

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

Submission status: Accepted

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

Submission status: Accepted

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

lst = [N]
while 1:
  n = min(abs(lst[-1]%K),abs(lst[-1]-K))
  if lst.count(n):
    break
  else:
    lst.append(n)
    
print(min(lst))

Submission status: Accepted

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



points = NK[0]//NK[1]
value = NK[1]*points
v1 = NK[0] - value
v2 = abs(v1-NK[1])

# print(value)
# print(v1)

if v1 > v2:
    print(v2)
else:
    print(v1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
  n,k = (int(x) for x in input().split())
  n = n % k
  min = n
  while True:
    n = abs(n - k)
    if(n < min):
      min = n
    else:
      break

  print(min)

if __name__ == '__main__':
  main()

Submission status: Accepted

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

while True:
    m = abs(n - k)
    if k == 1:
        break
    elif n == 0:
        break
    elif n <= k:
        result = min(n, m)
        break
    else:
        n = n % k

print(result)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
l = input()
m = l.split(" ")
n = int(m[0])
k = int(m[1])
if n > 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())
C = N // K

N -= K * (C)

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n,k=map(int,input().split())
    print(min(n%k,k-n%k))

if __name__=='__main__':
    main()

Submission status: Accepted

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

while N>K:
  N = N%K
  
print(min([N,K-N]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
import math
# 1行だけ読むやつ

data = stdin.readline().rstrip().split()
N, K = int(data[0]), int(data[1])

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
from collections import defaultdict
from heapq import heappop, heappush

from decimal import *

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

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

	N = N%K

	ans = N
	while True:
		tmp = abs(ans-K)

		if tmp < ans:
			ans = tmp
		else:
			break

	print(ans)

def gcd(a, b):
	if a > b:
		a, b = b, a

	while a > 0:
		a, b = b % a, a
	return b

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

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

n = N % K
print(min([n, K-n]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
B=int(N/K)
A=abs(N-(B-1)*K)
while A>=K/2:
    A=min(abs(A-K),abs(A+K),A)
print(A)

Submission status: Accepted

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