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())
ans=max(N,K)
for i in range(10000):
    N=N%K
    ans=min(ans,N)
    N=abs(N-K)
    ans=min(ans,N)
print(ans)

Solution (Python):






N,K = map(int,input().split())
new = N % K
n2 = K - new

print(min(new,n2))

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

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

Solution (Python):
import numpy as np
n_k = [int(i) for i in input().split()]
n = n_k[0]
k = n_k[1]
r = n % k
r_1 = abs(r - k)
min = np.array([r, r_1]).min()
print(min)

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

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

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

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

res = 0
if n % K > abs((n % K) - K):
    res = abs((n % K) - K)
else:
    res = n % K
    
print (res)

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

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

r=N%K
if r==0:print(0)
elif r<math.fabs(r-K):print(int(r))
else:print(int(math.fabs(r-K)))

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

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

n1 = n % k
n2 = k - n1
print(n1 if n1 < n2 else n2)

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

m = N % K
n = abs(m-K)
print(m if m < n else n)

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

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

print(ans)

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

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

n, k = list(map(int, input().split()))
n1 = n % k
n2 = abs(k-n1)
if (n1 < n2):
  print(n1)
else:
  print(n2)

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

Solution (Python):
import math

inputted = list(map(int, input().split()))
N = inputted[0]
K = inputted[1]

candidate1 = N % K
candidate2 = K - candidate1
answer = min(candidate1, candidate2)

print(answer)

Solution (Python):
n, k = map(int, input().split())
ans = 0
while True:
    if n > k:
        i = n // k + 1
        n = abs(n - k*i)
        continue
    elif n == k:
        ans = 0
        break
    elif n < k:
        if abs(n - k) > n:
            ans = n
            break
        else:
            ans = k - n
            break

print(ans)

Solution (Python):


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

Solution (Python):
def ans(n, k):
    if n <= k:
        return min(n,k-n)
    d = (n-k-1)//k+1
    return ans(n-d*k,k)

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

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

Solution (Python):
n, k = [int(x) for x in input().split(' ')]
d = n // k
ne = n - d * k
if abs(ne - k) < ne:
  print(abs(ne - k))
else:
  print(ne)

Solution (Python):


import sys

N, K = [int(x) for x in sys.stdin.readline().strip().split()]

mod = N % K

if mod < (K / 2.):
    print(mod)
else:
    print(K - mod)

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

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

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

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

rem = n % k

print(min(rem, k - rem))

Solution (Python):
import math

N , K = map(int, input().split())
flag = "F"
if N !=0:
    while 1:
        lenN = int (math.log10(N) + 1)
        lenK = int (math.log10(K) + 1)
        N = abs(N-K*(10**abs(lenN-lenK-1)))
        lenN = int (math.log10(N) + 1)
        if abs(lenN-lenK)<2:
            break



        
while 1:
    
    
    a = N
    N = abs(N - K)
    

    if a<=K and N <= K:
        if a < N:
            result = a
            flag="T"
        else:
            result = N
            flag="T"
    if flag == "T":
        break
print(result)

Solution (Python):
val= input()
val_list=list(map(int,val.split()))
n=val_list[0]
k=val_list[1]
n= n % k
judge=[]
while True:
    n = abs(n-k)
    if n in judge:
        break
    else:
        judge.append(n)
min= judge[0]
for i in range(0,len(judge)):
    if min > judge[i]:
       min= judge[i]

print(min)

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

if(n == k):
    result = 0
elif (n > k):
    result = n % k
    result = min(result, abs(result - k))
else:
    result = min(n, k-n)

print(result)

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

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

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

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

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

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

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

Solution (Python):
import sys, 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, groupby
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
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

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

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

Solution (Python):
n ,k = list(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())
r = N%K
s = K-r
print(min([r,s]))

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

Solution (Python):
inte, sub = map(int, input().split(" "))
inte = inte % sub
t = [inte]
count = 0
while True:
    ab = abs(inte - sub)
    t.append(ab)
    if t[count] <= t[count + 1]:
        print(t[count])
        break
    else:
        count += 1

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

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

remainder = n%k
if remainder > k/2:
    ans = k-remainder
else:
    ans = remainder

print(ans)

Solution (Python):


input_line = input()
inputs = input_line.split(' ')
N, K = [int(s) for s in inputs]

c1 = (N%K)
c2 = abs(c1-K)

print(min(c1,c2))

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

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

