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

Submission status: Accepted

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

from sys import stdin, stderr

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

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

   return 0

if __name__ == '__main__': main()

Submission status: Accepted

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

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

if n % k == 0:
    print(0)
    exit()
if n * 2 < k:
    print(n)
    exit()

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

Submission status: Accepted

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

if N >= K:
    q, r = divmod(N, K)
    v = min(r, abs(K - r))
    print(v)
else:
    print(min(N, abs(N - K)))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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())
q, mod = divmod(N, K)
if mod*2 > K:
    print(abs(mod-K))
else:
    print(mod)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

temp = n//k
print(min(abs(n-temp*k),abs(n-temp*k-k)))

Submission status: Accepted

Solution code:
V=0;
o=input().rstrip().split(' ')
n=int(o[0])
k=int(o[1])
if(n%k==0):
    print(0);
elif(k%n==0):
    print(n)
elif(n>k):
    a=(n//k)*k;
    b=n-a;
    print(k-b);
else:
    print(min(n,k-n))

Submission status: Accepted

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

Submission status: Accepted

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

N = a[0]
K = a[1]

p = N % K

if p <= K-p:
  print(p)
else:
  print(K-p)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
q, mod = divmod(N, K)
if mod*2 > K:
    print(abs(mod-K))
else:
    print(mod)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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


def main():
    import sys
    input = sys.stdin.readline

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
if N>K:
    N=N-(N//K)*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, abs(n%k-k)))

Submission status: Accepted

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

cand1 = abs(int(n-k*(n//k)))
cand2 = abs(int(n-k*(1+n//k)))
cand3 = n

ans = min(cand1, cand2, cand3)
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
print(min(N,K-N))

Submission status: Accepted

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

q = n // k
mod = n % k
ans_mod = k-mod

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

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

if K <= 2*N:
    N = K - N

print(N)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

n,m=input().split()
n,m=[int(n),int(m)]
k=n%m


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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

x = N % K

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

Submission status: Accepted

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

res = min(reminder, s)
print(res)

Submission status: Accepted

Solution code:
from sys import stdin, stdout
rl = lambda: stdin.readline()
rll = lambda: stdin.readline().split()

def main():
	n, k = (int(x) for x in rll())
	ans = float('inf')
	c1 = n % k
	c2 = abs(c1 - k)
	stdout.write(str(min(c1, c2)));stdout.write("\n")
	stdout.close()

if __name__ == "__main__":
	main()

Submission status: Accepted

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

a = n%k
while True:
    tmp = abs(a-k)

    if a > tmp:
        a = tmp
    else:
        break
print(a)

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

Submission status: Accepted

Solution code:

def submit():
    n, k = map(int, input().split())
    
    coef = n // k
    cand1 = abs(coef * k - n)
    cand2 = abs((coef + 1) * k - n)

    print(min(cand1, cand2))


submit()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

t = N % K
t_dush = abs(t-K)

print(min(t, t_dush))

Submission status: Accepted

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

def get_min(n, k):
    s = set()
    min_num = n
    while n not in s:
        s.add(n)
        n = k - n
        min_num = min(min_num,n)
    return min_num

print(get_min(n%k,k))

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
a=N//K
b=N%K
c=K*(a+1)-N
print(min(b,c))

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import unittest


class TestC(unittest.TestCase):
    def test_1(self):
        self.assertEqual(think(7, 4), 1)

    def test_2(self):
        self.assertEqual(think(2, 6), 2)

    def test_3(self):
        self.assertEqual(think(1000000000000000000, 1), 0)


def solve():
    n, k = read()
    result = think(n, k)
    write(result)


def read():
    return read_int(2)


def read_int(n):
    return read_type(int, n, sep=' ')


def read_float(n):
    return read_type(float, n, sep=' ')


def read_type(t, n, sep):
    return list(map(lambda x: t(x), read_line().split(sep)))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(n, k):
    if n % k == 0:
        return 0
    m = n % k
    return min(m, abs(k - m))


def write(result):
    print(result)


if __name__ == '__main__':
    # unittest.main()
    solve()

Submission status: Accepted

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

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 = n % k
a = n
while True:
    b = abs(a - k)
    if b <= k / 2:
        ret = b
        break
    else:
        a = b

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

Submission status: Accepted

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

Submission status: Accepted

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

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


minValue()

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

print(min(a,K-a))

Submission status: Accepted

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

Submission status: Accepted

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

amari = n % k
if amari == 0 :
  print("0")
  sys.exit()

base = amari
while base > abs(base - k):
  base = abs(base - k)

print(base)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
n%=k
if(k==1):
  print(0)
  exit()
s={n}
while True:
  n=abs(n-k)
  if(n in s):
    break
  else:
    s.add(n)
print(min(s))

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

Submission status: Accepted

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

shou = N // K

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

list = np.array([], dtype=int)
N = N % K

while(True):
    new_N =np.abs(N-K)
    list = np.append(list, new_N)
    N = new_N
    # listが現れた場合
    if(len(list) != len(set(list))):
        break;

N_min = np.min(list)
print(N_min)

Submission status: Accepted

Solution code:
list = input().split(" ")

startNum, standardNum = int(list[0]), int(list[1])

mod = startNum % standardNum
minusMod = abs(mod - standardNum)

result = mod if mod <= minusMod else minusMod

print(result)

Submission status: Accepted

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

first = int(N % K)
second = int(K - first)

if second < first:
  print(second)
else:
  print(first)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

t=N//K


ans = min(N - K * t, -(N - K * (t + 1)))
print(ans)

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

Submission status: Accepted

Solution code:
a, b = map(int, input().split(" "))

quotinent = a // b

res = min(a - b * quotinent,  abs(a- b * (quotinent + 1)))
print(res)

Submission status: Accepted

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

mini = N % K

if mini < abs(mini - K):
    mini = mini
else:
    mini = abs(mini - K)

print(mini)

Submission status: Accepted

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

A = N % K

if (A <= K):
    if A <= abs(A-K):#Aが小さい
        print(A)
    else:# A-Kが小さい
        A = A - K
        print(abs(A))
else:
    print(A)

Submission status: Accepted

Solution code:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n,k = nm()

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

a = N%K
ans = min(a, K-a)

print(ans)

Submission status: Accepted

Solution code:
import sys
def fastio():
    from io import StringIO
    from atexit import register
    global input
    sys.stdin = StringIO(sys.stdin.read())
    input = lambda : sys.stdin.readline().rstrip('\r\n')
    sys.stdout = StringIO()
    register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

INF = 10**20
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))
# from math import gcd
from math import ceil
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br

n, k = I()
print(min(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:
def main():
    N, K = map(int, input().split())
    ans = N % K
    print(min(ans, abs(ans-K)))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

x = nk[0]%nk[1]
p = abs(x-nk[1])
print(x if x < p else p)

Submission status: Accepted

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

resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
str = input().split()
n = int(str[0])
k = int(str[1])
t = n%k
other = abs(k-t)
print(min(t,other))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

rot = n//k
x = n

if k == 1 or n == k or n%k == 0:
  x = 0
elif rot >= 1:
  x = n - k*rot
  
t = x%k

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

Submission status: Accepted

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

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 = [int(i) for i in input().split()]
t = n % k
print(min(t, k - t))

Submission status: Accepted

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

x = N
d = N-K
if d > 0:
    x = K + d%K
ans = min(x, abs(x-K), abs(x-K-K))
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())
m=N%K
ans=min(m,K-m)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

import sys
sys.setrecursionlimit(300000)


def solve(N: int, K: int):
    def gcd(a, b):
        return a if b == 0 else gcd(b, a % b)
    ret = 0
    if N <= K:
        ret = min(K - N, N)
    else:
        tmp = N % K
        ret = min(K - tmp, tmp)
    print(ret)
    return

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:
N,K=map(int,input().split())
a=-(-N//K)
print(min(N,abs(N-K*a)))

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a = input()
a, b = a.split(' ')
a = int(a)
b = int(b)

a = a % b

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

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

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
ans=0
lis = 10**18
ans=min(n%k, (n//k+1)*k-n)
print(ans)

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

Submission status: Accepted

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


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


n = abs(n - k)
ans2 = n


print(min(ans1, ans2))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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())
ans=min(N%K,K-(N%K))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

def answer(A,B):
    return min(A%B, B-A%B)

print(answer(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())
a = n%k
ans = min(a, k-a)
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:
import sys


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


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

Submission status: Accepted

Solution code:
#AtCoder
N, K = map(int, input().split())
q, mod = divmod(N, K)
print(min(mod, K-mod))

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

t = N % K
k = K - t

print(min(t, k))

Submission status: Accepted

Solution code:
def main():
    N, K = map(int, input().split())
    N = N - (N // K) * K
    while N > abs(K - N):
        N = abs(K - N)
    print(N)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

# find smallest difference with N > K (the value N % K is equivalent to applying N := N-K repeatedly)
N = N % K 

# minimum is either N or N - K (for the case where K > N)
minimum = min(N, abs(N-K))

print(minimum)

Submission status: Accepted

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


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

Submission status: Accepted

Solution code:
base = input().split()
n = int(base[0])
k = int(base[1])
s = n%k
t = k-s
if s>t:
  print(t)
else:
  print(s)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, k = map(int, input().split())
a = n % k
b = abs(k - a)
print(a) if a <= b else print(b)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, k = map(int, input().split())
a = n % k
b = abs(k - a)
if a <= b:
    print(a)
else:
    print(b)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict, deque
import sys
import heapq
import bisect
import itertools
import queue
import copy
import time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7

def inp(): return int(sys.stdin.readline())

def inpl(): return list(map(int, sys.stdin.readline().split()))

def inpl_str(): return list(sys.stdin.readline().split())

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

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 = [int(x) for x in input().split()]
a = n % k
b = k - a
print(min(a, b))

Submission status: Accepted

Solution code:
N,K = map(int,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())

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

a = N - (N//K)*K
b = (N//K+1)*K - N

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

Submission status: Accepted

Solution code:
N,K=map(int, input().split()) 
y = N//K
x = N - y*K
#x = N%K
ans = min(x, abs(x-K))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

if N<=K:
  print(min(N,K-N))
else:
  n=N//K
  print(min(abs(N-K*n),abs(N-K*(n+1))))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

# 改行またはスペース区切りの入力をすべて読み込んでイテレータを返します。
def get_all_int():
    return map(int, open(0).read().split())

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

n,k = get_nums_l()

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

Submission status: Accepted

Solution code:
import sys
import os

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:
# coding: utf-8
from math import ceil
N, K = map(int, input().split())
v = ceil(N / K)
ans = abs(N - v*K)
print(min(N, abs(N%K - K), N%K))

Submission status: Accepted

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

Submission status: Accepted

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

print(t if t <= k-t else k-t)

Submission status: Accepted

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

Submission status: Accepted

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

x = abs(N - K*(int(N/K)-1))
ans = N

for i in range(0,100000):
    x = abs(x-K)
    ans = min(ans,x)

print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

N = N % K

if (N > abs(N - K)):
    N = abs(N - K)

print(N)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K = map(int, input().split())
syou = N//K
min_1 = N - syou*K
min_2 = -(N - (syou+1)*K)
if min_1 < min_2:
    print(min_1)
else :
    print(min_2)