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

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

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

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

Solution (Python):



from fractions import gcd
from itertools import combinations,permutations,accumulate, product 

from collections import deque,defaultdict,Counter
import decimal
import re












import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n,k = readInts()
print(min(n%k,k - (n%k)))

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

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

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

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

Solution (Python):
import math
import collections
import fractions
import itertools
import functools
import operator
import bisect

def solve():
    n, k = map(int, input().split())
    print(min(n%k, k-(n%k)))
    return 0

if __name__ == "__main__":
    solve()

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, K = mapint()
v = N//K
print(min(N-v*K, K-(N-v*K)))

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

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

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions
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))

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

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

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

mod = N%K

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

Solution (Python):
import sys
N, K = [int(x) for x in input().split()]
if(N>K):
  ans = N % K
else:
  ans = N
while True:
  n_ans = abs(ans - K)
  if(ans<n_ans):
    print(ans)
    sys.exit()
  else:
    ans = n_ans

Solution (Python):
n,k=map(int,input().split())
ok=0
n=n%k
def f(x):
    if abs(x-k)<x:
        f(abs(x-k))
    else:
        print(x)
f(n)

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

Solution (Python):
N,K = map(int,input().split())
i = N%K
j = -i+K
print(min(i,j))

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

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

x=N%K

if x ==0:
    print(0)
elif abs(x - K) > x:
    print(x)
else:
    print(abs(x - K))

Solution (Python):
import sys
import math

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






n = n % k

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

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

Solution (Python):

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

for i in range(N):
    if N % K == 0:
        N = 0
        break
    N = N % K
    if abs(N-K) < N:
        N = abs(N-K)
    else :
        break
print(N)

Solution (Python):
import array
N, K = map(int, input().split(' '))

minimum = x = N % K

cache = set()
while True:
    x = abs(x - K)
    minimum = min(x, minimum)
    if x in cache:
        break
    else:
        cache.add(x)

print(minimum)

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

q_1 = n % k
q_2 = k - q_1
if q_1 < q_2:
  print(q_1)
else:
  print(q_2)

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

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

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

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

print(min(a,K-a))

Solution (Python):

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

Solution (Python):

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

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

S=[]
n1=n%k
S.append(n)
S.append(n1)
for i in range(10):
    n1=abs(n1-k)
    S.append(n1)
  

print( min(S) )

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


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

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

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

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

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

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

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

Solution (Python):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from math import ceil
def main():
    n, k = map(int, input().split())
    if k == 1:
        print(0)
        sys.exit()
    r1 = n % k
    r2 = abs(r1 - k)
    r = min(r1, r2)
    print(r)


if __name__ == '__main__':
    main()

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

Solution (Python):

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

Solution (Python):
score = list(map(int,input().split()))

amari = score[0] % score[1]
kouho = score[1] - amari

if amari < kouho:
    print(amari)
else:
    print(kouho)

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

Solution (Python):
n,k= list(map(int, input().strip().split()))
s=n%k
t=k-s
print(min(s,t))

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

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

Solution (Python):

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

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

Solution (Python):
import sys
n, k = list(map(int, input().split()))

if (k == 1):
    print(0)
    sys.exit()

ans = 0
v_ans = 0
if (n > k):
    v_ans = n % k
else:
    v_ans = n

if (abs(v_ans - k) < v_ans):
    ans = abs(v_ans - k)
else:
    ans = v_ans

print(ans)

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

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

Solution (Python):
N,M = map(int,input().split())

ans = N

if N ==M or N%M ==0:
    print(0)

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

else:
    print(min(N % M,abs((N % M) - M)))

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

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

if N - a*K <= b:
  print(N-a*K)
elif N - a*K > b:
  print(b)

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

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

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

Solution (Python):
def readinput():
    n,k=map(int,input().split())
    return n,k

def main(n,k):
    if n>k:
        n=n%k+k
        n=min(n,abs(n-k))
    return min(n,abs(n-k))

if __name__=='__main__':
    n,k=readinput()
    ans=main(n,k)
    print(ans)

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

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

while N > K:
    N %= K

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

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

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

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

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

if N == 0 or K == 1:
    N = 0
elif N == K:
    N = 0
elif N % K == 0:
    N = 0
else:
    N = N % K
    while N != 0:
        m = abs(N - K)
        if N < m:
            break
        else:
            N = m

