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

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

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

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

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

Solution (Python):

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

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

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

a = a % b

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

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

print(min(t, K-t))

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

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

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

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

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


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


n = abs(n - k)
ans2 = n


print(min(ans1, ans2))

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())
l=n//k
m=min(abs(l*k-n),abs((l+1)*k-n))
while n > m:
  n=m
print(n)

Solution (Python):

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

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

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

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

print(answer(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())
a = n%k
ans = min(a, k-a)
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())      
print(min(N%K,abs(N%K-K)))

Solution (Python):
import sys


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


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

Solution (Python):

N, K = map(int, input().split())
q, mod = divmod(N, K)
print(min(mod, K-mod))

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

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

t = N % K
k = K - t

print(min(t, k))

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


if __name__ == '__main__':
    main()

Solution (Python):

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


N = N % K 


minimum = min(N, abs(N-K))

print(minimum)

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


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

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

Solution (Python):

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

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

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

print(ans)

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

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

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

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

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

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

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

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

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

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

print(ans)

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

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

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

a = N % K
b = K - a

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

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

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

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

Solution (Python):
N,K=map(int, input().split()) 
y = N//K
x = N - y*K

ans = min(x, abs(x-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()) 

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

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

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

ans = n%k

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

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

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

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

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

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

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

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

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

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

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

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

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

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


def get_nums_l():
    return [ int(s) for s in input().split(" ")]


def get_nums_n(n):
    return [ int(input()) for _ in range(n)]


def get_all_int():
    return map(int, open(0).read().split())

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

n,k = get_nums_l()

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

Solution (Python):
import sys
import os

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, K = il()
    q, mod = divmod(N, K)
    print(min(mod, abs(K-mod)))


if __name__ == '__main__':
    main()

Solution (Python):

from math import ceil
N, K = map(int, input().split())
v = ceil(N / K)
ans = abs(N - v*K)
print(min(N, abs(N%K - K), N%K))

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

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

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

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

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

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

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

print(ans)

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


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

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

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

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

Solution (Python):

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


if __name__ == "__main__":
    main()

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

N = N % K

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

print(N)

Solution (Python):
n, k = map(int, input().split(' '))
n = n % k
if n > k / 2:
    ans = abs(n-k)
else:
    ans = n
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())
syou = N//K
min_1 = N - syou*K
min_2 = -(N - (syou+1)*K)
if min_1 < min_2:
    print(min_1)
else :
    print(min_2)

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

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

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

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



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

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

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

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

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

Solution (Python):
n, k = map(int,input().split())
if n < k:
  if n > k/2:
    print(k-n)
  else:
    print(n)
elif n%k > 0:
  print(min(n%k, k-n%k))
else:
  print(0)

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

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

