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

i = abs(n % k)
j = abs(n % -k)
if i == 0:
	print(0)
	exit(0)
print(min(i, j, n))

Submission status: Accepted

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

counter = 0
x_min = n
x = n
while not counter == 3:
    x = x % k
    if x_min > x:
        x_min = x
        counter = 1
    elif x_min == x:
        counter = counter + 1
    x = k - x

print(x_min)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
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())
if N > K:
    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())

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:

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

if K == 1:
  print(0)
else:
  a = N % K
  if a > K-a:
    print(K-a)
  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:
a,b=map(int,input().split())
k=a%b
if k<=b/2:
    print(k)
else:
    print(b-k)

Submission status: Accepted

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

ans1 = n%k
ans2 = abs(k - n%k)

print(min(ans1,ans2))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

n=n%k

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

Submission status: Accepted

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

Submission status: Accepted

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

shou,lis = n//k,[]
if shou!=0: n=n%(shou*k)
for i in range(10):
  n=abs(n-k)
  lis.append(n)
print(min(lis))

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:
    N = K + 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)
else:
    num1 = N % K
    num2 = abs(K - num1)
    print(min(num1, num2))

Submission status: Accepted

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

c = n//k
plus = abs(n - c*k)
minus = abs(n - (c+1)*k)

print(min(plus,minus))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#設定
import sys
input = sys.stdin.buffer.readline

#ライブラリインポート
from collections import defaultdict

con = 10 ** 9 + 7
#入力受け取り
def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	N, K = getlist()
	ans = min(N % K, (K - N) % K)
	print(ans)


if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
import sys
import math
from collections import deque
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
def I(): 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 S(): return sys.stdin.readline()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h,w): return [[i for j in range(w)] for k in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**6+10)


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

Submission status: Accepted

Solution code:
n,k=[int(x) for x in input().split()]
a=n%k
b=k-a
if a>b:
  print(b)
else:
  print(a)

Submission status: Accepted

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


x = n % k
y = k - x

if x > y:
    print(y)
    
else:
    print(x)

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

Submission status: Accepted

Solution code:
N, K = 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())
if N>=K:
    N = N%K
    while abs(N-K) < N:
        N = abs(N-K)
    print(N)
else:
    if K >= 2*N:
        print(N)
    else:
        N = K%N
        print(N)

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())
a = N % K
b = abs(a-K)
if a >= b:
    print(b)
else:
    print(a)

Submission status: Accepted

Solution code:
import sys;input=lambda:sys.stdin.readline().rstrip();aint=lambda:int(input());ints=lambda:list(map(int,input().split()))
import math;floor,ceil=math.floor,math.ceil
from collections import deque
Yes=lambda b:print('Yes')if b else print('No');YES=lambda b:print('YES')if b else print('NO')
is_even=lambda x:True if x%2==0 else False

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

a=N//K

p=abs(N-a*K)
m=abs(N-(a+1)*K)

if p>m:
  print(m)
else:
  print(p)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
N,K=(int(x) for x in input().split())
x=N%K
if x>(K/2):x-=K
print(abs(x))

Submission status: Accepted

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

Submission status: Accepted

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

x = N % K
y = K - x 

if x <= y:
    print(x)
else:
    print(y)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
M, K = map(int,input().split())
M = M % K
switch = 1001001001
while True:
    if switch < 0:
        break
    switch = M-K
    M = min(M,abs(M-K))
print(int(M))

Submission status: Accepted

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

Submission status: Accepted

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

current = n
absV = 0
if current - k > 100000000:
    current = current % k
while True:
    absV = abs(current - k)
    if current < absV:
        print(current)
        exit()
    else:
        current = absV

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

amari = N % K
z_amari = abs(amari - K)

print(min([amari, z_amari]))

Submission status: Accepted

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

j = N//K

if N-j*K > -(N-(j+1)*K):
    print(-(N-(j+1)*K))
else:
    print(N-j*K)

Submission status: Accepted

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

Surplus = N % K
K_Surplus = K - Surplus

if Surplus <= K_Surplus:
  print(Surplus)
else:
  print(K_Surplus)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

  v = n % k

  print(min(v, k - v))

main()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

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

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#############
# Main Code #
#############

N,K = inputIL()

r = N%K

r2 = K-r

print(min(r,r2))

Submission status: Accepted

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

remin = N%K

ans = min(remin,K-remin)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def sovle(x, y):
    while(x>=y):
        x=x-x//y*y
    if(x>y-x):
        return y-x
    else:return x



if __name__ == '__main__':
    nums = list(map(int, input().split(" ")))
    print(sovle(nums[0], nums[1]))