print(N)

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

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

t = N % K

res = min(t, K - t)

print(res)

Solution (Python):
import sys
N, K = map(int, input().split())

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

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

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

Solution (Python):
a,b=map(int,input().split())
n = a // b
res = a - n*b
res = min(abs(res-b),res)
print(res)

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

Solution (Python):
import sys
n, k = map(int, sys.stdin.buffer.read().split())
rem = n % k
print(min(rem, k - rem))

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

n = N%K
m = K - n

print(min(abs(n), abs(m)))

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

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

N,k = inpm()

def solve(n):
    if n <= k:
        return min(n,k-n)
    else:
        return solve(n % k)

ans = solve(N)

print(ans)

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

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

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

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

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

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

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

Solution (Python):


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

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

print(ans)

Solution (Python):
def replacing_integer():
    
    N, K = map(int, input().split())
    
    now_num = N % K
    while True:
        if abs(now_num - K) <= now_num:
            now_num = abs(now_num - K)
        else:
            return now_num

result = replacing_integer()
print(result)

Solution (Python):

















































global FLAG_LOG
FLAG_LOG = False


def log(value):
    
    
    if FLAG_LOG:
        print(str(value))


def calculation(lines):
    
    
    N, K = list(map(int, lines[0].split()))
    
    
    
    
    
    
    
    

    tmp = N % K
    result = min(tmp, K-tmp)

    return [result]



def get_input_lines(lines_count):
    lines = list()
    for _ in range(lines_count):
        lines.append(input())
    return lines



def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['7 4']
        lines_export = [1]
    if pattern == 2:
        lines_input = ['2 6']
        lines_export = [2]
    if pattern == 3:
        lines_input = ['1000000000000000000 1']
        lines_export = [0]
    return lines_input, lines_export



def get_mode():
    import sys
    args = sys.argv
    global FLAG_LOG
    if len(args) == 1:
        mode = 0
        FLAG_LOG = False
    else:
        mode = int(args[1])
        FLAG_LOG = True
    return mode



def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines(1)
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation(lines_input)

    for line_result in lines_result:
        print(line_result)

    
    
    
    
    
    
    
    
    
    
    



if __name__ == '__main__':
    main()

Solution (Python):
N, K = map(int, input().split())
if N > K:
  N -= (N//K)*K
ans = N if N <= abs(N-K) else abs(N-K)
print(ans)

Solution (Python):
from sys import stdin
input = stdin.readline


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

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


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

Solution (Python):


from typing import *
import collections
import itertools
import math
import sys

INF = float('inf')


def solve(N: int, K: int):
    return min(N % K, abs(N % K - K))


def main():

    sys.setrecursionlimit(10 ** 6)

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    print(f'{solve(N, K)}')


if __name__ == '__main__':
    main()

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

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

Solution (Python):
import math

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

div = N//K

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

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

Solution (Python):
from sys import stdin
import math
from functools import reduce

n,k = [int(x) for x in stdin.readline().rstrip().split()]

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

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

Solution (Python):
N,K = (int(x) for x in input().split())
p = N % K
if p < (K-p):
    print(p)
else:
    print(K-p)

Solution (Python):
inputValues = [int(i) for i in input().split()]
n = inputValues[0]
k = inputValues[1]
res = n%k
res = min(k-res,res)
print(res)

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

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

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

Solution (Python):
import sys
line = sys.stdin.readlines()[0]
n, k = [int(s) for s in line.split(" ")]
t = n // k
a1 = abs(n - (k * t))
a2 = abs(n - k * (t + 1))
answer = min(a1, a2)
print(answer)

Solution (Python):


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

remainder = N % K

if remainder > K / 2:
  print(abs(remainder - K))
else:
  print(remainder)

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

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

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

Solution (Python):
import sys
import itertools

import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines



A, B = map(int, input().split())
x = A % B
print(min(x, B - x))

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

num_count = (N-N%K)//K
ans = min(abs(N - num_count*K), abs(N - (num_count+1)*K))
if num_count > 0:
    ans = min(abs(N - (num_count-1)*K), ans)
print(ans)

Solution (Python):
l=input("").split(" ")
n=int(l[0])
k=int(l[1])
s=n%k
ss=k-s
if(s<ss):
    print(s)
else:
    print(ss)

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

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

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

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

print(min([m, K-m]))

Solution (Python):
N,K = map(int, input().split())
if N % K == 0:
    print(0)
elif N > K:
    hako1 = N%K
    hako2 = abs(hako1-K)
    if hako1 > hako2:
      print(hako2)
    else:
      print(hako1)
else:
    if N >= abs(N-K):
        print(abs(N-K))
    else:
        print(N)

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

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

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

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

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

if n > 2*k :
    n = n%(2*k)

while True:
    mae = n
    n = abs(k-n)
    if mae <= n:
        break

print(mae)

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

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

Solution (Python):
n,k = map(int,input().split())
v = n//k
ans = float('inf')
for i in (1,-1):
    for j in (1,-1):
        for l in (v,v+1):
            ans = min(ans,abs(i*n+j*l*k))
print(ans)

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

ans=n % k

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

Solution (Python):

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

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

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

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


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

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

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

Solution (Python):


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

while N >= K:
  N = N % K

frag = 0
while frag == 0:
  cand = abs(N - K)
  if cand < N:
    N = cand
  else:
    frag = 1
print(N)

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

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

Solution (Python):

from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)

