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())
if n >= k:
  n = n%k
print(min([n, k-n]))

Submission status: Accepted

Solution code:
first = [int(i) for i in str(input()).split()]
n = first[0]
k = first[1]

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
l=n//k
n_d=abs(n-k*l)
n_u=abs(n-k*(l+1))
if n_u>n_d:
    print(n_d)
else:
    print(n_u)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

MOD = 10**9
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors

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

amari1 = N//K
amari2 = N//K+1
ans = min(abs(N-amari1*K), abs(N-amari2*K))
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr  4 20:49:45 2020

@author: naoki
"""

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

amari1 = N % K
amari2 = abs(K - amari1)


print(min(amari1, amari2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

mod = N % K

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


print(a)

Submission status: Accepted

Solution code:
N, K = list(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())
if K == 1:
    ans = 0
else:
    if N >= K:
        N -= (N//K)*K
        ans = min(N,abs(N-K))
    else:
        ans = min(N,abs(N-K))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
#A=list(map(int,input().split()))
ans=N-K*(N//K)
while True:
    ans=abs(K-ans)
    if ans<=K//2:
        break
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

N = N % K
if K - N < N:
  N = K - N

print(N)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

res = N

if N == K:
    print(0)

elif N < K:
    print(min(K- N, N))

else:
    r = N // K
    print(min(N, N - K * r, K * (r + 1) - N))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import numpy as np

#n=int(sys.stdin.readline())
n,k=map(int,sys.stdin.readline().split())
#a=np.array(sys.stdin.readline().split(),dtype='int64')
#a=np.array([sys.stdin.readline().split() for i in range(n)],dtype='int64')

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

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

n = n%k

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

b = abs(n - k * a)
c = abs(n - k * (a + 1))
print(min(b, c))

Submission status: Accepted

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

mo = N % K

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:


############ ---- USER DEFINED INPUT FUNCTIONS ---- ############
def inp():
    return(int(input().rstrip()))
def inlt():
    return(list(map(int,input().rstrip().split())))
def insr():
    s = input().rstrip()
    return(s[:len(s) - 1])
def invr():
    return(map(int,input().rstrip().split()))
################################################################
n,k = invr()
if k >= 2*n:
    print(n)
elif k >= n:
    print(k - n)
else:
    need = n//k
    remain = n%k
    first = need *k
    print(min(n - first,abs(n - first - k)))
#print()

Submission status: Accepted

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

Submission status: Accepted

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

a = N % K

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#


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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import os

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

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

NN = -(N - K)

print(min(N, NN))

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
y = N // K
print(min(abs(K - N + y * K), abs(K - N + (y - 1) * 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 = N % K
cand2 = abs(cand1 - K)

if cand1 < cand2:
    print(cand1)
else:
    print(cand2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = [int(i) for i in input().split()]
while(True):
    if N > K:
        tmp = N % K
    else:
        tmp = abs(N - K)
    if tmp < N:
        N = tmp
    else:
        break
print(N)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = [int(x) for x in input().split()]
def sub(n, k):
    if n < k:
        return min(n, k - n)
    return sub(n % k, k)

print(sub(n, k))

Submission status: Accepted

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

Submission status: Accepted

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

modK = N % K 

if modK < K - modK:
  Ans = modK
else:
  Ans = K - modK

print(Ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

def one_int():
    return int(input())

def one_str():
    return input()

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

N, K = input_int()

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

Submission status: Accepted

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

t = n // k

n -= t*k
print(min(abs(n-k), n))

Submission status: Accepted

Solution code:

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

tmp = n % k
tmp2 = abs(tmp - k)

print(min(tmp , tmp2))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))

def main():
    N, K = input_nums()
    a = N // K
    print(min(N-K*a, abs(N-K*(a+1))))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

if N>K:
    N = N%K

while True:
    x = abs(N-K)
    if x<N:
        N=x
        continue
    else:
        break
print(N)

Submission status: Accepted

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

Submission status: Accepted

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

X = N % K
Y = abs(X - K)

if X > Y:
    print(Y)
else:
    print(X)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
if N > K:
  m = N //K
  a = N - m*K
else:
  a = N
b = K - a
print(min(a,b))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
# 0, 1, 2...

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

b = min(N,K)
for i in range(100000):
    if b > max(N,K)-min(N,K):
        b = max(N,K)-min(N,K)
    else:
        break

print(b)

Submission status: Accepted

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


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

    # if k == 1:
    #     return 0

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

    if amari > sabun:
        return sabun

    return amari


if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)  # 再帰関数の再帰の深さを設定
to_index = lambda x: int(x) - 1  # 入力した数字に1を引いたものを返す
print_list_in_2D = lambda x: print(*x, sep="\n")  # リストの要素を改行を挟んで表示する関数

# 入力を整数に変換して受け取る
def input_int(): return int(input())

def map_int_input(): return map(int, input())

MII = map_int_input

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

def MII_to_index(): return map(to_index, input())

def MII_split_to_index(): return map(to_index, input().split())

# 入力全てを整数に変換したものの配列を受け取る
def list_int_inputs(): return list(map(int, input()))

LII = list_int_inputs

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

# 2次元リスト化
def LII_2D(rows_number): return [LII() for _ in range(rows_number)]

def LII_split_2D(rows_number): return [LII_split() for _ in range(rows_number)]

N, K = MII_split()

amari = abs(N % K)

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

Submission status: Accepted

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

Submission status: Accepted

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

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

    print(ans)


resolve()

Submission status: Accepted

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

Submission status: Accepted

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

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

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 = [int(w) for w in input().split()]

t = n // k
print(min([n, abs(n - k * t), abs(n - k * (t + 1))]))

Submission status: Accepted

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


cand1 = N % K
cand2 = abs(N % K - K)

print(min(cand1, cand2))

Submission status: Accepted

Solution code:
def main():
    N,K=map(int, input().split())
    ans=N
    for i in range(100000):
        N=N%K
        ans=min(ans,N)
        if N==0:
            break
        N=-N
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

a=n%k

b=k-a

print(int(min(n,a,b)))

Submission status: Accepted

Solution code:
import sys
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

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

left = N // K
right = left + 1

ans = min(abs(N - K * left), abs(N - K * right))

print(ans)

Submission status: Accepted

Solution code:
n,k=list(map(int,input().split()))
n1=n%k
#絶対正
n2=abs(k-n%k)
print(min(n1,n2))

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

if n > k:
    n = n % k

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

if N>=K:
  cand1 = N % K
else:
  cand1 = N
cand2 = abs(cand1-K)

answer = min(cand1, cand2)

print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

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:
def main():
  x, y = map(int, input().split())
  a = x % y
  b = abs((x % y) - y)
  print(min([a,b]))
main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = [int(x) for x in 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(abs(n%k-k), n%k))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


def solve(N: int, K: int):
    N = N % K
    if N > K // 2:
        print(K - N)
    else:
        print(N)
    return


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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
n, k = [int(j) for j in sys.stdin.readline().split()]
print(min(n%k, k-n%k))

Submission status: Accepted

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

result = N
if N > K:
    result = min(result, N % K)
for i in range(1000):
    result = min(result, abs(result - K))
print(result)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
n,k=map(int,sys.stdin.readline().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())
a = N // K
print(min(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:
# print('input >>')
N, K = map(int,(input().split()))

mod = N % K

# print('-----output-----')
print(min(abs(mod-K), mod))

Submission status: Accepted

Solution code:
''' بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ '''
#codeforces
gi = lambda : list(map(int,input().split()))
n, m = gi()
vis = set()
ans = n + m;
while n not in vis:
	vis.add(n)
	ans = min(ans, n);
	n %= m
	vis.add(n)
	ans = min(ans, n);
	n = abs(n - m)
ans = min(ans, n);
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from sys import stdin
N,K = [int(x) for x in stdin.readline().rstrip().split()]
ans = N%K
print(min(ans,abs(ans-K)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K = map(int,input().rstrip().split(" "))
print(min(N % K,abs(N % K - 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 = [int(x) for x in input().split()]
ans1 = N % K
ans2 = K - ans1
print(min(ans1, ans2))

Submission status: Accepted

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

Submission status: Accepted

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

if n % k == 0:
    print(0)
    exit()

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

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

div = N // K

tmp1 = N - K*div
tmp2 = abs(N-K*(div+1))

print(min(tmp1, tmp2))

Submission status: Accepted

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

res = N%K #abs(N-K)
while True:
    tmp = abs(res-K)
    if(res > tmp):
        res = tmp
    else:
        break
print(res)

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

Submission status: Accepted

Solution code:

n,k = map(int,input().split())
a1 = n%k
a2 = n%k -k
print(min(abs(a1),abs(a2)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect as bs

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

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

Submission status: Accepted

Solution code:
list_in=list(map(int,input().split()))
N = list_in[0]
K = list_in[1]

tmp = N // K
x = N - (tmp * K)

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

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

Submission status: Accepted

Solution code:
from fractions import gcd
def main():
    n, k = map(int, input().split())
    if n >= k:
        n = n % k
    ans = min(n, k-n)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

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

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

Submission status: Accepted

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

n %= k

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

    print(min(a,b))

    return 0

if __name__ == '__main__':
    sys.exit(main())

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N, K = LI()
    if N < K:
        ans = min(abs(K-N), N)
    else:
        N = N % K
        ans = min(abs(K-N), N)
    print(ans)

main()

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

import sys

DEBUG = False

def read(t):
    return t(sys.stdin.readline().rstrip())


def read_list(t, sep = " "):
    return [t(s) for s in sys.stdin.readline().rstrip().split(sep)]


def dprint(*args, **kwargs):
    if DEBUG:
        print(*args, **kwargs)
    return


def main():
    n, k = read_list(int)
    print(min(n, n - (n // k) * k, abs(n - (n // k + 1) * k)))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

     n_d = n % k
     ans = n_d
     while(ans > abs(ans - k)):
          ans = abs(ans - k)
     
     print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

ans = n % k
if ans > abs(ans - k):
    print(abs(ans - k))
else:
    print(ans)

Submission status: Accepted

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

if n > k:
  n = abs(n%k)

while True:
  n_ = abs(n-k)
  if n_ >= n:
    print(n)
    break
  else:
    n = n_

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

Submission status: Accepted

Solution code:
#create date: 2020-04-04 21:11

import sys
stdin = sys.stdin

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n, k = na()
    print(min(n%k, k-(n%k)))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

from sys import exit, setrecursionlimit
setrecursionlimit(10**6)

def array2D(row, col ,val=0):
    return [[val]*col for i in range(row)]

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

a1 = x//k
ans = 10**30
for i in range(-10, 11):
    ans = min(ans, abs(x-(a1+i)*k))
print(ans)

Submission status: Accepted

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

ans = n
q, r = divmod(n, k)
ans = min(ans, abs(r-k))
if r == 0:
    ans = 0
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

visited = set()
x = n
while x not in visited:
    visited.add(x)
    if x > k:
        x = x % k
    else:
        x = k - x
print(min(visited))

Submission status: Accepted

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

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

Submission status: Accepted

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

q = n //k

ans = n - q*k

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

Submission status: Accepted

Solution code:
import collections
import sys
import numpy as np
sys.setrecursionlimit(1000000000)

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

if n%k == 0:
    print(0)
else:
    c = n%k

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

c = n % b

d = abs(c - b)

print(min(c,d))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve(n, k):
    if n > k:
        n = n % k
    return min(n, k - n)


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


def test():
    assert solve(7, 4) == 1
    assert solve(2, 6) == 2
    assert solve(1000000000000000000, 1) == 0


if __name__ == "__main__":
    test()
    main()

Submission status: Accepted

Solution code:
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

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

c = N // K

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

Submission status: Accepted

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

n = n % k
ans = n
while True:
    ans = min(ans, n)
    n = abs(n - k)
    if n <= ans:
        break
print(n)

Submission status: Accepted

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