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

import sys


def solve(N: int, K: int):
    n = N
    k = K
    mi = n//k
    ma = mi+1
    res1 = abs(n - k*mi)
    res2 = abs(n-k*ma)
    print(res1 if res1 < res2 else res2)



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):
import math
l=list(map(int,input().split()))
n,k=l[0],l[1]
while n>k:
  s=n//k
  n=n-(k*s)
if n==0:
  print('0')
else:
  if n<math.fabs(n-k):
    print(int(n))
  else:
    print(int(math.fabs(n-k)))

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

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

Solution (Python):
import math

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

while True:
  if abs(tmp - K) < tmp:
    tmp = abs(tmp - K)
  else:
    break
 	
print(tmp)

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

buf = i[0]%i[1]
if buf > i[1] - buf:
  buf = i[1] - buf
print(buf)

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

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

Solution (Python):
import numpy as np
N, K = list(map(int, input().split(' ')))
x = N
min_x = x
while True:
    x1 = x % K if x >= K else np.abs(x - K)
    if x1 == min_x:
        break

    if x1 < min_x:
        min_x = x1
    
    x = x1
print(min_x)

Solution (Python):


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

r1 = n % k
r2 = abs(k - r1)

ans = min(r1,r2)

print("{}".format(ans))

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

r1 = n % k
r2 = abs(k - r1)

ans = min(r1,r2)

print("{}".format(ans))

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

Solution (Python):



def debug(arg):
    if __debug__:
        pass
    else:
        import sys
        print(arg, file=sys.stderr)

def main():
    N, K = map(int, open(0).read().split())
    debug([N, K])
    r = N % K
    a = min(r, K - r)
    print(a)

if __name__ == "__main__":
    main()

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

Solution (Python):
n,k=map(int,input().split())
if n>=k:
    n%=k
res=10000000000000000000000
for i in range(3):
    res=min(n,res)
    n=abs(n-k)
print(res)

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

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

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

x = N % K
y = abs(x - K)

print(x if x < y else y)

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

mo = n%k

mok = abs(mo - k)

if mo <= mok:
    print(mo)
else:
    print(mok)

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

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

NmodK = N % K

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

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

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

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

Solution (Python):


import sys


















def main():
    mod = 1000000007                  
    inf = float('inf')
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():  return int(input())
    def mi():  return map(int, input().split())
    def mi_0(): return map(lambda x: int(x)-1, input().split())
    def lmi(): return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():  return list(input())
    
    n, k = mi()
    if n <= k:
        print(min(n, k-n))
    else:
        n -= (n // k) * k
        print(min(n, k-n))
    

if __name__ == "__main__":
    main()

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

Solution (Python):

import sys


def solve(N: int, K: int):
    n = N
    k = K
    mi = n//k
    ma = mi+1
    res1 = abs(n - k*mi)
    res2 = abs(n-k*ma)
    print(res1 if res1 < res2 else res2)



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

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

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

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

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

Solution (Python):
N, K = map(int, input().split())
a = N // K
value = abs(N - a * K)
if K == 1:
    print(0)
    exit()
while True:
    if abs(value - K) > abs(value):
        break
    value = abs(value - K)
print(value)

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

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

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

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

mod = N % K


a = abs(mod - K)

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

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

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

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

amari = n % k

if amari == 0:
    ans = 0

else:
    if amari <= k/2:
        ans = amari
    else:
        ans = -(amari - k)

print(ans)

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

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

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

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

x = n%k
y = k-x

print(min([x,y]))

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

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

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

n, k = map(int, sys.stdin.readline().split())

mod = n % k

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

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

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

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())
a = N%K
if a >= K-a:
  print(K-a)
else:
  print(a)

