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):
url = "https://atcoder.jp/contests/abc161/tasks/abc161_c"

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

    anss = []
    ans = 1
    while ans != 0:
        if n > m:
            ans = n % m
            n = ans
        else:
            ans = abs(n - m)
            n = ans
        if ans in anss:
            break
        else:
            anss.append(ans)
    print(min(anss))


if __name__ == '__main__':
    main()

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

Solution (Python):
N,K=map(int, input().split())
stopf = True
wari = N // K
N = N - K*wari
def exchangeK():
    global stopf
    global N
    if N > abs(N-K):
        N = abs(N-K)
    else:
        stopf = False
while stopf:
    exchangeK()
print(N)

Solution (Python):
n,k = map(int,input().split())
if n % k ==0:
    print(0)
else:
    l = [n]
    p = n//k
    c = n-(p*k)
    half = k//2
    while c > half:
        c = abs(c-k)
    l.append(c)
    print(min(l))

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

def substract(min):
    if N % K == 0:
        return 0
    tmp = N % K
    if abs(tmp - K) < tmp:
        return abs(tmp-K)
    return tmp
    
    
    
    
    
    

if abs(N-K) < N or abs(N-K) < K:
    print(substract(abs(N-K)))
elif N < K:
    print(N)
else:
    print(K)

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

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

    if N <= K//2:
        res = N
    else:
        res = abs(K - N)

    print(res)



if __name__ == "__main__":
    main()

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

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

Solution (Python):
import numpy as np
import math
import scipy as sp
import queue
import collections

MOD = 10 ** 9 + 7
INF = 10 ** 9

