## coding: UTF-8

from decimal import *

from itertools import permutations, combinations,combinations_with_replacement,product

s = input().split()

t = [int(p) for p in s]



N = t[0]

K = t[1]



#p = int(N/K)

#a = N - p * K

a = Decimal(N % K)

b = Decimal(K - a)

#print('N:{}, K:{}, p:{}, a:{}, b:{}'.format(N,K,p,a,b))

print((min(a,b)))

# 1000000000000000000
## coding: UTF-8

from decimal import *

from itertools import permutations, combinations,combinations_with_replacement,product

s = input().split()

t = [int(p) for p in s]



N = t[0]

K = t[1]



#p = int(N/K)

#a = N - p * K

#a = Decimal(N % K)

#b = Decimal(K - a)

a = N % K

b = K - a



#print('N:{}, K:{}, p:{}, a:{}, b:{}'.format(N,K,p,a,b))

print((min(a,b)))

# 1000000000000000000
## coding: UTF-8
from decimal import *
from itertools import permutations, combinations, combinations_with_replacement, product

s = input().split()
t = [int(p) for p in s]
N = t[0]
K = t[1]
# p = int(N/K)
# a = N - p * K
a = Decimal(N % K)
b = Decimal(K - a)
# print('N:{}, K:{}, p:{}, a:{}, b:{}'.format(N,K,p,a,b))
print((min(a, b)))
# 1000000000000000000

## coding: UTF-8
from decimal import *
from itertools import permutations, combinations, combinations_with_replacement, product

s = input().split()
t = [int(p) for p in s]
N = t[0]
K = t[1]
# p = int(N/K)
# a = N - p * K
# a = Decimal(N % K)
# b = Decimal(K - a)
a = N % K
b = K - a
# print('N:{}, K:{}, p:{}, a:{}, b:{}'.format(N,K,p,a,b))
print((min(a, b)))
# 1000000000000000000

-a = Decimal(N % K) -b = Decimal(K - a) +# a = Decimal(N % K) +# b = Decimal(K - a) +a = N % K +b = K - a