Solution (Python):
import numpy as np
import math as mt
N,K=map(int, input().split())
while(1):
    if abs(N-K)<N:
        a,b=max(N,K),min(N,K)
        N=a-b*(a//b)
    else:
        print(N)
        break

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

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

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

if N > K:
  N = N % K

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

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

if n>k:
  n = n%k

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

print(ans)

Solution (Python):
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    N, K = list(map(int,_in[0].split(' ')))  
    
    x = N % K
    if abs(x-K) < x:
        cnt = abs(x-K)
    else:
        cnt = x
    
    print(cnt)

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

if N % K  == 0:
    print("0")
else:
    val = N % K
    if val < abs(val-K):
        print(val)
    else:
        print(abs(val-K))

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

kpn=n//k

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

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

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

amari = N % K;

hu = -1* (amari - K);

max_N = min(amari,hu);

print(max_N);

Solution (Python):
from collections import deque, Counter as cnt
from collections import defaultdict as dd
from operator import itemgetter as ig
from bisect import bisect_right as bsr
from math import factorial, ceil, floor
import sys
sys.setrecursionlimit(1000000)


args = None
INF = float("inf")
MOD = int(1e9 + 7)


def input(*ps):
    if type(ps[0]) is list:
        return [input(*ps[0][:-1]) for _ in range(ps[0][-1])]
    elif len(ps) == 1:
        return ps[0](next(args))
    else:
        return [p(next(args)) for p in ps]


def nlist(n, v):
    if not n:
        return [] if type(v) is list else v
    return [nlist(n[1:], v) for _ in range(n[0])]




def main():
    N, K = input(int, int)
    ans = [N % K]
    for _ in range(100):
        ans.append(abs(ans[-1] - K))
    print(min(ans))
    return


if __name__ == '__main__':
    args = iter(sys.stdin.read().split())
    main()

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

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

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

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

Solution (Python):
InputIntList = lambda : list(map(int, input().split()))
InputStrList = lambda : list(input().split())

N, K = InputIntList()

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

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

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

Solution (Python):


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

Solution (Python):
from sys import stdin
import numpy as np





temp = stdin.readline().split()
N, K = np.asarray(temp).astype(int)


if K == 1:
    print(0)
    
elif N <= K:
    a = np.abs(N-K)
    if a < N:
        print(a)
    else:
        print(N)
else:
    rem = N%K
    print(np.abs(rem - K))

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

Solution (Python):
from collections import *
from heapq import *
from itertools import *


from decimal import *
import copy
from bisect import *
import time
import sys
import math

input=lambda :sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**7)
def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))

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

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

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

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

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

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

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

num1=N//K
num2=K*(num1+1)-N
num3=N-K*(num1)

print(min(num2,num3))

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

a = []
a.append(N)

if N % K == 0:
    print(0)
else:
    N = N % K
    while True:
        if abs(N - K) not in a:
            a.append(abs(N - K))
            N = abs(N - K)
        else:
            print(min(a))
            break

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

def dif(n, k):
    if abs(n - k) <= n:
        return dif(abs(n-k), k)
    else:
        return n

if N % K == 0:
    print(0)
else:
    N = N % K
    print(dif(N,K))

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



if __name__ == "__main__":
    main()

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

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

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

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

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

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

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

else:
  print(min(n,k-n))

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

Solution (Python):



import sys
input = sys.stdin.readline

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

if __name__ == '__main__':
    main()

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

a = N // K

print(min([N, K, abs(N-K*a), abs(N-K*(a+1))]))

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

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