def res():
  if N == K:
    return 0
  elif N > K:
    return N % K if K - (N % K) > (N % K) else K - (N % K)
  else :
    return abs(N - K) if abs(N - K) < abs(abs(N-K) - K) else abs(abs(N-K) - K)

print(res())

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

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

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

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

N_1 = N % K
N_2 = K - N_1

print(min(N_1, N_2))

Solution (Python):
n, k = list(map(int, input().split(" ")))
n = n - k * (n // k)
n_last = abs(n - k)
if n_last < n:
    n = n_last
print(n)

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

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

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

Solution (Python):
list_input = [int(n) for n in input().split()]

N = list_input[0]
K = list_input[1]

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

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

Solution (Python):
from sys import stdin
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y) 
N,K=list(map(int,(stdin.readline().strip().split())))
if N%K==0:print(0)
else:
    M=10**19
    while(True):
        M = N
        N=N%K
        if M==N:
            M = N
            N-=K
            if N<=0:
                if abs(N)<abs(M):
                    print(abs(N))
                    break
                else:
                    print(abs(M))
                    break

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

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

x = N-(N//K)*K
print(min(abs(x-K), x))

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

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


z = a % b


if z > b/2:
    print(abs(z-b))
else:
    print(z)

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

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

resolve()

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

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

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

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

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

Solution (Python):
A = list(map(int,input().split()))
mod = A[0]%A[1]
minus = abs(mod-A[1])
if mod < minus:
  print (mod)
else:
  print (minus)

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

x=n%k
xi=abs(x-k)

while x > xi:
    x=xi
    xi=abs(x-k)

print(x)

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

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

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

Solution (Python):

value, deno = map(int,input().split())
if value%deno == 0:
    print(0)
else:
    print(min(value%deno, abs((value%deno) - deno)))

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

if __name__ == "__main__":
    main()

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

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

x = a%b
y = b - x

print(min([x,y]))

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())
r = N%K
if r == 0 or r == 1:
  print(r)
else:
  s = abs(r - K)
  if s < r:
    print(s)
  else:
    print(r)

Solution (Python):



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


tmp_1 = N % K
tmp_2 = abs(tmp_1 - K)
ans = min(tmp_1, tmp_2)


print(ans)

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



def in_int():
    return (int(input()))


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


def in_str():
    s = input()
    return (list(s[:len(s) - 1]))


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


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

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

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

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

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

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

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

print(min(a,b))

Solution (Python):


import sys
INT_MAX = sys.maxsize
INT_MIN = -(sys.maxsize)
sys.setrecursionlimit(10**7)
mod = 1000000007


def IOE():
    sys.stdin = open("input.txt", "r")
    sys.stdout = open("output.txt", "w")


def main():
    n, k = map(int, sys.stdin.readline().split())
    if n % k == 0:
        print(0)
    elif n < k:
        print(min(abs(n - k), n))
    else:
        a = n // k
        f, s = (a + 1) * k, a * k
        print(min(abs(f-n), abs(s-n)))


if __name__ == "__main__":
    try:
        IOE()
    except:
        pass
    main()

Solution (Python):
def Main(N, K):
    if K == 1:
        return 0
    mo = N%K
    if K//2 <= mo:
        return K-mo
    else:
        return mo
         
    

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

if __name__ == '__main__':
    main()

Solution (Python):


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

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

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


minimum = n % k


while n%k > k-n%k:
    minimum = k - n % k 
    n = k - n % k  

print(minimum)

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

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

if k - n < min:
  min = k-n

print(min)

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

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