def main():
    N, K = LI()
    x = N % K
    y = abs(x - K)
    print(min(x, y))

main()

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

Solution (Python):
N,K=map(int,input().split())
A=0
for a in range(100000000000000):
    if N>K:
        kot=N%K
        if abs(K-kot)>kot:
            print(kot)
            break
        else:
            print(abs(K-kot))
            break
    if N%K==0:
        print(0)
        break
    if N<K:
        if abs(N-K)<N:
            print(abs(N-K))
            break
        else:
            print(N)
            break
    tugi=abs(N-K)
    N=tugi
    A=tugi
    tugi=abs(N-K)
    B=tugi
    N=tugi
    tugi=abs(N-K)
    if A==tugi:
        if B<A:
            print(B)
            break
        else:
       	    print(A)
            break

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

if n >= k:
  ans_a = n % k
  ans_b = abs(n %k - k)
  ans = min(ans_a, ans_b)
else:
  if n >= abs(n-k):
    ans = abs(n-k)
  else:
    ans = n

print(ans)

Solution (Python):

import sys
import collections as cl


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 main():
    n, k = MI()

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


main()

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

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

tmp = n % k

if tmp <= abs(tmp - k):
  print(tmp)
else:
  print(abs(tmp - k))

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

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

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

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

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

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

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

Solution (Python):
n, k = list(map(int, input().split()))
if n == k:
    n = 0
else:
    while n > abs(n - k):
        if n > k:
            n = min(n, n % k)
        elif k > n:
            n = min(n, k % n)

print(n)

Solution (Python):
import sys
input = sys.stdin.readline

def binary_search_int(ok, ng, solve):
    """
    :param ok: solve(x) = True を必ず満たす点
    :param ng: solve(x) = False を必ず満たす点
    """
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if solve(mid):
            ok = mid
        else:
            ng = mid
    return ok, ng



def solve(x):
    """ ある点 x を境にして True と False が二分されるような関数"""
    return N - K*x >= 0

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

a, b = binary_search_int(0, 10**18, solve)
print(min(abs(N-a*K), abs(N-b*K)))

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

N = N % K
if (K - N) < N:
  N = K - N
print(N)

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

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

    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline

def binary_search_int(ok, ng):
    """
    :param ok: solve(x) = True を必ず満たす点
    :param ng: solve(x) = False を必ず満たす点
    """
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if solve(mid):
            ok = mid
        else:
            ng = mid
    return ok, ng



def solve(x):
    """ ある点 x を境にして True と False が二分されるような関数"""
    return N - K*x >= 0

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

a, b = binary_search_int(0, 10**18)
print(min(abs(N-a*K), abs(N-b*K)))

Solution (Python):
import sys
input = sys.stdin.readline

def binary_search_int(ok, ng, solve):
    """
    :param ok: solve(x) = True を必ず満たす点
    :param ng: solve(x) = False を必ず満たす点
    """
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if solve(mid):
            ok = mid
        else:
            ng = mid
    return ok, ng



def solve(x):
    """ ある点 x を境にして True と False が二分されるような関数"""
    return N - K*x >= 0

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

a, b = binary_search_int(0, 10**18, solve)
print(min(abs(N-a*K), abs(N-b*K)))

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

