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 numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


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

    res = N % K

    while True:
        temp = abs(res - K)
        if res > temp:
            res = temp
        else:
            break

    print(res)



main()

Submission status: Accepted

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

min_val = result
while True:
    result = abs(result - k)
    if (min_val <= result):
        break
    min_val = result
print(min_val)

Submission status: Accepted

Solution code:
import math

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

now = N
waru = N // K

A = abs(N-(K*(waru+1)))
B = abs(N-(K*(waru)))

if A >= B:
    print(B)
else:
    print(A)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

i_i = lambda: int(i_s())
i_l = lambda: list(map(int, stdin.readline().split()))
i_s = lambda: stdin.readline().rstrip()

N, K = i_l()

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

N = N % K
arr = []
while not N in arr[:-1]:
    N = abs(N-K)
    arr.append(N)

print(min(arr))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def resolve():
    N, K = map(int, input().split())
    div, mod = divmod(N, K)
    N = N - K * div
    print(min(abs(N-K), N))

resolve()

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import numpy as np
import itertools
import collections
from collections import deque

sys.setrecursionlimit(10**6)
readline = sys.stdin.readline

h = itertools.combinations_with_replacement
p = itertools.permutations

def main():
    # input
    N, K = map(int, input().split())
    rest = N%K
    reverse = abs(rest - K)
    ans = min(N, rest, reverse)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

input_line = input()
Line = input_line.split()
Line = [int(s) for s in Line]

a = Line[0] % Line[1]

b = y = abs(a- Line[1])

# print(a,b)

print(min(a,b))

Submission status: Accepted

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


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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
x = N % K
ans = 0
if N >= K:
    if K/2 >= x:
        ans = x
    else:
        ans = K - x
else:
    if N >= K/2:
        ans = K - N
    else:
        ans = N
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

while True:
    n = abs(N - K)
    if n >= N:
        ans = N
        break
    else:
        N = n

print(ans)

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 = N % K
while True:
    new = abs(answer - K)
    if new > answer:
        break
    answer = new
print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
# coding: utf-8
import sys
sys.setrecursionlimit(10000000)

#const
dxdy=((1,0),(0,1))
# my functions here!
def pin(type=int):
    return map(type,input().rstrip().split())

def resolve():
    N,K=pin()
    x=N%K
    print(min(K-x,x))
"""         
#printデバッグ消した？
#前の問題の結果見てないのに次の問題に行くの？
"""
"""
お前カッコ閉じ,るの忘れてるだろ
"""
import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """7 4"""
        output = """1"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """2 6"""
        output = """2"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """1000000000000000000 1"""
        output = """0"""
        self.assertIO(input, output)


if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
N, K = map(int, sys.stdin.readline().split())
a = N % K
if a < K/2:
  print(a)
else:
  print(K-a)

Submission status: Accepted

Solution code:
def resolve():
    n, k = map(int, input().split())
    m= n-(n//k)*k
    tmp=m
    min=m

    while True:
        tmp = abs(tmp-k)
        if tmp < min:
            min = tmp
        else:
            break
    print(min)
    
    
if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
def gcd(x, y): 
  
   while(y): 
       x, y = y, x % y 
  
   return x 
a,b=map(int,input().split())
if a==0:
    print(0)
else:
    if a%b==0:
        print(0)
    else:
        x=a%b 
        while True:
            if x>abs(x-b):
                x=abs(x-b)
            else:
                break 
        print(x)

Submission status: Accepted

Solution code:
n, k = map(int,input().split())
q, mod = divmod(n, k)
if mod == 0:
    print(0)
else:
    print(min(mod, abs(k - mod)))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import numpy as np

NK=np.array(input().split(" "),dtype=np.int64)

N=NK[0]
K=NK[1]

x=N%K

abs_x=abs(x-K)

if(x<abs_x):
    print(x)
else:
    print(abs_x)

Submission status: Accepted

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

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import copy
import math
import bisect
import pprint
import bisect
from functools import reduce
from copy import deepcopy
from collections import deque
from decimal import *


def lcm(x, y):
    return (x * y) // math.gcd(x, y)


if __name__ == '__main__':
    b, c = map(int, input().split())
    a =abs(b% c)
    
    if abs(a-c) < a:
        print(abs(a-c))
    else:
        print(a)

Submission status: Accepted

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

n_1 = N - (K*T)
n_2 = N - (K*(T+1))

print(min(abs(n_1),abs(n_2)))

Submission status: Accepted

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

a = n % k
while True:
    t = abs(a - k)
    if t < a:
        a = t
    else:
        break
print(a)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

if N > K:
    v = N // K
    N -= K * v

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(min(x, amari))

Submission status: Accepted

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

items = set([N])

def check(x, K):
	if x == K:
		x = 0
	elif x < K:
		x = K - x
	else:
		x = x % K
	return x

x = N
while True:
	new_x = check(x, K)
	if new_x in items:
		break
	items.add(new_x)
	x = new_x


print(min(items))

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
p=N//K
N=N-K*p
if N<(K-N):
  print(N)
else:
  print(K-N)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
if K >= N:
    print(min(N, K - N))
else:
    q = N // K
    print(min(N % K, (q + 1) * K - N))

Submission status: Accepted

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

lis=[]

cur=N%K
mini=N%K
while True:
    cur=abs(cur-K)

    if cur in lis:
        break
    else:
        lis.append(cur)
        mini=min((mini,cur))


print(mini)

Submission status: Accepted

Solution code:
# coding: utf-8
n, k = map(int, input().split())
if n > k:
    n = int(n%k)

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

Submission status: Accepted

Solution code:
def rep(n, k):
    if n < k:
        if n <= k-n:
            print(n)
        else:
            print(k-n)
    elif n == k:
        print(0)
    else:
        r = n % k
        if r <= k-r:
            print(r)
        else:
            print(k-r)

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

rep(n, k)

Submission status: Accepted

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

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

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:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
n, k = map(int, input().split())
while(True):
    if n - k > 0:
        if n // k > 0:
            n = n - k * (n // k)
        else:
            if n - k < n:
                n = n - k
            else:
                break
    else:
        if abs(n - k) < n:
            n = abs(n - k)
        else:
            break

print(n)

Submission status: Accepted

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


def solve(N: int, K: int):
    if abs(N-K) > N:
        print(N)
    else:
        diff = max(N, K) - min(N, K)
        k = diff//min(N, K)
        MAX = max(N, K)-k*min(N, K)
        MIN = min(N, K)
        a1 = MAX
        a2 = abs(MAX-MIN)
        a3 = abs(a2 - MIN)
        print(min(a1, a2, a3))


# 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 sys
# input = sys.stdin.buffer.readline
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getlist():
    return list(map(int, input().split()))
def getlist2():
    return [int(x) * 100000 for x in input().split()]
import math
import bisect
import heapq
from decimal import Decimal
# from collections import defaultdict, Counter, deque
MOD = 10**9 + 7
INF = 10**15
def main():
    n,k = getlist()
    mo = n % k
    print(min(mo, abs(mo - k)))

if __name__ == '__main__':
    main()

"""
9999
3