n -= k * (n//k)

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

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

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

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

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

if n >= k:
    n -= k * (n // k)      

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

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

Solution (Python):
from sys import stdin

n,m=map(int,input().split())
n%=m
while abs(n)>abs(n-m):
    n=abs(n-m)
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())

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

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

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

lst = [N]
while 1:
  n = min(abs(lst[-1]%K),abs(lst[-1]-K))
  if lst.count(n):
    break
  else:
    lst.append(n)
    
print(min(lst))

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

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



points = NK[0]//NK[1]
value = NK[1]*points
v1 = NK[0] - value
v2 = abs(v1-NK[1])




if v1 > v2:
    print(v2)
else:
    print(v1)

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

Solution (Python):
def main():
  n,k = (int(x) for x in input().split())
  n = n % k
  min = n
  while True:
    n = abs(n - k)
    if(n < min):
      min = n
    else:
      break

  print(min)

if __name__ == '__main__':
  main()

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

while True:
    m = abs(n - k)
    if k == 1:
        break
    elif n == 0:
        break
    elif n <= k:
        result = min(n, m)
        break
    else:
        n = n % k

print(result)

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

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

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

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

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

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

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

N -= K * (C)

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

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

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

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

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n,k=map(int,input().split())
    print(min(n%k,k-n%k))

if __name__=='__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n,k=map(int,input().split())
    print(min(n%k,k-n%k))

if __name__=='__main__':
    main()

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

while N>K:
  N = N%K
  
print(min([N,K-N]))

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

Solution (Python):
from sys import stdin
import math


data = stdin.readline().rstrip().split()
N, K = int(data[0]), int(data[1])

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

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

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

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

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

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

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

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

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

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

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

Solution (Python):

import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
from collections import defaultdict
from heapq import heappop, heappush

from decimal import *

























def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007




def main():
	N,K = inputMap()

	N = N%K

	ans = N
	while True:
		tmp = abs(ans-K)

		if tmp < ans:
			ans = tmp
		else:
			break

	print(ans)

def gcd(a, b):
	if a > b:
		a, b = b, a

	while a > 0:
		a, b = b % a, a
	return b





def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res


def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y



def H(n, r, mod=10**9+7):
	
	
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        
        
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

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

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

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

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

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

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

Solution (Python):

import numpy as np

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

if n > k:
    tmp = n % k
    if tmp >= k - tmp:
        ans = k - tmp
    else:
        ans = tmp
elif n < k:
    if n >= k - n:
        ans = k - n
    else:
        ans = n
else:
    ans = 0

print(ans)

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

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

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

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


if __name__ == "__main__":
    main()

Solution (Python):
def solve():
    n,k = map(int,input().split())
    if n>k:
        print(min(n%k,k-n%k))
    else:
        print(min(n,abs(n-k)))
if __name__=='__main__':
    solve()

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

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

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

t = N % K
t2 = K - t

if t > t2:
  print(t2)
else:
  print(t)

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

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

Solution (Python):

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

n = abs(N)
k = abs(K)

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

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

Solution (Python):

DATA = """
1000000000000000000 1
""".split("\n")

def get_debug_line():
	for i in DATA:
		yield i

if 1:
	get_line = input
else:
	get_line = get_debug_line().__next__
	get_line()

N, K = [int(i) for i in get_line().strip().split(" ")]

N %= K
_min = N
while(1):
	if N > K:
		N -= K
	else:
		N = K - N
	
	if _min == N:
		break

	if N < _min:
		_min = N

print (_min)

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

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

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

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

Solution (Python):
import sys
N, M = map(int, input().split())
ans = N%M
while True:
   if abs(ans-M) < ans:
        ans = abs(ans-M)
   else:
    print(ans)
    sys.exit()

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

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

Solution (Python):
n,k=[int(_) for _ in input().split()]
d=n//k
print(min(abs(n-k*d), abs(n-k*(d+1))))

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

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

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

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

if ans1 < ans2:
    print(ans1)
else:
    print(ans2)

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

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

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

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

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

A = N % K
B = K - N % K

print(min(A,B))

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

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

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

if abs(n%k) == 0:
  ans = 0
else:
  while n > k:
    n = abs(n - (n // k) * k)
  ans = min(abs(n-k),abs(abs(n-k)-n),n)

print(ans)

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

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

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

Nmin=K+(N%K)
while Nmin>abs(Nmin-K):
    Nmin=max(Nmin-K,K-Nmin)

print(Nmin)

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

Solution (Python):
from __future__ import print_function

import sys

import re
import array
import copy
import functools
import operator

import math
import string
import fractions
from fractions import Fraction
from fractions import gcd



import collections
import itertools
import bisect

import random
import time

import heapq
from heapq import heappush
from heapq import heappop
from heapq import heappushpop
from heapq import heapify
from heapq import heapreplace
from queue import PriorityQueue as pq
from queue import Queue

from itertools import accumulate

from collections import deque
from collections import Counter

from operator import mul
from functools import reduce

input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return


def lcm(n, m):
    return int(n * m / gcd(n, m))


def coprimize(p, q):
    common = fractions.gcd(p, q)
    return (p // common, q // common)


def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


def find_gcd(list_l):
    x = reduce(gcd, list_l)
    return x


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


if __name__ == '__main__':
    main()

Solution (Python):


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

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

print(ans)

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

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

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

main()

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

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

Solution (Python):


import sys

sys.setrecursionlimit(100000)
INF = float('inf')
MOD = 10**9+7
EPS = 10**-7





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

def f(x):
    ret = x % K
    ret = min(ret, abs(K-ret))
    return ret
    
print(f(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()) 
if N%K ==0:
    ans = 0
elif K%N ==0:
    ans = N
elif K>N:
    ans =min(N,K-N)
elif N>K:
    ans =min(N%K,-(N%K-K))
print(ans)

Solution (Python):

def main():
    N, K = map(int, input().split(' '))
    
    
    prev = N % K
    
    isFinish = False
    
    if prev == 0:
        print(0)
        return
    
    while not isFinish:
        next = abs(prev - K)
        
        if next >= prev:
            isFinish = True
        else:
            prev = next
            
    print(prev)

main()

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

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

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

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

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

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

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

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

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

Solution (Python):



def run():
    x, k = map(int, input().split())
    x = x % k
    while True:
        _x = abs(x - k)
        if x < _x:
            break
        x = _x
    print(x)


if __name__ == '__main__':
    run()

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

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

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

a = 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()))
if N==K:
    print(0)
elif N<K:
    if 2*N<=K:
        print(N)
    else:
        print(K-N)
else:
    if N%K==0:
        print(0)
    else:
        print(min(N%K, K-(N%K)))

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

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

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

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

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

Solution (Python):
def myAnswer(N:int, K:int) -> int:
   mod = N % K
   return min(mod,abs(mod - K))
   

def modelAnswer():
   tmp=1
def main():
   N,K = map(int,input().split())
   print(myAnswer(N,K))

if __name__ == '__main__':
   main()

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

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

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

print(res)

Solution (Python):
from sys import stdin
def main():
    
    readline=stdin.readline
    N,K=map(int,readline().split())
    a=N%K
    b=abs(a-K)
    print(min(a,b))
if __name__=="__main__":
    main()