number = N - K*(N//K)
abs_now  = number 
while all:
    abs_past = abs(abs_now - K)
    if(abs_past >= abs_now):
        break
    abs_now = abs_past
print(abs_now)

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

minimum=N%K
while N%K>K-N%K:
    minimum=K-N%K
    N=K-N%K
print(minimum)

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

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

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

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

while True:
    tmp = N % K
    if tmp >= N:
        if tmp > (abs(tmp - K)):
            N = abs(tmp - K)
        break
    N = tmp
print(N)

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

Solution (Python):
n, m = map(int, input().split())
l = []
ans = 1
if(n%m == 0):
  ans=0
else:
  for x in (0,1000000000000000000):
    n = abs(n%m)
    l.append(n)
  l.sort()
  ans = min(m-n,l[0])
print(ans)

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

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

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

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

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

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

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

Solution (Python):
import math
 
if __name__ == "__main__":
    N, K = map(int, input().split())    
    ret =  N - N // K *K
    if ret > abs(ret-K):
        ret = abs(ret-K)
    print(int(ret))

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

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


a = list(map(lambda x:int(x), input().split()))
n = a[0]%a[1]
k = a[1]

print(min(n, k-n))

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

Solution (Python):
import math
def I(): return(list(map(int,input().split())))
n,m=I()
print(min(n%m,m-n%m))

Solution (Python):
import sys



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


a = get()
b = a[0]
c = a[1]
if b < c:
    if abs(b - c) < b:
        print(abs(b - c))
        sys.exit(0)
    else:
        print(b)
        sys.exit(0)
else:
    if a[0] % a[1] == a[1] or a[0] % a[1] == 0:
        print(0)
        sys.exit(0)
    else:
        if abs((a[0] % a[1]) - a[1]) < abs(((a[0] % a[1]) - a[1]) % a[1]):
            if abs(a[0] % a[1]) < abs((a[0] % a[1]) - a[1]):
                print(abs(a[0] % a[1]))
                sys.exit(0)
            else:
                print(abs((a[0] % a[1]) - a[1]))
                sys.exit(0)
        else:
            if abs(a[0] % a[1]) < abs(((a[0] % a[1]) - a[1]) % a[1]):
                print(abs(a[0] % a[1]))
                sys.exit(0)
            else:
                print(abs(((a[0] % a[1]) - a[1]) % a[1]))
                sys.exit(0)

Solution (Python):
num=input().rstrip().split(" ")
num1=int(num[0])
num2=int(num[1])
if num1%num2==0:
  print(0)
else:
  date0=num1%num2
  date1=num2-date0
  if date0>date1:
    print(date1)
  else:
    print(date0)

Solution (Python):
from sys import stdin
N, K = [int(_) for _ in stdin.readline().rstrip().split()]
mod = N % K
print(min(mod, abs(K - mod)))

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


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

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

N = N % K

if K - N > N:
  print(N)
  exit()
print(K - N)

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

amari = N % K  

if amari > (K-amari):
  answer = K-amari
else:
  answer = amari
  
print(answer)

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

print(min(N%K,cal))

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

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

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

Solution (Python):
if __name__ == '__main__':
    n, k = [int(i) for i in input().split(" ")]
    while abs(n - k) < n and n != 0:
        n = n%k
        if n != 0:
            n = abs(n - k)
    print(n)

Solution (Python):
input_line = input().split(" ")
N,K,sol=int(input_line[0])%int(input_line[1])+int(input_line[1]),int(input_line[1]),10**18+1
for i in range(10):
    if sol>N:
        sol=N
    N=abs(N-K)
print(sol)

Solution (Python):
input_line = input().split(" ")
N,K=int(input_line[0]),int(input_line[1])
sol=10**18+1
N=N%K+K
for i in range(10):
    if sol>N:
        sol=N
    N=abs(N-K)
print(sol)

Solution (Python):
def CalAbs(a, b):
    sol=a-b
    if sol<0:
        sol=sol*(-1)
    return sol
    
input_line = input().split(" ")
N=int(input_line[0])
K=int(input_line[1])

sol=10**18+1
N=N%K+K
i=0
count=0
while i<=10:
    if sol>N:
        sol=N
    N=CalAbs(N,K)
    i=i+1
print(sol)

Solution (Python):

def CalAbs(a, b):
    sol=a-b
    if sol<0:
        sol=sol*(-1)
    return sol
    

def CalDigi(a):    
    digi=1
    flag=0
    while flag==0:
        a=a/10
        if a>=1:
            digi=digi+1
        else:
            flag=1
    return digi
    

input_line = input().split(" ")
N=int(input_line[0])
K=int(input_line[1])

rec=10**18+1


N=N%K+K


i=0
times=100
count=0
while i<=times:
    if rec>N:
        rec=N
    N=CalAbs(N,K)
    if i==times:
     lastN=N
    i=i+1
if rec>N:
    rec=N
    

    

print(rec)

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

N = N % K

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

Solution (Python):
n, m = map(int, input().split())
l = []
ans = 1
if(n%m == 0):
  ans=0
else:
  for x in (0,1000000000000000000):
    n = abs(n%m)
    l.append(n)
  l.sort()
  ans = min(m-n,l[0])
print(ans)

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

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

Solution (Python):
def main():
    args = input().rstrip().split(' ')
    N, K = int(args[0]), int(args[1])
    last_point = N % K
    print(min(last_point, abs(last_point-K)))
    
if __name__ == "__main__":
    main()

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

Solution (Python):
a,b=map(int,input().split())
if a>b:
  s=a%b
  k=b-s
  print(min(s,k))
elif a==b:
  print(0)
else:
  j=b-a
  print(min(a,j))

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

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

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

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

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

Solution (Python):
n,k = map(int,input().split())
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):
a = list(map(int, input().split()))
b = a[0]%a[1]