2916
"""

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
def main():
    N,K = map(int, input().split())
    ans = min(N%K,abs(N%K-K))
    print(ans)
if __name__ == "__main__":
    main()

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:
#!/usr/bin/env python3
import sys


def solve(N: int, K: int):
    x = N%K
    while True:
        tmp = x
        x = min(abs(x - K), x)
        if tmp == x:
            break
    print(x)


# 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:
x = input().split()
N = int(x[0])
K = int(x[1])

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

rem = n % k
rem_minus = (rem - k) * - 1

if rem >= rem_minus:
  print(rem_minus)
else:
  print(rem)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from fractions import gcd
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 == 0:
    n=0
else:
    tmp = n%k
    n = tmp
    while True:
        n = abs(n-k)
        if tmp <= n:
            n = tmp
            break
        tmp = n
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=(int(i) for i in input().split())
while n>abs(n-k):
	if n>k:
		n%=k
	else:
		n=abs(n-k)
print(n)

Submission status: Accepted

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

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

Submission status: Accepted

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


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


N, K = inpl()

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

Submission status: Accepted

Solution code:
import sys
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value

N, K = mi()

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

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())
amari = n % k
if amari == 0:
         print(0)
         exit()
else:
         print(min(amari,abs(amari - k)))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

    tmp = n % k
    return min([tmp, n, k, k - tmp])


if __name__ == "__main__":
    result = solve()
    print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

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

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:
import heapq
from collections import defaultdict, deque
from math import ceil, factorial
from fractions import gcd
import sys
sys.setrecursionlimit(10**7)
INF = float('inf')
MOD = 10**9+7

si = lambda: input().strip()
ii = lambda: int(input())
mii = lambda: map(int, input().split())
lmii = lambda: list(map(int, input().split()))
smii = lambda: sorted(map(int, input().split()))


N, K = mii()

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

d, m = divmod(N, K)

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

y = n // x

print(min(n - y * x, abs(n - x * (y+1))))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
nk=input().split()
N=int(nk[0])
K=int(nk[1])
a=N%K
b=K-a
if a>b :
  print(b)
else :
  print(a)

Submission status: Accepted

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

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

if not N % K:
    print(0)
else:
    a = N % K
    b = K - a
    if a > b:
        print(b)
    else:
        print(a)

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

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

Submission status: Accepted

Solution code:
import sys
N, K = map(int, input().split())
if N % K == 0:
    print(0)
    sys.exit()
else:
    N = N % K
    while abs(N - K) < N:
        N = abs(N - K)
    else:
        print(N)

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

ans = N % K

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

import sys

sys.setrecursionlimit(10**9)

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

mod = N % K

if mod==0:
    print(0)
else:
    print(min(abs(mod-K), mod))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
a = n - (n // k) * k
b = k - a
# print(a, b)
if a < b:
    c = a
else:
    c = b
print(c)

Submission status: Accepted

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

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

print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

x = N % K
num = [x, K - x]

print(min(num))

Submission status: Accepted

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

Submission status: Accepted

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

amari = N % K

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

Submission status: Accepted

Solution code:
from sys import stdin


def get_result(data):
    N, K = data
    q, mod = divmod(N, K)
    ans = min([mod, abs(mod-K)])
    return ans


if __name__ == '__main__':
    data = list(map(int, stdin.readline().rstrip().split(' ')))
    result = get_result(data)
    print(result)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n, k = tuple([int(i) for i in input().split()])

    while abs(n - k) < n:
        if n > k:
            n -= k * (n // k)
        else:
            n = abs(n - k)

    print(n)

Submission status: Accepted

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

Submission status: Accepted

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


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

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

Submission status: Accepted

Solution code:
import math
from decimal import Decimal

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

Submission status: Accepted

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

check_set = set()
number = N
if number > K:
    number = number % K

while number not in check_set:
    check_set.add(number)
    number = abs(K - number)

print(min(check_set))

Submission status: Accepted

Solution code:
def listint():
    a = input().split()
    for i in range(len(a)):
        a[i] = int(a[i])
    return a
def main(n,k):
    z = n % k
    if z > k-z:
        return k-z
    else:
        return z
n,k = listint()
print(main(n,k))

Submission status: Accepted

Solution code:
input = raw_input

n, k = map(int, input().split(" "))
n1 = n % k
n2 = k - n1
out = min(n1, n2)
print(out)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

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

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

Submission status: Accepted

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

if k == 1:
    print('0')
else:
    n = n % k
    print(min(n, abs(n - k), k))

Submission status: Accepted

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

    ans = n % k

    if abs(ans - k) % k < ans:
        ans = abs(ans - k) % k

    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8

import sys
import math
import collections
import itertools
INF = 10 ** 10
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def gcd(x, y) : return y if x % y == 0 else gcd(y, x % y)
def lcm(x, y) : return (x * y) // gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def PL(L) : print(*L, sep="\n")

N, K = LI()

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

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

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

n_k = n % k

ans = min(n_k, k - n_k)

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N, K = list(map(int, input().split()))
mod = N%K
min_ = mod
while True:
    tmp = abs(min_ - K)
    if (tmp < min_):
        min_ = tmp
    else:
        break
print(min_)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(min(b, c))

Submission status: Accepted

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

n = n % k

while flag:
  if min(abs(n-k),abs(n)) != n:
    n = min(abs(n-k),abs(n))
  else:
    print(n)
    flag = 0

Submission status: Accepted

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

if N > K:
    N = N % K

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

Submission status: Accepted

Solution code:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n,k = mi()

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

while (N >= 0):
  N -= K
  if (abs(N) < ans):
    ans = abs(N)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans_ = n % k

ans__ = abs(ans_ - k)
ans = min(ans_, ans__)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K = [int(i) for i in input().split()]
a = N//K
amari = N - K*a
abs_K = abs(amari-K)
if abs_K < amari:
    print(abs_K)
else:
    print(amari)

Submission status: Accepted

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

A = N//K

B = abs(N-(A-1)*K)
C = abs(N-A*K)
D = abs(N-(A+1)*K)

print(min(B,C,D))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
nk=list(map(int,input().split(' ')))
nk[0]=nk[0]%nk[1]
while(True):
    if abs(nk[0]-nk[1])<nk[0]:
        nk[0]=abs(nk[0]-nk[1])
    else:
        break
print(nk[0])

Submission status: Accepted

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

Submission status: Accepted

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
i = 0
while(i==0):
  #print(n,k)
  if n>k:
    a = n%k
    if a<n:
      n = a
  else:
    a = abs(k-n)
    if a<n:
      n = a
    else:
      print(n)
      exit()

Submission status: Accepted

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

Submission status: Accepted

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

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

th = .5 * K

n = N % K
while n > th:
    n = abs(n - K)

print(n)

Submission status: Accepted

Solution code:
import bisect,collections,copy,itertools,math,numpy,string
def I(): return int(input())
def S(): return input()
def LI(): return list(map(int,input().split()))
def LS(): return list(input().split())
##################################################
N,K = LI()
amari = N%K
print(min(amari,abs(amari-K)))

Submission status: Accepted

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

Submission status: Accepted

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