Submission status: Accepted

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

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 + 1
上記の回数回して結果のリストを作成し、
最小値を出力する

興味があるのは、差が正から負へ変わった瞬間の２つの値のみ

NをKで割った商をKにかけた数を引いた数とそれにKを一回足した数
上記２つの数の内小さい方を出力すればよい
'''
import math

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

num1 = abs(N - math.ceil(N // K) * K)
num2 = abs(num1 - K)

print(num1) if num1 < num2 else print(num2)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
n, k = map(int, input().split())
t = n % k
print(min(t, k - t))

Submission status: Accepted

Solution code:
from sys import stdin
import math

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

mod = N % K

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

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()


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

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

if __name__ == '__main__':
  main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K = map(int, input().split())
if N == 0:
    print(0)
else:
    for i in range(18):
        while True:
            if N > K*10**(18-i):
                N = N-(K*10**(18-i))
            else:
                break

    x0 = N #7
    x1 = abs(x0-K) #3
    while True:
        if x0 < x1:
            print(x0)
            break
        else:
            x0,x1 = x1,abs(x0-K)
            continue

Submission status: Accepted

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

m = n % k
m1 = k - m

if m > m1:
  ans = m1
else:
  ans = m
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N=[int(x) for x in input().split()]
if N[0]%N[1]==0:
   print("0")

elif abs(N[0]%N[1]- N[1])<N[0]:
     print(abs(N[0]%N[1]- N[1]))

else:
     print(N[0])

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
s = input().split(' ')
n, k = int(s[0]), int(s[1])
p = [n, n % k, abs((n % k) - k)]
res = p[0]
for x in p:
	res = min(res, x)
print (res)

Submission status: Accepted

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

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

Submission status: Accepted

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

ans=0

if n<k:
  ans=min(n,abs(n-k))
  print(ans)
else:
  ans=min(int(n%k),abs(int(n%k)-k))
  print(ans)

Submission status: Accepted

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

while True:
    if B>abs(B-A):
         B=abs(B-A)
    else:
         break

print(B)

Submission status: Accepted

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

s = n // k
a = n % k
if a == 0:
    ans = 0
else:
    ans = min(abs(n - (s * k)), abs(n - ((s + 1) * k)))
print(ans)

Submission status: Accepted

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

amari = N % K
nokori = abs(amari-K)

print(min(amari, nokori))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
ans = N
x = abs(N - K * max(N // K, 1))
while ans > x:
    ans = x
    x = abs(x - K * max(x // K, 1))
print(ans)

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

# check
if K == 1:
    print("0")
elif abs(N - K) > N:
    print(N)
else:
    if N % K < abs(N % K - K):
        print(N % K)
    else:
        print(abs(N % K - K))

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
[N,K]=list(map(int,input().split()))
def hoge(X):
	if X<0:X*=-1
	return X
print(sorted(map(hoge,[(N//K)*K-N,(N//K)*K+K-N]))[0])

Submission status: Accepted

Solution code:
inp=[int(x) for x in input().split()]
n,k=inp[0],inp[1]
if n>k:
  print(min(n%k,k-n%k))
elif n<k:
  print(min(n,k-n))
else:
  print(0)

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(k-n%k)))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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-(n//k)*k
print(min(n,k-n))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#import numpy as np


def solve():
    m = n
    if m > k:
        m = m % k
    ans = min(m, abs(k-m))
    print(ans)



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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

m = (a - a%b) / b
print(min(a%b, b - a%b))

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

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

t_1 = N % K
t_2 = K - t_1

if t_1 > t_2:
    print(t_2)
else:
    print(t_1)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    n, k = map(int, rl().split())
    ans = min(n % k, abs(n % k - k))
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# solution

nim, mike = map(int,input().split())
print(abs(min(nim%mike, mike-nim%mike)))

Submission status: Accepted

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

Submission status: Accepted

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

loop = [0]

for i in loop:
  if num[0] < num[1]:
    difference = abs(num[0] - num[1])
    if difference > num[0]:
      break
    else:
      num[0] = difference
      break
  elif num[1] == 1:
    num[0] = 0
    break
  elif num[1] == 0:
    break
  if i == 0:
    num[0] = num[0] % num[1]
  num[0] = abs(num[0] - num[1])
  loop.append(i+1)

print(num[0])

Submission status: Accepted

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

Submission status: Accepted

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

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


print(min(b,c))

Submission status: Accepted

Solution code:
N,K = map(int, input().split())
import math
a = N % K;
b = K - a;

print(a if a <= b else b)

Submission status: Accepted

Solution code:
import numpy as np


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

time = N // K
min_ = N- time*K
min = N- (time+1)*K
print (min_ if min_<np.abs(min) else np.abs(min))

Submission status: Accepted

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

Submission status: Accepted

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

mod = N % K
mod2 = abs(mod - K)

print(min(mod, mod2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#https://atcoder.jp/contests/abc161/tasks/abc161_c
# -*- coding: utf-8 -*-
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())
x = N%K
y = N%K-K
print(min(abs(x), abs(y)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

l = n//k
ans = abs(n-l*k)
l = n//k + 1
ans = min(ans, abs(n-l*k))

print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
n %= k
while n > abs(n - k):
    n = abs(n - k)
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:
import sys
input = sys.stdin.readline

n, m = map(int, input().split())
# A = list(map(int, input().split()))
num = n // m
n -= m * num
print(min(n, m - 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 == 0:
    print(0)

elif N<K and N<=K-N:
    print(N)

elif N<K and N>K-N:
    print(K-N)

elif N>K:
    if K-(N%K)>N%K:
        print(N%K)
    else:
        print(K-(N%K))

Submission status: Accepted

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

m=n%k
d=abs(m-k)

if d<m:
    print(d)
else:
    print(m)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
if K==1:
  if N==0:
    print("1")
    exit()
  else:
    print("0")
    exit()
if N > K :
  N-=(N//K)*K
if abs(K-N) < N:
  print(abs(K-N))
else:
  print(N)

Submission status: Accepted

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

Submission status: Accepted

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

def abs(a):
    if a>=0:
        return a
    else:
        return -a

m=n%k

while True:
    b=m
    m=abs(m-k)
    if b<=m:
        print(b)
        break

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N, K = map(int, input().split()) 
    a,b = divmod(N, K)
    print(min(b, K - b))
if __name__ == "__main__":
    main()

Submission status: Accepted

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

n = x%k

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

Submission status: Accepted

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

po=(N//K+1)*K-N
ne=N%K

print(min(po,ne))

Submission status: Accepted

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



y = abs(N - K)
N = y
y2 = abs(N - K)
c=1
dif = (y, y2)

if dif[c-1] < dif[c]:
    dif += (dif[-1],)
    
if dif[c-1] > K:

    dif += (dif[c-1]%K,)
    c += 1

while dif[c-1] > dif[c]:

    yy = abs(dif[c] - K)
    dif += (yy,)
    c += 1


print(min(dif))

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=[int(i) for i in input().split()]
a=n[0]%n[1]
if a<abs(a-n[1]):
    print(a)
else:
    print(abs(a-n[1]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

A, B = map(int, sys.stdin.readline().split())

r = A%B

print(min(r, abs(r - B)))

Submission status: Accepted

Solution code:
import math
N,K=map(int,input().split())
amari=N%K
a=abs(amari-K)
print(min(amari,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())
ans = min(k-(n % k), n % k)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
from bisect import bisect_left 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

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

Submission status: Accepted

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

old = N
new = 0

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

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

Submission status: Accepted

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

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())
import numpy as np
ans1 = N % K
ans2 = np.abs(ans1 - K)

if ans1 < ans2:
  ans = ans1
else:
  ans = ans2
print(ans)

Submission status: Accepted

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

inst = n%k
print(min([inst,k-inst]))

Submission status: Accepted

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

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

a = n%k 
ans = min([a, k-a])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

remainder = N // K
N = N - K*remainder

if N == 0:
    print(N)
    exit();

abc = abs(N-K)

for i in range(10**18):
    N = abc
    abc = abs(N-K)
    if N + abc == K and abc < N:
        break;

print(abc)

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:
a=list(map(int,input().split()))
n=a[0]
k=a[1]
if n>=k:
    if n%k==0:
        print("0")
    else:
        if (n%k)<=(k-(n%k)):
            print(n%k)
        else:
            print(k-(n%k))
else:
    c=k-n
    if c<=n:
        print(c)
    else:
        print(n)

Submission status: Accepted

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

Submission status: Accepted

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

N = N % K
# print(N)
ans_candidate_list = [N]
while True:
    N = abs(N - K)
    # print(N)
    # print(ans_candidate_list)
    if N not in ans_candidate_list:
        ans_candidate_list.append(N)
    else:
        break

print(sorted(ans_candidate_list)[0])

Submission status: Accepted

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

t = n % k
t2 = k -t

print(min(t, t2))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

mod = N % K

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

Submission status: Accepted

Solution code:
def main():
    s = input().split()
    n = int(s[0])
    k = int(s[1])
    x = n % k
    if x > k/2:
        x = abs(x - k)
    print(x)

main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

if n<=(k/2):
    print(n)

else:
    n = n - (n//k)*k
    if n<= k/2:
        print(n)
    else:
        print(abs(n-k))