def main(kwargs):
    n = kwargs["n"]
    k = kwargs["k"]

    res = n - (max(0, (n - 2 * k) // k)) * k

    while True:
        if res <= abs(k - res):
            break
        else:
            res = abs(k - res)

    return res

if __name__ == "__main__":
    kwargs = {}

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

    cout = main(kwargs)
    print(cout)

Solution (Python):
import sys

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

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

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


print(min)

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

if n == k or k == 1:
    print(0)
    exit()

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

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

ans = min(amari,abs(amari-K))


print(ans)

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

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

x = N // K

y = N - x*K

ans_1 = y
ans_2 = abs(y-K)

if ans_1 <= ans_2:
    print(ans_1)
else:
    print(ans_2)

Solution (Python):
N, K = map(int, input().split())
q = N // K
r = N % K

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

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

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

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

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

N=N%K

start = N
ans = 0

def hoge(i, j):
  return abs(i - j)

ans = hoge(N, K)

if start <= ans:
  print(start)
else:
  while ans >= hoge(ans, K):
    ans = hoge(ans, K)
  print(ans)

Solution (Python):
import math
import numpy as np

n,k=(int(x) for x in input().split())
print(min(n%k,k-n%k))

Solution (Python):
n,k=map(int,input('').split(' '))
s=0
h=0

n=n%k
while s==0:
    h=n
    n=n-k
    if n<0:
        n=n*(-1)
    if h>n:
        s=0
    else:
        s=1
        n=h

print(n)

Solution (Python):
N,K=list(map(int,input().split()))
n=N%K
old_n=K+1
while True:
  if old_n<=n:
    print(old_n)
    break
  old_n=n
  n=abs(n-K)

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

N = N % K

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

Solution (Python):
n, k= list(map(int, input().split()))
if k<=n:
    if n%k == 0:
        print("0")
        exit()
amari = n%k
n = amari
while(True):
    new_n = abs(k-n)
    if new_n < n:
        n = new_n
    else :
        break
print(n)

Solution (Python):
n,k = map(int,input().split())
if k ==1:
    print(0)
else:
    ans1 = n//k
    ans2 = ans1+1
    mn = min((n-(ans1*k)),(ans2*k)-n)
    print(mn)

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

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

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

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

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

div, mod = divmod(n, k)




mod_ = abs(mod - k)



if mod < mod_:
    print(mod)
else:
    print(mod_)

Solution (Python):
num = list(map(int, input().split()))
print(min([num[0],num[0]%num[1],abs((num[0]%num[1])-num[1])]))

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

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

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








ans = []
before = abs(N - K)
N = N % K
for i in range(10000):
    ze = abs(N - K)
    ans.append(ze)
    before = ze
    N = ze
print(min(ans))

Solution (Python):

def main():
    n,k = map(int,input().split())
    if n >= k:
        answer = min(n%k,k-(n%k))
    else:
        answer = min(k-n,n)

    print(answer)

if __name__ == '__main__':
    main()

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

if k>=n*2:
  print(n)
elif k>=n:
  print(abs(n-k))
else:
  amari=n%k
  print(min(amari,abs(amari-k)))

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

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

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

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

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

Solution (Python):
a,b = map(int, input().split())
c = b*100
d = b*10000
e = b*1000000
f = b*100000000
g = b*10000000000
h = b*1000000000000
i = b*100000000000000
j = b*10000000000000000
k = b*1000000000000000000

while(a > abs(a-b)):
    if a > k:
        a = abs(a-k)
    elif a > j:
        a = abs(a-j)
    elif a > i:
        a = abs(a - i)
    elif a > h:
        a = abs(a - h)
    elif a > g:
        a = abs(a - g)
    elif a > f:
        a = abs(a - f)
    elif a > e:
        a = abs(a - e)
    elif a > d:
        a = abs(a - d)
    elif a > c:
        a = abs(a - c)
    else:
        a = abs(a-b)

print(a)

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

n = N-(((N-K)+(K-1))//K)*K
print(min(n, abs(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())
H = 0
while True:
  if (N > K):
    H = N%K
  else:
    H = K-N
  if (H>N):
    break
  else:
    N = H
print(N)

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

if n < k:
    if n > k / 2:
        print(k - n)
    else:
        print(n)
else:
    s = math.ceil(n / k)
    ans = abs(n - s  * k)
    while True:
        x = abs(ans - k)
        if x > ans:
            break
        else:
            ans = x
    print(ans)

Solution (Python):
def f(N, K):
    if N < K:
        if K-N >= N:
            return N
        else:
            return f(K-N, K)
    else:
        if N <= N%K:
            return N
        else:
            return f(N%K, K)


def main():
    N, K = [int(x) for x in input().split()]
    print(f(N, K))


if __name__ == '__main__':
    main()

Solution (Python):




def fom(x, k):
    if x <= abs(x - k):
        return x

    return fom(abs(x - k), k)


n, k = map(int, input().split())
m = n % k
print(fom(m, k))

Solution (Python):


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

    def sousa(num):
        before = num
        after = abs(N - K)
        if after >= before:
            return before
        else:
            return sousa(after)

    print(sousa(N))

if __name__ == "__main__":
    main()

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


def main(n, k):
    
    if n % k == 0:
        
        res = 0
    else:
        
        res = min(n % k, abs((n % k) - k), n)

    print(res)


main(N, K)

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

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

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

print(n)

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

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

if 2*n <= k:
  n = n
elif (k<2*n)and(n<=k):
  n = k-n
elif k<n:
  n = n%k
  if 2*n<=k:
    n = n
  else:
    n = k-n
print(n)

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

if n==k:
  print(0)

elif k > n:
  if k-n<=n:
    print(k-n)
  else:
    print(n)
  
else:
  amari = n%k
  com3 = k -amari
  if amari<= com3:
    print(amari)
  else:
    print(com3)

Solution (Python):
N, K = list(map(int, input().split()))
if N > K:
    p = (N - 1)//K
    N_p = N - p*K
    N_q  = -(N_p - K)
    ans = min(N_p, N_q)
else:
    ans = min(N, K-N)
print(ans)

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

if(n%k == 0):
        
    g = n//k
        
    print(n - (g*k))
else:
    g = n//k
    ans = n - (g*k)
    if(abs(ans - k) < ans):
        print(abs(ans - k))
    else:
        print(ans)

Solution (Python):

n, k = map(int, input().split())
if n == k:
    print(0)
else:
    ans = n % k
    while (True):
        if ans < abs(ans - k):
            print(ans)
            exit()
        ans = abs(ans - k)

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

if k == 1 or n == 0:
  print(0)
elif n < k :
  r1 = n
  r2 = n - k
  if r2 < 0:
    r2 *= -1
  print(min(r1,r2))
else:
  r1 = n % k
  r2 = r1 - k
  if r2 < 0:
    r2 *= -1
  print(min(r1,r2))

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

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

if(n==k or n%k==0):
    print(0)
elif(n<k):
    print(min(n,abs(n-k)))
else:
    q=n//k
    ma=n%k

    aa = n-q*k
    print(min(aa,abs(aa-k)))

Solution (Python):
def main():
    n, k = map(int, input().split())
    while True:
        if n % k == 0:
            n = 0
            break
        else:
            if n // k == 0:
                if n > abs(n - k):
                    n = abs(n - k)
                break
            else:
                n = n % k
    print(n)


if __name__ == "__main__":
    main()

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

t = N % K

while t >= abs(t -K):
  t = abs(K - t)
  if t == 0:
    break

print(t)

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

if n >= k:
  d = n//k
  if k*(d+1)-n >= n-k*d:
    n = n-k*d
  else:
    n = k*(d+1)-n
else:
  if n >= k-n:
    n = k-n
print(n)

Solution (Python):
n, k = (int(i) for i in input().split())
if k > n:
    print(min(n, abs(n-k)))
elif n % k == 0:
    print(0)
else:
    q = n % k
    p = (k * (n//k+1)) - n
    print(min(p, q))

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

a, b = N // K, -(-N // K)

def f(t):
    return abs(N - K * t)

print(min(f(a), f(b)))

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

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

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

Solution (Python):


"""
about module
"""


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

    while n * 2 > k:
        n = abs(n - k)
    return n


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

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

Solution (Python):
hoge = input().split()
N = int(hoge[0])
K = int(hoge[1])


while 1:
  if N / K > pow(10,18):
    N = N - (K * pow(10,17))
  elif N / K > pow(10,17):
    N = N - (K * pow(10,16))
  elif N / K > pow(10,16):
    N = N - (K * pow(10,15))
  elif N / K > pow(10,15):
    N = N - (K * pow(10,14))
  elif N / K > pow(10,14):
    N = N - (K * pow(10,13))
  elif N / K > pow(10,13):
    N = N - (K * pow(10,12))
  elif N / K > pow(10,12):
    N = N - (K * pow(10,11))
  elif N / K > pow(10,11):
    N = N - (K * pow(10,10))
  elif N / K > pow(10,10):
    N = N - (K * pow(10,9))
  elif N / K > pow(10,9):
    N = N - (K * pow(10,8))
  elif N / K > pow(10,8):
    N = N - (K * pow(10,7))
  elif N / K > pow(10,7):
    N = N - (K * pow(10,6))
  elif N / K > pow(10,6):
    N = N - (K * pow(10,5))
  elif N / K > pow(10,5):
    N = N - (K * pow(10,4))
  elif N / K > pow(10,4):
    N = N - (K * pow(10,3))
  elif N / K > pow(10,3):
    N = N - (K * pow(10,2))
  else:
    break
  
_min = N
while 1:
  if _min <= abs(_min - K):
    break
  else:
    _min = abs(_min - K)

print(int(_min))

Solution (Python):

from sys import stdin
from operator import itemgetter
import math


N,K = [int(x) for x in stdin.readline().rstrip().split()]
ans1 = N % K
ans2 = (ans1 - K) * -1

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):
def LI():
	return [ int(s) for s in input().split() ]
N,K= LI()

MOD = N % K

print(min(MOD,K-(MOD)))

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

Solution (Python):
N1, K = map(int, input().split()) 
if N1>K:
  N1 = N1%K
N2=N1-K
if N2<0:
  N2=-N2
if N1>N2:
  print(N2)
else:
  print(N1)

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

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

N = nk[0]
x = nk[1]

while N > abs(N-x):
    if N > x:
            N = abs(N-(N//x)*x)
    else:
        N = abs(N-x)

print(N)

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

if n > k:
    n = n % k

while abs(n-k) < n:
        n = abs(n - k)

print(n)

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

Solution (Python):

NS = [int(_) for _ in input().split()]

N = NS[0]
K = NS[1]

def rep(x, K):
    return abs(x-K)

def solve(N, K):
    b = N % K
    if b != 0:
        b = rep(N%K, K)
    
    if b > N:
        b = N

    return b

print(solve(N, K))

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

Solution (Python):

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


if abs(n-k) >= n:
    print(n)
    exit()

if n > k:
    tmp = n%k
    print(min(tmp,abs(tmp-k)))
    exit()

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

if n == 0:
    print(0)


print(abs(n-k))

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

mod = N % K
mod1 = K - mod

if mod > mod1:
    print(mod1)
else:
    print(mod)

Solution (Python):
n,k = map(int,input().split())
if n % k == 0:
    print(0)
else:
    a = [n]
    b = n//k
    c = n-(b*k)
    half = k//2
    while c > half:
        c = abs(c-k)
    a.append(c)
    print(min(a))

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


N_list = [N,K]

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

Solution (Python):
import math


n,k = (int(x) for x in input().split())

b = n % k



c = abs(k-b)

if b < c:
    ans = b
else:
    ans = c

print(ans)

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

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

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

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

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

N = N%K
already = set([N])

while True:
  N = replace(N, K)
  if N in already:
    break
  else:
    already.add(N)

print(min(already))

Solution (Python):
import sys
from math import floor

if __name__ == "__main__":
	N, K = map(int,input().split())

	if K == 1 or (N % K) == 0:
		print(0)
		sys.exit()
	elif N > K:
		N = N % K

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

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

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

Solution (Python):
a, b = map(int, input().split())
c=a%b
d=[a,c,(c-b)*-1]
print(min(d))

Solution (Python):

import sys,os
from collections import defaultdict, deque
from fractions import gcd
from math import ceil, floor
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7

    N,K = LMIIS()
    count = 0
    pre = 10**18+1

    x = N


    x = N % K
    while x >= K //2 and x!= 0:
        x = K % x

    print(x)



    

if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np

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

preN=0

N=N%K

while True:
	preN = N
	N = abs(N-K)

	if preN<N:
		break
print(preN)

Solution (Python):
line = input().split(" ")
leftNum = int(line[0])
rightNum = int(line[1])

output = ""

if leftNum < rightNum:
  output = leftNum

leftNum = leftNum % rightNum  
  
while(1):
  output = abs(leftNum - rightNum)
  if output <= (rightNum / 2):
    break;
  leftNum = output

print(output)

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

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

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

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

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

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

if __name__ == "__main__":
    main()

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

I = N // K
J = N % K
j = J

if J == 0:
  pass
else:
  j = J
  for i in range(I+2):
    J = abs(J - K)
    if J <= j:
      j = J
    else:
      break
      
print(j)

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

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

right=N%K
left=K-N%K


if right>=left:
    print(int(left))
else:
    print(int(right))

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

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

while N > K:
  N = N % K

dis = K - N

if dis >= N:
  min = N
if dis < N:
  min = dis

print(min)

Solution (Python):
N,K = map(int, input().split())
if N==0:
    print(0)
    exit()
if K==1:
    print(0)
    exit()
if K==2:
    if N%2==0:
        print(0)
        exit()
    else:
        print(1)
        exit()

def func(x,K):
    tmp=x-K
    if tmp<0:
        tmp*=(-1)
    return tmp

if (N>K):
    y=int(N/K)
    N=func(N,K*y)

ans = N
while(True):
    N=func(N,K)
    if N<ans:
        ans=N
    else:
        break
print(ans)

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

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

intlist = [ (N % K) ,abs (( N % K ) - K )] 
print (min(intlist))

Solution (Python):
n,k = list(map(int,input().split()))
if n%k==0:
    print(0)
else:
    l = [n]
    p = n//k
    c = n - (k*p)
    half = k//2
    while c > half:
        c = abs(c-k)
    l.append(c)
    print(min(l))

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

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

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

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

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

Solution (Python):
def solution():
    n, m = map(int, input().split())
    
    
    
    if n == 0:
        print(0)
        return
    ans = n % m
    print(min(ans, abs(ans-m)))

solution()

Solution (Python):

"""
Created on Sat Apr  4 21:17:14 2020

@author: liang
"""

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

if N > K :
    N = N%K
ans = N

    
while(min(ans,abs(ans-K)) != ans):
    ans = abs(ans-K)
    

print(ans)

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

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

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

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

if mod == 0:
    print("0")
elif (k - mod) > mod :
    print(mod)
else:
    print(k - mod)

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

m = N//K 
if m == 0:
    a = N
    b = abs(a-K)
    while b < a:
        a = min(a,b)
        b = abs(a-K)
else:
    a = N
    b = abs(a-K*m)
    while b < a:
        a = min(a,b)
        b = abs(a-K)

print(a)

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

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

INF = float('inf')


def abs_score(a, b):
    return abs(a - b)


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

    if N < K:
        sa = abs(N - K)
        print(min(N, sa))
        exit()

    if N % K == 0:
        a = N // K
    else:
        a = N // K + 1

    print(abs(N - a * K))


if __name__ == '__main__':
    main()

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

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

if N//K>=2:
  N=N-(N//K)*K
  
while N>abs(N-K):
  N=abs(N-K)

print(N)

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


def calc(i, j, bf):
    if i == 0:
        return 0

    if i > bf:
        return bf

    return calc(abs(i - j), j, i)


amari = n % k

ans = calc(amari, k, amari)

print(min(ans, n))

Solution (Python):
def get_int_list(inp_str):
    return list(map(int, inp_str.split(" ")))

n, k = get_int_list(input())

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

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




b = a[0] % a[1]
c = abs(b-a[1])
d = abs(b+a[1])
print(min(b,c,d))

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())
a=n//k
n=n-k*a
if n>abs(n-k):
	n=abs(n-k)
print(n)

Solution (Python):
n,k=map(int,input().split())
ans=n
nex=n
w=n//k

nex=n-k*w
if ans>nex:
  ans=nex

while (nex<=k and nex>(k-nex)) and ans!=0:
  nex=k-nex
  if nex<ans:
    ans=nex
  if ans==0:
    break
print(ans)

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

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

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

Solution (Python):
inputLine=input()
N=int(inputLine.split(" ")[0])
K=int(inputLine.split(" ")[1])

x=N%K
result=x
firstX=x

xNext=abs(x - K)
result=min(result, xNext)
x=xNext
while(x != firstX):
    xNext=abs(x - K)
    result = min(result, xNext)
    x=xNext

print(result)

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

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

while(N > diff):
  max_num = max(N, K)
  min_num = min(N, K)
  N = max_num % min_num
  diff = abs(N - K)

print(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())
min = n + 1
if n >= k:
    n = n % k
    
while True:
    if n < min:
        min = n
        n = abs(n - k)
    else:
        break

print(min)

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10


def solve():
    n, k = LI()
    n = n % k
    print(min(n % k, abs(n - k)))
    
    return



if __name__ == '__main__':
    solve()

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

if k > n:
    tmp = n
else:
    tmp = n % k

if tmp > k / 2:
    tmp = k - tmp

print(tmp)

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

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

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

x = n//k
a = k * x
b = a + k

b -= n
a -= n

b = abs(b)
a = abs(a)

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

Solution (Python):
x, k = map(int, input().split())
result = x%k
if x < k :
    if x < (k - x) :
        print(x)
    else :
        print(k - x)
else :
    if result == 0 :
        print(0)
    elif k > result :
        print(abs(k - result))
    else :
        print(result)

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

print(min(ans, ans2))

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

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

Solution (Python):
def main():
    I = [int(_) for _ in input().split()]
    N = I[0]
    K = I[1]

    before = N % K
    after = abs(before - K)
    while before > after:
        before = after
        after = abs(after - K)

    print(before)



if __name__ == "__main__":
    main()

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

flag = 0
if K == 1:
    print(0)
    quit()
if K == 2:
    if N % 2:
        print(1)
        quit()
    else:
        print(0)
        quit()

i = N // K
N = N - K * i
while True:
    i = N // K
    ab = abs(N - K)
    if N > ab:
        N = ab
    else:
        break

print(N)

Solution (Python):
n, k = map(int, input().split())
min_n = n
min_1 = n % k
min_2 = abs(min_1 - k)
min_arr = [min_n, min_1, min_2]
print(sorted(min_arr)[0])

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

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

Solution (Python):
import sys

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


def solve():
    N, K = map(int, rl().split())
    if N % K == 0:
        print(0)
        exit()

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


if __name__ == '__main__':
    solve()

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

N = N % K

if (N == 0):
    print(N)
elif (N == K):
    print(0)
elif (K == 1):
    print(0)
else:
    while (N >= 0):
        if (N > abs(N - K)):
            N = abs(N - K)
        else:
            print(N)
            break

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

Solution (Python):
import collections, math

local = False
if local:
    file = open("inputt.txt", "r")

def inp():
    if local:
        return file.readline().rstrip()
    else:
        return input().rstrip()

def ints():
    return [int(_) for _ in inp().split()]


N, K = ints()

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

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

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

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

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

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

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


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

Solution (Python):
n,k = map(int,input().split())
if n % k ==0:
    print(0)
else:
    l = [n]
    p = n//k
    c = n-(p*k)
    half = k//2
    while c > half:
        c = abs(c-k)
    l.append(c)
    print(min(l))

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

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

ans = r
if r > d:
    ans = d
print(ans)

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

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

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

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

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