def f(x):
    return( N-x*K )

def test(x):
    return( f(x) >= 0 )

left = - 1        
right = 10**18

while right - left > 1:             
    mid = (left+right)//2
    if test(mid):
        left = mid
    else:
        right = mid
print(min(abs(N-left*K), abs(N-right*K)))

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

Solution (Python):
def main():
    N, K = map(int, input().split())
    v = N // K
    print(min([N - K * v, abs(N - K * (v + 1))]))


if __name__ == '__main__':
    main()

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

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

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

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

Solution (Python):
N, K = (int(x) for x in input().split())
N%=K
dif=N-K
if dif<0:
  dif*=-1
print(min(N, dif))

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

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

Solution (Python):
n,k = map(int,input().split(" "))
a = n%k
b= abs(k-a)
if a <= b:
  print(a)
else:
  print(b)

Solution (Python):
import sys


def main():
    input = sys.stdin.buffer.readline
    n, k = map(int, input().split())
    n %= k
    while True:
        if abs(n - k) >= n:
            break
        else:
            n = abs(n - k)
    print(n)


if __name__ == '__main__':
    main()

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

r = n % k

ans = min(r, k - r)

print(ans)

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

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

if n>=k:
  ans=abs(n-((n+k//2)//k)*k)

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

print(ans)

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

Solution (Python):
n, k = (int(i) for i in input().split())

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

print(ans)

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

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

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

x = abs((N%K) - K)
if x > K/2:
    x = abs(x - K)
print(x)

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

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

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

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

Solution (Python):


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

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

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

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

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

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

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

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

if n == k:
    print(0)
elif n < k:
    print(min(n, k - n))
else:
    n = n % k
    print(min(n, k - n))

Solution (Python):

import sys
N,K = map(int,input().split())
num = float("inf")
n = 0

for i in reversed(range(1,18)):
    n = (10**i)*K
    for j in range(9):
        N -= n
        if N < 0:
            N+=n
            break

if N == 0:
    print(0)
    sys.exit()
    

while num > K:
    num = abs(N-K)
    N = num
    

if abs(N-K) > num:
    print(num)
else:
    print(abs(N-K))

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



if k<n:
  n-=n//k*k



if k-n<n:
  n=k-n
  
print(n)

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

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

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

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

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

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

print(ans)

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

if N%K == 0:
    print(0)
if N%K != 0 and N > K :
    a = N%K
    b = K%a
    if a >= b :
        print(b)
    if a < b:
        print(a)
if N%K != 0 and N < K :
    c = K-N
    if c > N:
        print(N)
    if c < N:
        print(c)

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

a, b = divmod(N, K)

if K == 1:
    print('0')
else:
    result = min(N, b, abs(K - b))
    print(result)

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

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

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

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

tmp = n%k

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

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

i = N // K
N = N - K * i
A = N
B = -(A-K)
ans = min(A,B)

print(ans)

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

Solution (Python):
import sys
input = sys.stdin.readline

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

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

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

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

Solution (Python):
N, K = list(map(int, input().split()))
if N == K:
    print(0)
    exit()
elif N < K:
    ans = min(N, K - N)
    print(ans)
    exit()
elif N > K:
    temp1 = N - N // K * K
    temp2 = abs(temp1 - K)
    ans = min(temp1, temp2)
    print(ans)
    exit()

Solution (Python):
N, K = map(int, input().split())
if  N % K == 0:
    print(0)
    exit()
m = N // K + 1
print(min(m * K - N, N - (m - 1) * K))

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

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

Solution (Python):
import sys
n,k = map(int,input().split())
if n%k == 0:
	print(0)
	sys.exit()
num = n - k*(n//k)
abnum = abs(num - k)
print(min(num,abnum))

Solution (Python):
import sys
n,k = map(int,input().split())
if n%k == 0:
	print(0)
	sys.exit()
num = n - k*(n//k)
abnum = abs(num - k)
print(min(num,abnum))

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

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

Solution (Python):
N, K = input().split(' ')
NA = int(N)
K = int(K)
if NA - K > K:
  NA = NA - K*(NA//K - 1)
N1 = abs(NA-K)

while N1 < NA:
  NA = N1
  N1 = abs(N1-K)
else:
  print(NA)

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

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

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

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