if b > a[1]/2 :
  print (a[1]-b)
else:
  print (b)

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

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

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


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

amari = n % k
inv_amari = abs(amari - k)
if amari <= inv_amari:
  print(int(amari))
else:
  print(int(inv_amari))

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

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

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

prev,cur=n+1,n
if k<=0:
  print(n)
else:
	rem = n%k
	print(min(rem,abs(rem-k)))

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

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

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

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

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

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

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

Solution (Python):
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n, k = list(map(int, readline().split()))
    n = divmod(n, k)[1]
    print(min(n, abs(n - k)))


if __name__ == '__main__':
    solve()

Solution (Python):
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n, k = list(map(int, readline().split()))
    while n > divmod(n, k)[1]:
        n = divmod(n, k)[1]
    print(min(n, abs(n - k)))


if __name__ == '__main__':
    solve()

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

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

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

print(n)

Solution (Python):

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

Solution (Python):
N, K = map(int, input().split())    
if N % K == 0:                      
    print(0)
else:                               
    N = N % K                       
    if abs(N - K) < N:           
        N = abs(N - K)              
        print(N)
    else:
        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())

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

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

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

Solution (Python):
N, K = map(int, input().split())
if N == K:
    print(0)
else:
    N = N % K
    a, b, c = N, abs(N - K), abs(abs(N - K) - K)
    while not (a == b or a == c or b == c):
        a, b, c = b, c, abs(c - K)
    print(min(a, b, c))

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

Solution (Python):
N, K = (int(x) for x in input().split(' '))
tmp = N % K
if tmp == 0:
  print(0)
elif N > tmp or N > abs(tmp-K):
  print(abs(tmp-K))
else:
  print(N)

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

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

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

N %= K

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

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

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

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

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

Solution (Python):
nk = input()
N = int(nk[:nk.index(" ")])
K = int(nk[nk.index(" ")+1:])
ans = N - K

if ans<0:
	ans*=-1
	if ans>N:
		ans = N
else:
	ans = N - K*(N//K)
	if (ans-K)*-1<ans:
		ans = (ans-K)*-1
print(ans)

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

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

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

print(main())

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

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

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

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

Solution (Python):
buf = input().split()
n = int(buf[0])
k = int(buf[1])

c1 = n % k
c2 = -1 * (c1 - k)

print(min([c1, c2]))

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

print(ans)

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

Solution (Python):
import math
import string
import collections
from collections import Counter
from collections import deque


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


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


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor



dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]



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

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

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


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

Solution (Python):
n,k=map(int,input().split())
x=n%k
while x>=0:
    x-=k
if x<0:
    x*=-1
y=x-k
if y <0:
    y*=-1
if x<y:
    print(x)
if x>y:
    print(y)
if x==y:
    print(x)

Solution (Python):
nk = input().split(' ')
n = int(nk[0])
k = int(nk[1])
n = n % k

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

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

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

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

n = N % K

if n != 0 and N > K:
    n = K % n
else:
    n = min(n, K - n)

print(n)

Solution (Python):

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

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

import numpy as np
N = N % K
past = N

while(True):
    N = np.abs(N - K)
    if N >= past:
        break;
    past = N
print(past)

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

if k >= 2 * n:
    print(n)
    quit()
elif k >= n and k < 2*n:
    print(abs(n-k))
    quit()
else:
    x_b = n%k
    x = abs(x_b - k)
    print(min(x_b,x))

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

if k >= 2 * n:
    print(n)
    quit()
elif k >= n and k < 2*n:
    print(abs(n-k))
    quit()
elif n > k:
    x_b = n%k
    x = abs(x_b - k)
    print(min(x_b,x))

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

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

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

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

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

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

Solution (Python):

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


b=N%K


print(min(b, K-b))

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

d = n//k

n = n - d*k

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

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

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

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

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

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