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

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

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

x = n
mm = n

x = n%m
if x ==0:
  print('0')
else:

  for i in range(n):
    dif = x-m

    if dif < 0:
      mm = min(mm,abs(abs(dif)-m),abs(dif))
      break
    mm = min(mm,dif)
    x = dif
  print(mm)

Solution (Python):
def zettai(n,k):
    return abs(n-k)
n,k=map(int,input().split())
n=n%k
while(zettai(n,k)<n):
    n=zettai(n,k)
print(n)

Solution (Python):
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
import sys,bisect,string,math,time,functools,random
def Golf():*a,=map(int,open(0))
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[];g=[[] for i in range(n)]
    for i in range(E):
        inp=LI();org_inp.append(inp)
        if index==0:inp[0]-=1;inp[1]-=1
        if len(inp)==2:
            a,b=inp;g[a].append(b)
            if not Directed:g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp;aa=(inp[0],inp[2]);bb=(inp[1],inp[2]);g[a].append(bb)
            if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,boundary=1,search=[],replacement_of_found='.',mp_def={'

    mp=[boundary]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def bit_combination(k,n=2):return [[tb//(n**bt)%n for bt in range(k)] for tb in range(n**k)]
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)
def show2d(g,h,w):
    for i in range(h):show(g[i*w:i*w+w])

YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase

input=lambda: sys.stdin.readline().rstrip()

def ran_input():
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

show_flg=False
show_flg=True

ans=0



x,k=LI()
ans=x%k
ans=min(ans,abs(ans-k),x)
print(ans)

Solution (Python):
x = list(map(int,input().split(" ")))
n = x[0]
k = x[1]
import math
l = abs(n % k)


if l > k//2:
    print(k-l)
else:
    print(l)

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

a = n // k 

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

Solution (Python):
n, k = map(int, input().split())
if n == 0:
    print(n)
else:
    x = n // k
    i = n - k * x
    while True:
        if min(i, abs(i-k)) == i:
            break
        else:
            i = abs(i-k)
    print(i)

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

Solution (Python):
s = input().split()
n = int(s[0])
k = int(s[1])
mod = n % k
abs_dist = abs(mod-k)
if(mod<abs_dist):
  print(mod)
else:
  print(abs_dist)

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

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

N,K=nm()
ans=0
if(N>K):
    ans=min(N%K,K-(N%K))
else:
    ans=min(N,K-N)

print(ans)

Solution (Python):


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

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

print(b)

Solution (Python):
x, K = map(int, input().split())
while True:
    p = x
    if x > K:
        x %= K
    else:
        x = abs(K - x)
    if p <= x:
        print(p)
        break

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

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

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



print(min(a,b))

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

new_N = N % K

while(new_N>(K/2)):
    x = abs(new_N-K)
    new_N = x

print(new_N)

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

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


if N>K:
    N=N % K

while True:
    if abs(N - K) < N:
        N = abs(N - K)
    else:
        print(N)
        exit()

Solution (Python):
n,k=map(int,input().split())
flag=True
tmp=n-n//k*k
cnt=k

while(flag):
    if tmp > abs(tmp-k) :
        tmp=abs(tmp-k)
        cnt+=1
    else:
        flag=False
        print(tmp)

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

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


if n>k:
    large=(n-(k*(n//k-1)))-k
    small=k-(n-k*(n//k))
    
    print(min(large,small))
elif n==k:
    print(0)
else:
    print(min(n,abs(n-k)))

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

amari = N%K

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

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

n %= k

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

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

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

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

Solution (Python):
import math
n,k = map(int,input().split())
x = n % k
list = [x]


while 1:
  x = abs(x - k)
  if(x in list):
    break
  list.append(x)
  
    
print(min(list))

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

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

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

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

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

if N%K==0:
  print(0)

elif K >= 2*N:
  print(N)

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

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

while True:
    b = abs(a-k)
    if a < b:
        print(a)
        break
    else:
        a = b
        continue

Solution (Python):
N,K = map(int, input().split())
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):
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)


inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""


def main():
    N, K = map(int, input().split())
    if N > K:
        a = N % K
        b = abs(a - K)
        c = abs(b - K)
        print(min(b, c))
    else:
        b = abs(N - K)
        c = abs(b - K)
        print(min(b, c))


if __name__ == '__main__':
    main()

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

x = N
d = N // K
x = N - K*d
while abs(x - K) < x:
  x = abs(x - K)
print(x)

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

Solution (Python):
def calc(N, K):
    N = N % K
    d = []
    while True:
        x = abs(N - K)
        if x in d:
            return min(d)
        else:
            d += [x]
            N = x


(N, K) = tuple([int(s) for s in input().split()])
print(calc(N, K))

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

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()]
if n <= k/2:
    print(n)
elif n % k == 0:
    print(0)
else:
    t1=n-(n//k*k)
    t2=abs(t1-k)
    print(min(t1,t2))

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

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

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

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

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

dif = abs(N - K)
if N <= dif:
  print(N)
  exit()

N = N % K
while(True):
  dif = abs(N - K)
  if N <= dif:
    break
  N = dif

print(N)

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

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

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

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

Solution (Python):
x,y= list(map(int, input().split()))
if x%y==0:
    print(0)
else:
    z=min((x%y),(y-(x%y)))
    print(z)

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

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

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

print(min(a,b))

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

Solution (Python):


            
def main():
    n, k = map(int, input().split())
    minv = n
    while True:
        sur = abs(minv - k) % k
        if sur >= minv:
            break
        minv = sur
        
    print(minv)
        
  
if __name__ == '__main__':
    main()

Solution (Python):
n,k = map(int, raw_input().split())
from fractions import gcd

if n % k== 0:
  print 0
else:
  mini = n
  s = set()
  while True:
    if n > k:
      n = n % k
    else:
      n = abs(n-k)
    mini = min(n, mini)
    if n in s:
      print mini
      break
    s.add(n)

Solution (Python):
N, K = map(int, input().split())
def f(x, k):
    d = abs(k-x)
    if d<x:
        return f(d, k)
    else:
        return x
x = N%K
print(f(x, 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())
if n == 0 or n == k:
    print(0)
else:
    a = abs(n % k)
    b = abs(a - k)
    print(min(a, b))

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

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

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

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

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

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

ret = min(res, k - res)
print(ret)

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

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

ans = min(ans1,ans2)


print(ans)

Solution (Python):
N,K=map(int,input().split())
a=N//K
plus=N-a*K
minus=(a+1)*K-N
if plus>=minus:
	print(minus)
else:
	print(plus)

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

Solution (Python):

import collections
import itertools as it
import math
import numpy as np



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





if N >= K:
    seimin = N - (N//K) * K
    print(min(seimin, abs(seimin-K)))
else:
    print(min(N, abs(N-K)))

Solution (Python):
from math import ceil


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

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

    print(ans)


if __name__ == "__main__":
    main()

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

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

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

s = n % k

if s < k - s:
    print(s)
else:
    print(k - s)

Solution (Python):
def main():
    N,K = map(int,input().split())
    
    if N==K:
        ans = 0
    elif N<K:
        ans = min(-N+K,N)
    else:
        ans = N%K
        ans = min(ans, K-ans)

    print(ans)
    return

if __name__ == "__main__":
    main()

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


x = n//k
ans1 = abs(-k*x+n)
ans2 = abs(-k*(x+1)+n)

ans = min(ans1, ans2)

print(ans)

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

r = n % k

ans = 0

if abs(r - k) > r:
    ans = r
else:
    ans = abs(r - k)

print(ans)

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

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

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

res = 1e18
s = set()
while True:
    res = min(res, K-N)
    if not N in s:
        s.add(N)
        N = K-N
    else:
        break

print(res)

Solution (Python):

import sys
N,K = list(map(int, input().split()))
dup = [N]
N = N % K
M = N
while True:
    N = abs(N-K)
    if ( N < M ):
        M = N
    if N in dup:
        break
    else:
        dup.append(N)

print(M)

Solution (Python):


def solve():

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

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


if __name__ == "__main__":
    solve()

Solution (Python):

N,K=map(int,input().split())
a=1
if N>2*K:
    N=2*K+N%K
while a==1:
    if N>abs(N-K):
        N=abs(N-K)
    else:
        a=0
print(N)

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

Solution (Python):
[n,k]=list(map(int,input().split()))
ans1=abs(n-(n//k)*k)
ans2=abs(n-(n//k+1)*k)
ans=min(ans1,ans2)
print(ans)

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

  print(mod)
  
if __name__ == "__main__":
  main()

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

if x > K:
    x -= K * (x//K)

y = abs(x-K)
print(min(x,y))

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

def f(x,k):
    return (abs(x-k) < x)

n = divmod(n,k)[1]

while(f(n,k)):
    n = abs(n-k)

print(n)

Solution (Python):
[N,K]=[int(item)for item in input().split()]
while (True):
    tmp = N - K
    if tmp>0:
        N = N%K
        tmp = N - K
    print(abs(tmp) if abs(tmp)<N else N)
    exit()

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

Solution (Python):
N,K=map(int,input().split())
list_A=[abs(N-K*(N//K)),abs(N-K*(N//K)-K)]
print(min(list_A))

Solution (Python):

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

if n % k == 0:
    print(0)
else:
    n = n - (k*(n // k))
    if n > abs(n - k):
        print(abs(n-k))
    else:
        print(n)

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

val = N%K
while True:
    bef = val
    val = abs(bef-K)
    if abs(val) >= abs(bef):
        break

print(abs(bef))

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

saA = N % K 
saB = K - saA

if saA < saB:
    print(saA)
else:
    print(saB)

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

if n % k == 0:
    ans = 0
else:
    x = n % k
    y = abs(k - x)
    ans = min(x,y)
print(ans)

Solution (Python):
import math

N,K = map(int,input().split())
a = N%K
b = abs(N-(N//K+1)*K)
print(min(a,b))

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

def aaa(a):
    x=n+(-k*(a-1))
    return x
if n<=k:
    print(min(n,abs(n-k)))
else:
    print(min(aaa(n//k + 1),abs(aaa(n//k + 2))))

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

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

n2 = n%k
if n2 > k/2:
    n2 = k - n2

print(n2)

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

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

Solution (Python):
N,K = map(int,open(0).read().split())
x = N % K
x1 = abs(K - x)
print(min(x,x1))

Solution (Python):
line = input().rstrip().split(' ')

n = int(line[0])
k = int(line[1])

t = n % k

ans = t
while True:
    t = t - k
    if t < 0:
        t *= -1

    if ans > t:
        ans = t
    else:
        break

print(ans)

Solution (Python):
n,k=map(int,input().split())
n=int(n)
k=int(k)
n = n % k
minimam = n
record=[]
for i in range(k):
  n=abs(n-k)
  if n < minimam:
    minimam = n
  record.append(n)
  if n in record:
    break
print(minimam)

Solution (Python):
import numpy as np

def main():
    n, k = map(np.int64, input().split(" "))
    
    
    
    
    
    
    b1 = np.mod(n, k)
    b2 = np.mod(n, k) - k
    
    ans = np.min([abs(b1), abs(b2)])
    print(ans)
    
if __name__ == '__main__':
    main()

Solution (Python):
import sys
N, K = map(int, sys.stdin.readline().rstrip().split())
ans = min(N % K, abs(K - N % K))
print(ans)

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

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

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

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

Solution (Python):
from decimal import *
N,K = map(int,input().split())
ans = Decimal(N)%Decimal(K)
if K-ans > ans:
    print(Decimal(ans))
else:
    print(Decimal(K-ans))

Solution (Python):
def c161(n , k):

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

def main():
    n , k = map(int , input().split())
    print(c161(n , k))
if __name__ == '__main__':
    main()

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

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


a = N // K
b = N % K


if b == 0:
    print(0)
    exit()

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

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

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

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

ans = abs(n-k)
prev = 10**18 + 10
end = True
while end:
    ans = min(abs(ans-k), prev)
    if prev == ans:
        end = False
    prev = ans

print(ans)

Solution (Python):
n,k = map(int,input().split())
a = 10**19
if n >= k:
    a=n//k
b=n%k
c=abs(b-k)
l=[n,b,c]
print(min(l))

Solution (Python):


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

n_k_amari = n % k
n_k_1 = k - n_k_amari

print(min(n, n_k_amari, n_k_1))

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

ans = N
if N%K==0 :
  print(0)
else:
  N = N%K
  while True:
      N = abs(N-K)
      if N < ans:
          ans=N
      elif N == ans or ans== 0:
          break
  print(ans)

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

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

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

Solution (Python):
import sys
import math
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
import numpy as np
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix
import itertools
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()

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

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

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

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

if N > K:
    if N % K > K / 2:
        print(K - (N % K))
    else:
        print(N % K)
else:
    if N > K / 2:
        print(K - N)
    else:
        print(N)

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

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

if N >= K:
    N = N - (N // K) * K

minimum = N
previous_value = 1000000000000000001
while True:
    N = abs(N - K)

    if N < minimum:
        minimum = N

    if N >= previous_value:
        break

    previous_value = N

print(minimum)

Solution (Python):

import sys


def solve(N: int, K: int):
    if N > K:
        print(min(N % K,abs(N % K-K)))
    else :
        N = K - N
        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):
n,m = map(int,input().split())
div = n//m
minval = div*m
maxval = (div+1)*m
print(min(abs(minval-n),abs(maxval-n)))

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

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

if n >= k:
  n = n % k

for i in range(10**18):
  if abs(k-n) < n:
    n = abs(k-n)
  else:
    break

print(n)

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

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

mae1 = 0
mae2 = 0
mae3 = 0

def calc(n, k):
    return abs(n-k)

if n > k:
    n = n % k

while(mae1!=n):
    n = calc(n, k)
    mae1 = mae2
    mae2 = mae3
    mae3 = n
    
print(min(mae1, mae2))

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

ans1 = N%K

ans2 = abs(ans1-K)

print(min(ans1,ans2))

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

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

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

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

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

Solution (Python):
a,b = map(int,input().split())
if a > b:
    a = a % b
print("{}".format(min(a,b-a)))

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

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
N, K = [int(x) for x in input().strip().split()]
if N == 0 or K == 1:
    print(0)
    exit()
if N >= K and (N - K) >= K:
    N -= (N - K) // K * K
if N >= K:
    N = abs(N-K)

if N >= (K / 2):
    print(K - N)
    exit()
print(N)

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

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

Solution (Python):





def solv(N, K):
    if N % K == 0:
        return 0
    N = N % K
    return min(N, K-N)


if __name__ == "__main__":

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

    print(ans)

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

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

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

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

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

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

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

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

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

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

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

Solution (Python):



from sys import stdin


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

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


if __name__ == "__main__":
    main()

Solution (Python):
n,k = map(int,input().split())
n = n%k
m =[]
while n not in m:
    m.append(n)
    n = abs(n-k)
print(min(m))

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

min_num = N % K

while True:
    tmp = min_num
    abs_num = abs(tmp-K)

    if abs_num < min_num:
        min_num = abs_num
    else:
        break


print(min_num)

Solution (Python):
import sys
import numpy as np

import fractions
from collections import deque 
from functools import reduce

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

mi = n
mo = n % k

if mi > mo:
    mi = mo
if mi > abs(mo - k):
    mi = abs(mo - k)    
print(mi)

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

a, b = n%k, abs(n%k-k)

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

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

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

nn = n // k
nnn = n - nn * k

base = n
for i in range(10000):
    xxx = abs(nnn - k)
    if xxx < nnn:
        base = xxx
    
    nnn = xxx

print(base)

Solution (Python):
n,k = (int(x) for x in input().split())
a = n//k
n-=k*a
while(True):
    dif = abs(n-k)
    if dif<n:
        n=dif
    else:
        break
print(n)

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

alpha = N // K

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

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

ans, div = divmod(N, K)
div2 = abs(div-K)

print(min(div, div2))

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

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

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

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

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

ans1 = n%k

if ans1 > k-ans1:
  ans1 = k-ans1
print(ans1)

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

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

if n == k:
    print(0)
if n < k:
    if n <= k-n:
        print(n)
    if n > k-n:
        print(k-n)
if n > k:
    n2 = n%k
    print(min(n2,abs(n2-k)))

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

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

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

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

x=n%k

y=abs(x-k)

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

Solution (Python):

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

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

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

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

n = n % k
ans = n

if(ans > abs(n-k)):
    ans = abs(n-k)

print(ans)

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

Solution (Python):
n, k = map(int, input().split())
count = n // k
ans = n - k * count
if abs(ans - k) < ans:
    print(abs(ans - k))
else:
    print(ans)

Solution (Python):
N_K = list(map(int, input().split()))
buf = N_K[0] % N_K[1]
print(buf) if N_K[1]-buf > buf else print(N_K[1] - buf)

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

Solution (Python):
import numpy as np

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

N = N % K

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

Solution (Python):
import sys

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

pre = N
while True:
    if N % K == 0 and N // K >= 1:
        print(0)
        sys.exit()
        
    if N > K:
        N = N % K
    else:
        N = abs(N-K)
    
    if pre <= N:
        print(pre)
        sys.exit()
    else:
        pre = N

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

n = n%k

tmp = n
n = k - n

if tmp > n:
    print(n)
else:
    print(tmp)

Solution (Python):
n,k = (int(i) for i in input().split())
m = n%k
l =abs(n-k)
r = abs(m-k)

print(min(l,r,m))

Solution (Python):
n,k=map(int,input().split())
n-=(n//k)*k
if n==0:
  print(0)
  exit()
mi=n
for i in range(100):
  if abs(n-k)<mi:
    mi=abs(n-k)
  n=abs(n-k)
print(mi)

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

q, mod = divmod(N, K)

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

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


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

Solution (Python):
n,k=map(int,input().split())
while True:
    w = int(n/k)
    c = n
    n = abs(n-(w*k))
    if n==c:
        while True:
            c = n
            n = min(n,abs(n-k)) 
            if n == c:
                break
        break
print(n)

Solution (Python):


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

print(min((N % K), abs((((N // K) + 1) * 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//2:
    print(n)
elif n <= k:
    print(k-n)
else:
    s = n//k
    print(min(n-(k*s), k*(s+1)-n))