Solution (Python):
n,k=map(int,input().split())
amari=n%k
mainasu=n-(n//k+1)*k
if amari<abs(mainasu):
  print(amari)
else:
  print(abs(mainasu))

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

ans = None
if N < K:
    ans = min(N, abs(N - K))
else:
    N = N % K
    ans = min(N, abs(N-K))


print(ans)

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

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

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

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

a = N%K
b = K-N%K

ans = min(a,b)
print(ans)

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

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

print(ans)

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

Solution (Python):
import math
n, k = map(int, input().split())
if n <= k:
    print(min(n, k - n))
else:
    t = (n - k - 1) // k + 1
    n = n - t * k
    print(min(n, k - n))

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

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

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

if __name__ == '__main__':
    main()

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

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

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

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

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

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

Solution (Python):
n,m=map(int,input().split())
n%=m
if 2*n>m:
    print(m-n)
else:
    print(n)

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

Solution (Python):
cl1 = input().split()
cl1 = [int(s) for s in cl1]
x = cl1[0]
k = cl1[1]

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

Solution (Python):

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

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

Solution (Python):
n,m=map(int,input().split())
ans=n%m
if ans<abs(ans-m):
    print(ans)
else:
    print(abs(ans-m))

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

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

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

Solution (Python):
import bisect
import collections
import sys

sys.setrecursionlimit(100000)
input = sys.stdin.readline
ACMOD = 1000000007
INF = 1 << 62


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


def llmi(n):
    return [lmi() for _ in range(n)]


N, K = lmi()
tmp = N % K

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

Solution (Python):
n,k=map(int,input().split())
a=(n//k)-1
b=(n//k)
c=(n//k)+1

x=abs(n-a*k)
y=abs(n-b*k)
z=abs(n-c*k)

print(min(x,y,z))

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

ans = N%K

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

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

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

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

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

Solution (Python):
n, k = (int(x) for x in input().split())
i = n % k
if i > (k / 2):
  ans = abs(i - k)
else:
  ans = abs(i)
print(ans)

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

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

Solution (Python):
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time
import random


def I(): return int(input())


def S(): return input()


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


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


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


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False



def main():
    N, K = MI()
    v = min(N % K, abs(N % K - K))
    print(v)
    

    
    
    
    
    
    
    
    

        
        

        
        
        
        
        
        



if __name__ == '__main__':
    main()

Solution (Python):
import sys

N,K = map(int, sys.stdin.readline().strip().split())
a = N % K
print(min(a, K - a))

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

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

Solution (Python):
import sys
input = sys.stdin.readline
from collections import Counter, deque

INF = float('inf')


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


if __name__ == '__main__':
    main()

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

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

Solution (Python):

def resolve():
    N, K = map(int, input().split())
    t = N%K
    return print(min(t, K-t))

if __name__ == "__main__":
    resolve()

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

a = N%K

print(min(a,K-a))

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

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

Solution (Python):
def getInput():
    return [int(i) for i in input().rstrip().split(' ')]

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

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

Solution (Python):
import sys

def main():
    n, k = [int(_) for _ in input().split()]
    
    m = abs(n - n // k * k)
    while True:
        ans = abs(m - k)
        if m <= ans:
            print(m)
            sys.exit()
        m = abs(m - k)
main()

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

Solution (Python):
n,k = (int(x) for x in input().split())
if n > k:
  n -= k*(n // k)
ans = min(n, k-n)
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 <= k//2:
    print(n%k)
else:
    print(k-n%k)

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

a = N % K

if a - K >= 0:
  b = a - K
  
else:
  b = K - a
  
print(min(a,b))

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

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

main()

Solution (Python):
def absolute(N,K):
    return abs(N-K)
    
N,K=map(int,input().split()) 

valuelist = [N%K,absolute(N%K,K)]
     
print(min(valuelist))

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

Solution (Python):
def absolute(N,K):
    return abs(N-K)
    
N,K=map(int,input().split())
 
N=N%K
valuelist = []
 
for i in range (0,2):
    N=absolute(N,K)
    valuelist.append(absolute(N,K))
      
print(min(valuelist))

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

Solution (Python):

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

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

if __name__ == '__main__':
    main()

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

Solution (Python):

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

min_val = N
memory = [N]
n = N
while True:
    if n > K*2:
        n = n % K
        memory.append(n)
        if min_val > n:
            min_val = n
        continue
    n = abs(K-n)
    if n in memory:
        break
    memory.append(n)
    if min_val > n:
        min_val = n
    if min_val == 0:
        break
print(min_val)

Solution (Python):
def absolute(N,K):
    return abs(N-K)
    
N,K=map(int,input().split())
 
N=N%K
valuelist = []
 
for i in range (0,2):
    i+=1
    N=absolute(N,K)
    valuelist.append(absolute(N,K))
            
print(min(valuelist))

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

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

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

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

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

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

print(x)

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

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

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7 
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n, k = map(int,input().split())
    n %= k
    print(min(n, k - n))
resolve()

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

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

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

Solution (Python):

import sys


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

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    solve(N, K)

if __name__ == '__main__':
    main()

Solution (Python):
def count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count

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

def input_list_str():
  return list(map(str, input().split()))

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c


def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

def main():
  n,k = input_list()
  a = n % k
  b = k - a
  print(min(a, b))

import math
import fractions
import collections
import itertools
from functools import reduce
main()

Solution (Python):
def absolute(N,K):
    return abs(N-K)
    
N,K=map(int,input().split())

N=N%K
valuelist = [N%K]

for i in range (0,3):
    i+=1
    N=absolute(N,K)
    valuelist.append(absolute(N,K))
        
print(min(valuelist))

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

Solution (Python):
def absolute(N,K):
    return abs(N-K)
    
N,K=map(int,input().split())
N=N%K
valuelist = [N%K]

for i in range (0,3):
    i+=1
    N=absolute(N,K)
    valuelist.append(absolute(N,K))
    
    
print(min(valuelist))

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

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

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

Solution (Python):
N,K=[int(i) for i in input().split(" ")]
proc=lambda x:abs(x-K)
a=N%K
b=K-a
if a>b:
    print(b)
else:
    print(a)

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

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

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

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

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

Solution (Python):
import sys
import os
import math

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, K = il()
    q, mod = divmod(N, K)
    print(min(mod, abs(K - mod)))


if __name__ == '__main__':
    main()

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

while True:
    new_saisyou =abs(saisyou-K)
    if new_saisyou>= saisyou:
        break
    else:
        saisyou = new_saisyou

print(saisyou)

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

def replace(N,K):
    a = N%K
    return min(a,abs(a-K))

print(replace(N,K))

Solution (Python):
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

from math import ceil

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

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

Solution (Python):



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

if __name__=='__main__':
  main()

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

a = N%K

b = abs(a-K)

while a > b:
    a = b
    b = abs(b-K)

print(a)

Solution (Python):
import sys
import math

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

pre=abs(n-math.floor(n/k)*k)

while 1:
    if pre <= abs(pre-k):
        print(pre)
        sys.exit()
    pre = abs(pre - k)

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

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(K-(N%K))))

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

K = N%X
k = X - K

if K == 0:
  print(0)
elif K >= k:
  print(k)
else:
  print(K)

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

Solution (Python):
import sys
import math

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

pre=abs(n-(math.floor(n/k)*k))

while 1:
    if pre <= abs(pre-k):
        print(pre)
        sys.exit()
    pre = abs(pre - k)

Solution (Python):
import sys
import math

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

pre=abs(n-n//k*k)

while 1:
    if pre <= abs(pre-k):
        print(pre)
        sys.exit()
    pre = abs(pre - k)

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

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

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

Solution (Python):
N, K = map(int,input().split())
if K == 1 or N == 0 or N == K or N%K==0:
    print(0)
elif K == 0:
    print(N)
elif N == 1 or N == K-1 or K == N-1 or N%K==1:
    print(1)
elif N > K:
    res = abs(N - (K*(N//K)))
    if abs(res - K) < res:
        print(abs(res-K))
    else:
        print(res)
elif K > N:
    if abs(K-N) < N:
        print(abs(K-N))
    else:
        print(N)

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

Solution (Python):

import math
def my_round(val, digit=0):
    p = 10 ** digit
    return (val * p * 2 + 1) // 2 / p

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

Solution (Python):
X = list(map(int, input().split()))
N = X[0]
K = X[1]
t = N % K
s = K - t
if t <= s:
  print(t)
else:
  print(s)

Solution (Python):
def MI(): return map(int, input().split())

if __name__ == "__main__":
    N, K = MI()
    print(min(N%K, K-N%K))

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

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

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

if l >= abs(l-k):
    print(abs(l-k))
else:
    print(l)

Solution (Python):
import math
 
N, K = map(int, input().split())
mod = N % K
  
if abs(mod-K) < mod:
  mod = abs(mod-K)
  
print(mod)

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

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

n  = n % k

print(min(n, k - n))

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

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

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

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

Solution (Python):
x, y = map(int, input().split())
tmp = 10**20
k = x // y
x = abs(x-y*k)
while tmp >= x:
    tmp = x
    x = abs(x-y)
print(tmp)

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

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