Berland Football Cup starts really soon! Commentators from all over the world come to the event.

Organizers have already built $n$ commentary boxes. $m$ regional delegations will come to the Cup. Every delegation should get the same number of the commentary boxes. If any box is left unoccupied then the delegations will be upset. So each box should be occupied by exactly one delegation.

If $n$ is not divisible by $m$, it is impossible to distribute the boxes to the delegations at the moment.

Organizers can build a new commentary box paying $a$ burles and demolish a commentary box paying $b$ burles. They can both build and demolish boxes arbitrary number of times (each time paying a corresponding fee). It is allowed to demolish all the existing boxes.

What is the minimal amount of burles organizers should pay to satisfy all the delegations (i.e. to make the number of the boxes be divisible by $m$)?


-----Input-----

The only line contains four integer numbers $n$, $m$, $a$ and $b$ ($1 \le n, m \le 10^{12}$, $1 \le a, b \le 100$), where $n$ is the initial number of the commentary boxes, $m$ is the number of delegations to come, $a$ is the fee to build a box and $b$ is the fee to demolish a box.


-----Output-----

Output the minimal amount of burles organizers should pay to satisfy all the delegations (i.e. to make the number of the boxes be divisible by $m$). It is allowed that the final number of the boxes is equal to $0$.


-----Examples-----
Input
9 7 3 8

Output
15

Input
2 7 3 7

Output
14

Input
30 6 17 19

Output
0



-----Note-----

In the first example organizers can build $5$ boxes to make the total of $14$ paying $3$ burles for the each of them.

In the second example organizers can demolish $2$ boxes to make the total of $0$ paying $7$ burles for the each of them.

In the third example organizers are already able to distribute all the boxes equally among the delegations, each one get $5$ boxes.

Difficulty: EASY
Tags: Mathematics

Implementation
Expected Time Complexity: O(N)

Starter code:

Solutions:
import sys
import math
get_string = lambda : sys.stdin.readline().strip()
get_int_list = lambda : list(map(int, sys.stdin.readline().strip().split()))
get_int = lambda : int(sys.stdin.readline())
(n, m, a, b) = get_int_list()
if n % m == 0:
	print(0)
else:
	res1 = n % m
	res2 = m - res1
	if res1 * b < res2 * a:
		print(res1 * b)
	else:
		print(res2 * a)


(n, m, a, b) = map(int, input().split())
v = []
if n % m == 0:
	print(0)
else:
	re = n % m
	print(min(re * b, (m - re) * a))


(n, m, a, b) = map(int, input().split())
t1 = n % m
t2 = m - t1
t1 *= b
t2 *= a
print(min(t1, t2))


(n, m, a, b) = [int(i) for i in input().split()]
try:
	down = n % m
	up = m - down
	print(min(up * a, down * b))
except:
	print(0)


def main():
	(n, m, a, b) = map(int, input().split())
	(q, r) = divmod(n, m)
	if r == 0:
		ans = 0
	else:
		ans = min(r * b, (m - r) * a)
	print(ans)
main()


import math
getInputList = lambda : list(input().split())
getInputIntList = lambda : list(map(int, input().split()))
(a, b, c, d) = getInputIntList()
m = a % b
print(min(m * d, (b * math.ceil(a / b) - a) * c))


(n, m, a, b) = map(int, input().split())
ans = 0
if n % m == 0:
	pass
elif n < m:
	ans = min(b * n, a * (m - n))
else:
	ans = min(b * (n % m), a * (m - n % m))
print(ans)


(n, m, a, b) = map(int, input().split())
moded = n % m
if moded == 0:
	print('0')
	exit(0)
if moded * b < (m - moded) * a:
	print(moded * b)
else:
	print((m - moded) * a)


(n, m, a, b) = [int(i) for i in input().split(' ')]
create_number = m - n % m
delete_number = n % m
print(min(a * create_number, b * delete_number))


read_numbers = lambda : map(int, input().split())
INF = 1 << 64
(n, m, a, b) = read_numbers()
r = n % m
print(min(r * b, (m - r) * a))


(n, m, a, b) = [int(_) for _ in input().strip().split()]
re = n % m
if re == 0:
	print(0)
else:
	print(min((m - re) * a, re * b))


(boxes, teams, build, demol) = map(int, input().split())
low = teams * (boxes // teams)
print(min((boxes - low) * demol, (low + teams - boxes) * build))


(n, m, a, b) = map(int, input().split())
reminder = n % m
remove_cost = reminder * b
add_cost = (m - reminder) * a
if remove_cost < add_cost:
	print(remove_cost)
else:
	print(add_cost)


(n, m, a, b) = map(int, input().split())
print(min(a * (m - n % m), n % m * b))


(n, m, a, b) = input().split(' ')
n = int(n)
m = int(m)
a = int(a)
b = int(b)
remainder = n % m
costdestroy = remainder * b
numberToBuild = m - remainder
costToBuild = numberToBuild * a
if costdestroy < costToBuild:
	print(costdestroy)
else:
	print(costToBuild)


(n, m, a, b) = [int(x) for x in input().split()]
if n > m:
	if n % m == 0:
		print(0)
	else:
		t1 = n % m
		print(min(t1 * b, (m - t1) * a))
elif n == m:
	print(0)
else:
	print(min(n * b, (m - n) * a))


import math
(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
else:
	temp = math.ceil(n / m)
	x = (temp - 1) * m
	y = temp * m
	min1 = 10 ** 18
	min2 = 10 ** 18
	min1 = (n - x) * b
	min2 = (y - n) * a
	print(min(min1, min2))


(n, m, a, b) = map(int, input().split())
destroy = n % m
if destroy == 0:
	print(0)
else:
	build = (n // m + 1) * m - n
	(s1, s2) = (destroy * b, build * a)
	print(min(s1, s2))


def main():
	(n, m, a, b) = input().split(' ')
	(n, m, a, b) = [int(n), int(m), int(a), int(b)]
	if n % m == 0:
		print(0)
		return
	else:
		r = m - n % m
		l = n % m
		if l * b > r * a:
			print(r * a)
		else:
			print(l * b)
main()


import time
(n, m, a, b) = (int(i) for i in input().split())
start = time.time()
S = n % m
V = m - S
ans = min(a * V, b * S)
print(ans)
finish = time.time()


(n1, m1, a1, b2) = input().split()
n = int(n1)
m = int(m1)
a = int(a1)
b = int(b2)
su = 0
if n > m:
	r1 = n % m * b
	r2 = ((n // m + 1) * m - n) * a
	print(min(r1, r2))
else:
	r1 = n * b
	r2 = (m - n) * a
	print(min(r1, r2))


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
else:
	x = n % m
	y = m * (n // m + 1) - n
	print(min(x * b, y * a))


(n, m, a, b) = [int(x) for x in input().split()]
print(min((n - n // m * m) * b, ((n // m + 1) * m - n) * a))


(N, M, A, B) = map(int, input().split())
D = N // M * M
res = (N - D) * B
U = (N + M - 1) // M * M
res = min(res, (U - N) * A)
print(res)


(n, m, a, b) = map(int, input().strip().split())
r = n % m
if r == 0:
	print('0')
	exit(0)
s = m - r
res = min(a * s, b * r)
print(res)


(n, m, a, b) = map(int, input().split())
x = (n // m + 1) * m - n
y = n % m
x = x * a
y = y * b
print(min(x, y))


(a, b, c, d) = input().split()
a = int(a)
b = int(b)
c = int(c)
d = int(d)
o1 = a % b
o2 = b - o1
print(min(o1 * d, o2 * c))


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
	exit(0)
else:
	dostroit = (n // m + 1) * m - n
	snesti = n - n // m * m
	print(min(dostroit * a, snesti * b))


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print('0')
else:
	k = n % m * b
	j = ((n // m + 1) * m - n) * a
	if k < j:
		print(k)
	else:
		print(j)


(n, m, a, b) = map(int, input().split(' '))
if n >= m:
	dem = b * (n % m)
	build = a * ((n // m + 1) * m - n)
else:
	dem = b * n
	build = a * (m - n)
print(min(build, dem))


from math import *
a = input().split()
sub_d = fmod(int(a[0]), int(a[1])) * int(a[3])
sub_c = (int(a[1]) - fmod(int(a[0]), int(a[1]))) * int(a[2])
print(int(min(sub_d, sub_c)))


(n, m, a, b) = list(map(int, input().split()))
if n % m == 0:
	print(0)
else:
	d = n % m * b
	e = ((n // m + 1) * m - n) * a
	f = n * b
	print(min(d, f, e))


from math import ceil
(n, m, a, b) = map(int, input().split())
print(min((ceil(n / m) * m - n) * a, (n - n // m * m) * b))


(n, m, a, b) = map(int, input().split())
p = n % m * b
q = (m - n % m) * a
print(min(p, q))


def main():
	(N, M, A, B) = map(int, input().split())
	build = ((N // M + 1) * M - N) * A
	demolish = N % M * B
	print(min(build, demolish))
main()


(n, m, a, b) = map(int, input().split())
d = n % m
v = (n // m + 1) * m - n
print(min(b * d, v * a))


(n, m, a, b) = map(int, input().split())
if not n % m:
	print(0)
else:
	k = n - n % m
	k1 = n + m - n % m
	p = (k1 - n) * a
	p1 = (n - k) * b
	if p >= p1:
		print(p1)
	else:
		print(p)


import collections
from functools import cmp_to_key
import sys

def getIntList():
	return list(map(int, input().split()))
(n, m, a, b) = getIntList()
z = n % m
r = min((m - z) * a, z * b)
print(r)


__file__ = __file__.startswith('/home/a_farazei')

def input_():
	global __file__
	if not __file__:
		return input()
	if not hasattr(__file__, 'readline'):
		__file__ = open('input.txt', 'r')
	return __file__.readline()
(n, m, a, b) = map(int, input_().split())
q = n % m
print(min(q * b, (m - q) * a))


(n, m, a, b) = map(int, input().split())
q = n % m
r = n // m
if q == 0:
	print(0)
elif n > m:
	print(min(q * b, ((r + 1) * m - n) * a))
else:
	print(min(n * b, (m - n) * a))


(n, m, a, b) = map(int, input().split())
rem = n % m
val = (n // m + 1) * m
print(min(rem * b, (val - n) * a))


(a, b, c, d) = map(int, input().split())
t = a // b
k = min((b - a % b) * c, a % b * d)
print(k)


(n, m, a, b) = [int(x) for x in input().split()]
bx = n % m
p1 = bx * b
p2 = (m - bx) * a
print(min(p1, p2))


(a, b, c, d) = map(int, input().split())
if a % b == 0:
	print(0)
elif a % b * d < (b - a % b) * c:
	print(a % b * d)
else:
	print((b - a % b) * c)


(n, m, a, b) = map(int, input().split())
if n % m != 0:
	c = (m - n % m) * a
else:
	c = 0
b = n % m * b
print(min(c, b))
1000000000000
750000000001


(n, m, a, b) = map(int, input().split())
if not n % m:
	print(0)
else:
	print(min(n % m * b, ((n // m + 1) * m - n) * a))


(n, m, a, b) = list(map(int, input().split()))
l = []
todestroy = n % m * b
tocreate = ((n + m) // m * m - n) * a
l.append(todestroy)
l.append(tocreate)
print(min(l))


(n, m, a, b) = map(int, input().split())
cost = 0
if n % m == 0:
	print(cost)
else:
	k = n % m * b
	f = (m - n % m) * a
	cost = min(k, f)
	print(cost)


(n, m, a, b) = map(int, input().split())
q = n // m
print(min(b * (n - q * m), a * ((q + 1) * m - n)))


(n, m, a, b) = map(int, input().split())
ans = None
if n <= m:
	ans = min(a * (m - n), b * n)
else:
	ans = min(n % m * b, (n - n % m + m - n) * a)
print(ans)


(n, m, a, b) = list(map(int, input().split()))
x = n // m
if ((x + 1) * m - n) * a > (n - x * m) * b:
	print((n - x * m) * b)
else:
	print(((x + 1) * m - n) * a)


(n, m, a, b) = map(int, input().split())
rem = n % m
arem = m - rem
if arem * a > rem * b:
	print(rem * b)
else:
	print(arem * a)


(n, m, a, b) = [int(x) for x in input().split()]
if n % m == 0:
	print('0')
else:
	lowerLim = int(n / m) * m
	upperLim = lowerLim + m
	print(min((n - lowerLim) * b, (upperLim - n) * a))


(n, m, a, b) = [int(i) for i in input().split()]
k = n % m
x = k * b
y = (m - k) * a
if x > y:
	print(y)
else:
	print(x)


args = [int(n) for n in input().split()]
rem = args[0] % args[1]
if rem == 0:
	print(0)
else:
	build_cost = args[2] * (args[1] - rem)
	demo_cost = args[3] * rem
	if build_cost <= demo_cost:
		print(build_cost)
	else:
		print(demo_cost)


(n, m, a, b) = map(int, input().split())
costnew = (m - n % m) * a
costdemolish = n % m * b
print(min(costdemolish, costnew))


(n, m, a, b) = map(int, input().split())
import math
if n % m == 0:
	print(0)
elif m > n:
	des = n * b
	build = (m - n) * a
	if build <= des:
		print(build)
	else:
		print(des)
else:
	temp = math.floor(n / m)
	build = ((temp + 1) * m - n) * a
	des = (n - temp * m) * b
	if build <= des:
		print(build)
	else:
		print(des)


def main():
	(n, m, a, b) = input().split(' ')
	n = int(n)
	m = int(m)
	a = int(a)
	b = int(b)
	mod = n % m
	div = n // m
	if mod != 0:
		if n > m:
			plus = ((div + 1) * m - n) * a
			minus = (n - div * m) * b
			ans = min(plus, minus)
		elif n < m:
			plus = (m - n) * a
			minus = n * b
			ans = min(plus, minus)
	else:
		ans = 0
	print(ans)
main()


import sys
(n, m, a, b) = map(int, input().split())
distroy = n % m
build = (n // m + 1) * m - n
ans = 0
if a * build < b * distroy:
	print(a * build)
else:
	print(b * distroy)


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
else:
	x = n // m
	y = n % m
	d = y * b
	v = (m - y) * a
	print(min(d, v))


(n, m, a, b) = map(int, input().strip().split())
if n % m == 0:
	exit(print(0))
if n < m:
	exit(print(min((m - n) * a, n * b)))
upper_limit = (n // m + 1) * m
lower_limit = n - n % m
print(min((upper_limit - n) * a, (n - lower_limit) * b))


(n, m, a, b) = map(int, input().split())
an = n // m
li = [an * m - m, an * m, an * m + m]
print(min((n - li[0]) * b, (n - li[1]) * b, (li[2] - n) * a))


(n, m, a, b) = map(int, input().strip().split())
if n == m:
	print(0)
elif n < m:
	if (m - n) * a < n * b:
		print((m - n) * a)
	else:
		print(n * b)
elif n > m:
	x = n % m * b
	y = (m - n % m) * a
	print(min(x, y))


(a, b, c, d) = list(map(int, input().split()))
print(min(a % b * d, -(a - (a // b + 1) * b) * c))


t = list(map(int, input().split()))
if t[0] % t[1] == 0:
	print(0)
else:
	p = t[0] % t[1]
	print(min(p * t[3], (t[1] - p) * t[2]))


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
else:
	c = n // m
	c1 = (c + 1) * m - n
	c2 = n - c * m
	if c2 <= 0:
		print(c1 * a)
	elif c1 * a > c2 * b:
		print(c2 * b)
	else:
		print(c1 * a)


(n, m, a, b) = map(int, input().split())
print([0, min(n % m * b, abs(m - n % m) * a)][n % m != 0])


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
else:
	left = n % m
	build = (m - left) * a
	demo = left * b
	print(min(build, demo))


(n, m, a, b) = [int(x) for x in input().split()]
mod = n % m
if mod == 0:
	print(0)
else:
	mul = m - mod
	print(min(mul * a, mod * b))


(n, m, a, b) = [int(s) for s in input().split()]
bul = (m - n % m) * a
des = n % m * b
if n % m == 0:
	print(0)
elif bul > des:
	print(des)
else:
	print(bul)


(n, m, a, b) = map(int, input().split())
d = n % m
u = m - d
print(min(u * a, d * b))


(n, m, a, b) = tuple(map(int, input().split(' ')))
if n % m == 0:
	print(0)
else:
	temp = n % m
	print(min(temp * b, (m - temp) * a))


(n, m, a, b) = input().split(' ')
n = int(n)
m = int(m)
a = int(a)
b = int(b)
mod = n % m
print(min(mod * b, (m - mod) * a))


def main():
	(n, m, a, b) = [int(x) for x in input().split()]
	rem = n % m
	ans = min(rem * b, (m - rem) * a)
	print(ans)
main()


def read():
	return int(input())

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

def readmap():
	return map(int, input().split())
(n, m, a, b) = readmap()
if n % m == 0:
	print(0)
	quit()
build = m * (n // m + 1) - n
demolish = n % m
print(min(build * a, demolish * b))


TN = 1

def solution():
	(n, m, a, b) = map(int, input().split())
	if n % m == 0:
		ans = 0
	else:
		x = n % m * b
		y = (m - n % m) * a
		ans = min(x, y)
	print(ans)
while TN != 0:
	solution()
	TN -= 1


(n, m, a, b) = map(int, input().split())
f = 0
if n % m == 0:
	print(0)
	f = 1
if not f:
	bl = n + (m - n % m)
	bl = bl - n
	d = n - n % m
	d = n - d
	print(min(bl * a, d * b))


(n, m, a, b) = list(map(int, input().split()))
k = n % m
print(min(k * b, (m - k) * a))


(n, m, a, b) = map(int, input().split())
ost = n % m
cnt = n // m
print(min(ost * b, (m - ost) * a))


str1 = list(map(int, input().split()))
n = str1[0]
m = str1[1]
a = str1[2]
b = str1[3]
rem = n % m
print(min(rem * b, (m - rem) * a))


x = list(map(int, input().split()))
n = x[0]
m = x[1]
a = x[2]
b = x[3]
rem = n % m
orem = m - rem
print(min(b * rem, a * orem))


(n, m, a, b) = str(input()).split(' ')
(n, m, a, b) = (int(n), int(m), int(a), int(b))
if n % m == 0:
	print(0)
else:
	con = (m - n % m) * a
	dem = n % m * b
	if con < dem:
		print(con)
	else:
		print(dem)


(n, m, a, b) = input().split()
(n, m, a, b) = [int(n), int(m), int(a), int(b)]
if n % m == 0:
	print(0)
else:
	if n > m:
		x = (n // m + 1) * m - n
	elif m > n:
		x = m - n
	if n % m * b >= x * a:
		print(x * a)
	else:
		print(n % m * b)


x = input()
arr = [int(i) for i in x.split()]
n = arr[0]
m = arr[1]
a = arr[2]
b = arr[3]
if n % m == 0:
	print('0')
else:
	rem = n % m
	need_more = m - rem
	if need_more * a < rem * b:
		print(need_more * a)
	else:
		print(rem * b)


(n, m, a, b) = [int(v) for v in input().split()]
down = n // m * m
up = (n + m - 1) // m * m
cost_down = (n - down) * b
cost_up = (up - n) * a
print(min(cost_down, cost_up))


(n, m, a, b) = map(int, input().split())
nBuild = m - n % m
nDem = n % m
print(min(nBuild * a, nDem * b))


(n, m, a, b) = [int(x) for x in input().split()]
cost = 0
cc = cd = cud = 0
if n % m == 0:
	cost = 0
else:
	z = n % m
	if n > m:
		cc = (m - z) * a
		cd = z * b
		cud = n * b
		t = min(cc, cd)
		cost = min(t, cud)
	if n < m:
		cud = n * b
		cc = (m - n) * a
		cost = min(cc, cud)
print(cost)


a = list(map(int, input().split()))
(n, m, a, b) = (a[0], a[1], a[2], a[3])
if n % m == 0:
	print(0)
else:
	a = min((n - n // m * m) * b, ((n // m + 1) * m - n) * a, n * b)
	print(a)


(n, m, a, b) = map(int, input().split())
n = n % m
if n == 0:
	print(0)
else:
	mx1 = (m - n) * a
	mx2 = n * b
	print(min(mx1, mx2))


(n, m, a, b) = [int(x) for x in input().strip().split(' ')]
d = n % m
e = (m - d) % m
print(min(d * b, e * a))


(n, m, a, b) = map(int, input().split())
c = 0
if n % m == 0:
	print(0)
else:
	k = (n + m) // m
	c = k * m - n
	k = n % m
	print(min(c * a, k * b))


n = [int(i) for i in input().split()]
print(min(n[0] % n[1] * n[3], (n[1] - n[0] % n[1]) * n[2]))


import sys

def main():
	(n, m, a, b) = map(int, sys.stdin.readline().split())
	top = n % m
	bot = m - top
	if top == 0:
		print(0)
	else:
		print(min(bot * a, b * top))
main()


(a, b, c, d) = map(int, input().split())
k = l = 0
if a - a // b == 0:
	print(0)
elif b > a:
	k = abs((b - a) * c)
	l = abs(a * d)
	if k < l:
		print(k)
	else:
		print(l)
elif a > b:
	k = (b - a % b) * c
	l = a % b * d
	if k < l:
		print(k)
	else:
		print(l)
else:
	print(0)


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

def getTransIntList(n):
	first = getIntList()
	m = len(first)
	result = [[0] * n for _ in range(m)]
	for i in range(m):
		result[i][0] = first[i]
	for j in range(1, n):
		curr = getIntList()
		for i in range(m):
			result[i][j] = curr[i]
	return result
(n, m, a, b) = getIntList()
rest = n % m
print(min(rest * b, (m - rest) * a))


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
else:
	mod = n % m
	print(min(b * mod, a * (m - mod)))


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


(n, m, a, b) = list(map(int, input().split(' ')))
if n % m != 0:
	if n > m:
		diff = n % m
		dec = diff * b
		inc = (m - diff) * a
		print(min(inc, dec))
	else:
		diff = n % m
		inc = (m - diff) * a
		dec = diff * b
		print(min(inc, dec))
else:
	print(0)


(x, y, a, b) = [int(x) for x in input().split()]
f = x % y * b
s = (y - x % y) * a
if f < s:
	print(f)
else:
	print(s)


(n, m, a, b) = list(map(int, input().split()))
if n % m == 0:
	print(0)
else:
	res1 = n % m * b
	res2 = (m - n % m) * a
	print(min(res1, res2))


(n, m, a, b) = map(int, input().split())
r = n % m
t = r * b
p = (m - r) * a
print(min(t, p))


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
else:
	p = m * (n // m)
	c1 = b * (n - p)
	p1 = m * (n // m + 1)
	c2 = a * (p1 - n)
	print(min(c1, c2))


data = input().split()
data = [int(i) for i in data]
mod = data[0] % data[1]
print(min(mod * data[3], (data[1] - mod) * data[2]))


Input = lambda : map(int, input().split())
a = list(Input())
if a[0] > a[1]:
	Cost1 = a[3] * (a[0] % a[1])
	Cost2 = ((a[0] // a[1] + 1) * a[1] - a[0]) * a[2]
	print(min(Cost1, Cost2))
elif a[0] < a[1]:
	Cost1 = (a[1] - a[0]) * a[2]
	Cost2 = a[0] * a[3]
	print(min(Cost1, Cost2))
else:
	print(0)


(n, m, a, b) = map(int, input().split())
sum = 0
extra = n % m
need = m - n % m
print(min(extra * b, need * a))


(n, m, a, b) = list(map(int, input().split()))

def solve(n, m, a, b):
	if m == 0:
		return n * b
	if n == 0:
		return m * a
	if n % m == 0:
		return 0
	to_destroy = n % m
	to_build = m - to_destroy
	return min(to_build * a, to_destroy * b)
print(solve(n, m, a, b))


def readInts():
	return [int(x) for x in input().split(' ')]
array = readInts()
d = array[0] % array[1]
delet = d * array[3]
create = (array[1] - d) * array[2]
if d != 0:
	if delet <= create:
		print(delet)
	else:
		print(create)
else:
	print(d)


from sys import stdin, stdout
(n, m, a, b) = map(int, stdin.readline().split())
ans = float('inf')
if not n % m:
	stdout.write('0')
else:
	mod = n % m
	ans = min(ans, mod * b)
	ans = min(ans, (m - mod) * a)
	stdout.write(str(ans))


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
else:
	r1 = n % m
	r2 = (n + m - 1) // m * m - n
	ans = min(r1 * b, r2 * a)
	print(ans)


(n, m, a, b) = map(int, input().split())
ans = 0
k = n - m * (n // m)
print(min(b * k, a * (m - k)))


(n, m, a, b) = map(int, input().split())
(q, r) = divmod(n, m)
if not r:
	print(0)
else:
	x = r * b
	y = ((q + 1) * m - n) * a
	print(x) if x < y else print(y)


from math import ceil
X = list(map(int, input().split()))
print(min((X[0] - X[0] // X[1] * X[1]) * X[3], (ceil(X[0] / X[1]) * X[1] - X[0]) * X[2]) if X[0] / X[1] != 0 else 0)


inp = input()
(n, m, a, b) = inp.split()
(n, m, a, b) = (int(n), int(m), int(a), int(b))
r = m - n % m
print(min(a * r, b * (n % m)))


(n, m, a, b) = [int(s) for s in input().split()]
a = (m - n % m) % m * a
b = n % m * b
print(min(a, b))


from sys import stdin, stdout
lst = list(map(int, stdin.readline().strip().split()))
rem = lst[0] % lst[1]
if rem == 0:
	stdout.write('0')
else:
	cons_cost = (lst[1] - rem) * lst[2]
	dest_cost = rem * lst[3]
	stdout.write(str(min(cons_cost, dest_cost)))


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
else:
	diff = n % m
	below = n - diff
	goto = n + (m - diff)
	if (goto - n) * a < (n - below) * b:
		print((goto - n) * a)
	else:
		print((n - below) * b)


import math
(n, m, a, b) = map(int, input().split())
x = (n - m * math.floor(n / m)) * b
y = (m * math.ceil(n / m) - n) * a
if n % m == 0:
	print(0)
elif x < y:
	print(x)
else:
	print(y)


(n, m, a, b) = map(int, input().split())
ost = n % m
if ost != 0:
	ans = ost * b if ost * b < (m - ost) * a else (m - ost) * a
	print(ans)
else:
	print(0)


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

def main():
	(n, m, a, b) = readline()
	remove = n % m * b
	add = (m - n % m) * a
	print(min(add, remove))

def __starting_point():
	main()
__starting_point()


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
elif n < m:
	print(min(n * b, (m - n) * a))
else:
	d = m * (n // m + 1)
	e = n % m
	print(min(e * b, (d - n) * a))


(n, m, a, b) = [int(i) for i in input().split()]
ost = n % m
if ost == 0:
	print(0)
else:
	f = b * ost
	s = a * (m - ost)
	if f > s:
		print(s)
	else:
		print(f)


(n, m, a, b) = map(int, input().split())
price_a = (m - n % m) * a
price_b = n % m * b
print(min(price_a, price_b))


from sys import stdin, stdout
import bisect

def ii():
	return int(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def li():
	return list(mi())

def si():
	return stdin.readline()
(n, m, a, b) = mi()
x = n % m
print(min(b * x, a * (m - x)))


import math
import os
import random
import re
from sys import stdin, stdout
from collections import Counter
from itertools import permutations

def si():
	return str(stdin.readline())

def ii():
	return int(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def li():
	return list(mi())

def debug(x):
	return stdout.write(str(x))
(n, m, a, b) = map(int, input().split())
print(min(n % m * b, (m - n % m) * a))


def main():
	(n, m, a, b) = list(map(int, input().split()))
	lower = n // m * m
	upper = lower + m
	rlower = (n - lower) * b
	rupper = (upper - n) * a
	print(min(rlower, rupper))
main()


(stfn, stfm, stfa, stfb) = input().split()
(n, m, a, b) = (int(stfn), int(stfm), int(stfa), int(stfb))
mod = n % m
if mod == 0:
	print(0)
else:
	togrow = m - mod
	tolose = mod
	gr = togrow * a
	ls = tolose * b
	print(min(gr, ls))


(n, m, a, b) = [int(i) for i in input().split()]
if n % m == 0:
	print(0)
else:
	r = n % m
	rr = m - r
	if r * b < rr * a:
		print(r * b)
	else:
		print(rr * a)


(n, m, a, b) = map(int, input().split(' '))
if n > m and n % m != 0:
	c = n % m
	dd = n // m
	d = abs(n - (dd + 1) * m)
	c1 = c * b
	d1 = d * a
	if c1 > d1:
		print(d1)
	else:
		print(c1)
elif n < m:
	c = abs(n - m)
	c1 = a * c
	d1 = n * b
	if c1 > d1:
		print(d1)
	else:
		print(c1)
else:
	print(0)


import math
(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
elif n < m:
	x = (m - n) * a
	y = n * b
	print(min(x, y))
elif n > m:
	x = math.floor(n / m) * m
	y = math.ceil(n / m) * m
	c = (n - x) * b
	d = (y - n) * a
	print(min(c, d))


(n, m, a, b) = map(int, input().split())
rem = n % m
build = (m - rem) * a
destroy = rem * b
print(build if build < destroy else destroy)


numbers = list(map(int, input().split()))
n = numbers[0]
m = numbers[1]
a = numbers[2]
b = numbers[3]
cal = min((m - n % m) * a, n % m * b)
print(cal)


(boxes, deligate, built, demolish) = map(int, input().split())
if boxes % deligate is 0:
	print(0)
elif boxes > deligate:
	foo = boxes % deligate
	a = (deligate - foo) * built
	b = foo * demolish
	print(min(a, b))
elif boxes < deligate:
	print(min(boxes * demolish, (deligate - boxes) * built))


def getMinCost(n, m, a, b):
	k = n % m
	return min(k * b, (m - k) * a)
(n, m, a, b) = map(int, input().split(' '))
print(getMinCost(n, m, a, b))


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print('0')
	exit()
tmp1 = n % m
tmp2 = m - tmp1
tmp1 = tmp1 * b
tmp2 = tmp2 * a
print(min(tmp1, tmp2))


(n, m, a, b) = list(map(int, input().split()))
ans = 100000000000000000000
if n % m == 0:
	ans = 0
k = n % m
ans = min(ans, k * b, (m - k) * a)
print(ans)


import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
sys.setrecursionlimit(10000)

def read_int():
	return int(input())

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

def read_float():
	return float(input())

def read_float_n():
	return list(map(float, input().split()))

def read_str():
	return input()

def read_str_n():
	return list(map(str, input().split()))

def error_print(*args):
	print(*args, file=sys.stderr)

def mt(f):
	import time

	def wrap(*args, **kwargs):
		s = time.time()
		ret = f(*args, **kwargs)
		e = time.time()
		error_print(e - s, 'sec')
		return ret
	return wrap

@mt
def slv(N, M, A, B):
	t1 = M - N % M
	t2 = M - t1
	return min(t1 * A, t2 * B)

def main():
	(N, M, A, B) = read_int_n()
	print(slv(N, M, A, B))
main()


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
else:
	per = (m - n % m) * a
	sec = n % m * b
	print(min(per, sec))


a = list(map(int, input().split()))
n1 = a[0] % a[1]
n2 = a[1] - n1
print(min(n1 * a[3], n2 * a[2]))


(n, m, a, b) = input().split(' ')
n = int(n)
m = int(m)
a = int(a)
b = int(b)
if n % m == 0:
	print(0)
else:
	ba = int(n / m)
	bb = ba
	ba = ba + 1
	ba = ba * m - n
	ba = ba * a
	bb = n - bb * m
	bb = bb * b
	if bb > ba:
		print(ba)
	else:
		print(bb)


i = list(map(int, input().split()))
n = i[0]
m = i[1]
a = i[2]
b = i[3]
mod = n % m
if mod == 0:
	print(0)
else:
	divide = n // m
	built = m - mod
	destroy = mod
	DC = destroy * b
	BC = built * a
	print(min(DC, BC))


(n, m, a, b) = map(int, input().split())
k = n % m
z = m - k
print(min(a * z, k * b))


(n, m, a, b) = map(int, input().strip().split())
k = n // m
l = n // m + 1
k1 = (n - k * m) * b
k2 = (l * m - n) * a
print(min(k1, k2))


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 998244353

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def main():
	(n, m, a, b) = LI()
	r = n % m * b
	t = (m - n % m) * a
	return min(r, t)
print(main())


import sys
import collections
import math
import itertools as it

def readArray(type=int):
	line = input()
	return [type(x) for x in line.split()]

def getFreq(arr):
	freq = collections.defaultdict(int)
	for x in arr:
		freq[x] += 1
	return freq

def getNearestLargerMultiple(n, m):
	return math.ceil(n / m)

def getNearestSmallerMultiple(n, m):
	return n // m

def solve():
	(n, m, a, b) = readArray()
	cost = 0
	ex_cost = n * b
	if n <= m:
		cost = (m - n) * a
	else:
		costa = (n - m) * b
		costb = (m * getNearestLargerMultiple(n, m) - n) * a
		costc = abs(m * getNearestSmallerMultiple(n, m) - n) * b
		cost = min(costa, costb, costc)
	print(min(cost, ex_cost))
solve()


(n, m, a, b) = [int(i) for i in input().split()]
k = n % m
print(min((m - k) * a, k * b))


(n, m, a, b) = input().split()
n = int(n)
m = int(m)
a = int(a)
b = int(b)
if n % m == 0:
	print('0')
else:
	if n < m:
		cost_of_const = (m - n) * a
		cost_of_dest = n * b
	else:
		s = n // m
		cost_of_dest = (n - m * s) * b
		cost_of_const = (m * (s + 1) - n) * a
	print(min(cost_of_const, cost_of_dest))


(n, m, a, b) = map(int, input().split(' '))
ans1 = n % m * b
ans2 = (m - n % m) * a
print(str(min(ans1, ans2)))


(n, m, a, b) = (int(x) for x in input().split())
x = n
c = n
if n % m == 0:
	print(0)
else:
	p = n % m * b
	s = (m - n % m) * a
	print(min(p, s))


(n, m, a, b) = input().split()
(n, m, a, b) = [int(n), int(m), int(a), int(b)]
if n % m == 0:
	print('0')
else:
	s = n // m
	b1 = s * m
	a1 = (s + 1) * m
	c1 = (a1 - n) * a
	c2 = (n - b1) * b
	print(min(c1, c2))


arr = list(map(int, input().split()))
(n, m, a, b) = (arr[0], arr[1], arr[2], arr[3])
if n == m:
	print(0)
else:
	p = n % m
	q = n - p
	q += m
	q -= n
	z = p * b
	x = q * a
	if z < x:
		print(z)
	else:
		print(x)


(n, m, a, b) = map(int, input().split())
k = n % m
s = k * b
p = (m - k) * a
print(min(s, p))


import sys
import math
fileoperation = 0
if fileoperation:
	orig_stdout = sys.stdout
	orig_stdin = sys.stdin
	inputfile = open('W:/Competitive Programming/input.txt', 'r')
	outputfile = open('W:/Competitive Programming/output.txt', 'w')
	sys.stdin = inputfile
	sys.stdout = outputfile
mod = 1000000007

def nospace(l):
	ans = ''.join((str(i) for i in l))
	return ans
(n, m, a, b) = map(int, sys.stdin.readline().split(' '))
val = (m - n % m) * a
v2 = n % m * b
print(min(val, v2))
if fileoperation:
	sys.stdout = orig_stdout
	sys.stdin = orig_stdin
	inputfile.close()
	outputfile.close()


(l, m, a, b) = map(int, input().split())
y = l % m * b
z = (m - l % m) * a
print(min(y, z))


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
else:
	k1 = abs((n // m + 1) * m - n)
	k2 = abs(n // m * m - n)
	k1 = k1 * a
	k2 = k2 * b
	print(min(k1, k2))


(n, m, make, breaks) = [int(x) for x in input().split()]
extra = n % m
require = min(extra * breaks, (m - extra) * make)
print(require)


(a, b, c, d) = map(int, input().split())
if a % b == 0:
	print('0')
else:
	m = a % b * d
	n = (b - a % b) * c
	if m > n:
		print(n)
	else:
		print(m)


a = [int(x) for x in input().split()]
b = a[0] % a[1]
c = a[1] - b
print(min(c * a[2], b * a[3]))


(n, m, a, b) = map(int, input().split())
print(min(abs(n // m * m - n) * b, abs((n // m + 1) * m - n) * a))


(n, m, a, b) = [int(x) for x in input().strip().split()]
l = n - n // m * m
r = (n // m + 1) * m - n
if a * r > b * l:
	print(b * l)
else:
	print(a * r)


from math import *
(n, m, a, b) = map(int, input().split())
x = n % m
p = x * b
q = (m - x) * a
if p < q:
	print(p)
else:
	print(q)


(n, m, a, b) = [int(x) for x in input().split()]
rem = n % m
add = m - rem
add *= a
rem *= b
if add > rem:
	print(rem)
else:
	print(add)


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


import math
(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
	exit()
r = n % m
x = math.ceil(n / m)
z = (x * m - n) * a
zz = r * b
print(min(z, zz))


(n, m, a, b) = map(int, input().split())
aa = m - n % m
bb = n % m
print(min(aa * a, bb * b))


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
elif (m - n % m) * a > b * (n % m):
	print(b * (n % m))
else:
	print((m - n % m) * a)


(n, m, a, b) = map(int, input().split())
ans = 0
if n % m == 0:
	print(0)
else:
	s = n % m
	print(min(s * b, (m - s) * a))


(n, m, a, b) = input().split()
n = int(n)
m = int(m)
a = int(a)
b = int(b)
x = n % m
if x == 0:
	print('0')
else:
	y = m - x
	x = x * b
	y = y * a
	if x < y:
		print(x)
	else:
		print(y)


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
else:
	x = n % m
	y = m - x
	xx = x * b
	yy = y * a
	if yy >= xx:
		print(xx)
	else:
		print(yy)


def round_up(n, m):
	if n % m == 0:
		return n
	return n + (m - n % m)
(n, m, a, b) = map(int, input().split())
print(min(a * (round_up(n, m) - n), b * (n % m)))


inp = list(map(int, input().split()))
n = inp[0]
m = inp[1]
a = inp[2]
b = inp[3]
if n % m == 0 and n >= 1 and (a >= 1) and (b <= 100):
	print(0)
elif n > m and n >= 1 and (a >= 1) and (b <= 100):
	q = n // m
	build = ((q + 1) * m - n) * a
	r = n % m
	dem = r * b
	if build > dem:
		print(dem)
	else:
		print(build)
elif n < m and n >= 1 and (a >= 1) and (b <= 100):
	dem = n * b
	build = (m - n) * a
	if build > dem:
		print(dem)
	else:
		print(build)


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	ans = 0
else:
	p = n // m
	ans = min(a * ((p + 1) * m - n), b * (n - p * m))
print(ans)


(n, m, a, b) = map(int, input().split())
d = n // m
i = (n - d * m) * b
j = ((d + 1) * m - n) * a
if i < j:
	print(i)
else:
	print(j)


(n, m, a, b) = map(int, input().split())
rem = n % m
lol = rem * b
lol_lol = (m - rem) * a
print(min(lol, lol_lol))


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
else:
	remove = n % m
	add = m - n % m
	remove *= b
	add *= a
	print(min(add, remove))


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
elif m > n:
	ans1 = b * n
	ans2 = (m - n) * a
	print(min(ans1, ans2))
else:
	temp = n // m
	m1 = temp * m
	temp = temp + 1
	m2 = temp * m
	ans1 = abs(m1 - n)
	ans2 = abs(m2 - n)
	ans = min(ans1 * b, ans2 * a)
	print(ans)


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
	exit()
z1 = (n - n // m * m) * b
z2 = (n // m * m + m - n) * a
print(min(z1, z2))


import math
s = input().split()
for i in range(4):
	s[i] = int(s[i])
print(min(s[0] % s[1] * s[3], (s[1] - s[0] % s[1]) * s[2]))


(n, m, a, b) = [int(i) for i in input().split()]
if n % m != 0:
	print(min((m - n % m) * a, n % m * b))
else:
	print(0)


(n, m, a, b) = input().split()
n = int(n)
m = int(m)
a = int(a)
b = int(b)
l = n % m
p = min((m - l) * a, b * l)
print(p)


p = list(map(int, input().split()))
n = p[0]
m = p[1]
a = p[2]
b = p[3]
k = n % m
if k == 0:
	print(0)
else:
	d = b * k
	n = a * (m - k)
	r = min(d, n)
	print(r)


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
else:
	first = n % m * b
	second = (m - n % m) * a
	print(min(first, second))


(n, m, a, b) = map(int, input().split())
g = n % m * b
z = (m - n % m) * a
print(min(g, z))


x = list(map(int, input().split(' ')))
if x[0] % x[1] == 0:
	print(0)
else:
	y = x[0] % x[1]
	print(min(x[3] * y, (x[1] - y) * x[2]))


(n, m, a, b) = (int(i) for i in input().split())
if n % m == 0:
	print(0)
elif a * (m * (n // m + 1) - n) >= b * (n - m * (n // m)):
	print(b * (n - m * (n // m)))
else:
	print(a * (m * (n // m + 1) - n))


line = list(map(int, input().split()))
n = line[0]
m = line[1]
a = line[2]
b = line[3]
demolish = n % m * b
construct = (m - n % m) * a
print(str(min(demolish, construct)))


while True:
	try:
		if __name__ == '__main__':
			(box, delg, buil, brok) = map(int, input().split())
			if box % delg == 0:
				print(0)
			else:
				xtr = box % delg
				print(min(xtr * brok, (delg - xtr) * buil))
	except EOFError:
		break


def main():
	(n, m, a, b) = list(map(int, input().split()))
	if n > m:
		d2 = n % m
		d1 = m - d2
	else:
		d1 = m - n
		d2 = n
	print(min(d1 * a, d2 * b))
main()


def i_ints():
	return list(map(int, input().split()))
(n, m, a, b) = i_ints()
rest = n % m
print(min(rest * b, (m - rest) * a))


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
else:
	r = n % m
	ff = int((m - r) * a)
	ss = int(r * b)
	print(min(ff, ss))


(n, m, a, d) = [int(x) for x in input().split()]
k = n % m
l = m - k
if k > 0:
	print(min(l * a, k * d))
else:
	print(k)


(n, m, a, b) = map(int, input().split())
const = n % m * b
demol = (m - n % m) * a
if const > demol:
	print(demol)
else:
	print(const)


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


(n, m, a, b) = map(int, input().split())
rem = n % m
buildcost = (m - rem) * a
destrcost = rem * b
print(min(buildcost, destrcost))


(n, m, a, b) = map(int, input().split())
y = n % m * b
z = (m - n % m) * a
print(min(y, z))


(n, m, a, b) = map(int, input().split())
n2 = n // m
n3 = n - m * n2
n4 = m * (n2 + 1) - n
print(min(n3 * b, n4 * a))


from math import *
import sys
(n, m, a, b) = input().split()
n = int(n)
m = int(m)
a = int(a)
b = int(b)
if n / m == 0:
	print(0)
	sys.exit(0)
temp1 = ceil(n / m)
temp2 = floor(n / m)
x = (m * temp1 - n) * a
y = (n - m * temp2) * b
if x > y:
	print(y)
else:
	print(x)


s = input()
arr = list(map(int, s.split()))
a = arr[0] % arr[1] * arr[3]
b = (arr[1] - arr[0] % arr[1]) * arr[2]
if a > b:
	print(b)
else:
	print(a)


(n, m, a, b) = map(int, input().split())
c = n % m * b
d = (m - n % m) * a
print(min(c, d))


import sys
import io
stream_enable = 0
inpstream = '\n2 7 3 7\n'
if stream_enable:
	sys.stdin = io.StringIO(inpstream)
	input()

def inpmap():
	return list(map(int, input().split()))
(n, m, a, b) = inpmap()
x = n % m * b
y = (m - n % m) * a
print(min(x, y))


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
else:
	add = ((n // m + 1) * m - n) * a
	minu = (n - n // m * m) * b
	print(min(add, minu))


(a, b, c, d) = [int(x) for x in input().split()]
x = a % b
y = b - x
print(min(x * d, y * c))


(n, m, a, b) = map(int, input().split())
de = n % m
bu = m - de
print(min(de * b, bu * a))


(n, m, a, b) = [int(x) for x in input().split()]
p = n % m
s = (m - p) * a
d = p * b
print(min(s, d))


(n, m, a, b) = map(int, input().split())
if n % m:
	more = m - n % m
	less = n % m
	print(min(more * a, less * b))
else:
	print(0)


(n, m, a, b) = map(int, input().split())
print(min(n % m * b, (m - n % m) * a))
d = 7
e = 6
r = d + e


(n, m, a, b) = map(int, input().split())
rem = int(n / m)
req = rem * m
lis = []
if n > req:
	cost1 = (m - (n - req)) * a
	lis.append(cost1)
else:
	cost2 = (req - n) * a
	lis.append(cost2)
if n > req:
	cost3 = (n - req) * b
	lis.append(cost3)
else:
	cost4 = n * b
	lis.append(cost4)
lis.sort()
print(lis[0])


(n, m, a, b) = [int(x) for x in input().split()]
if n % m == 0:
	print(0)
	quit()
c = n // m
d = c + 1
c1 = c * m
d1 = d * m
s1 = abs(c1 - n) * b
s2 = abs(d1 - n) * a
print(min(s1, s2))


inp = input()
temp = inp.split()
n = int(temp[0])
m = int(temp[1])
a = int(temp[2])
b = int(temp[3])
if n % m == 0:
	print(0)
else:
	num_to_remove = n % m
	num_to_add = m - n % m
	cost_to_remove = b * num_to_remove
	cost_to_add = a * num_to_add
	print(min(cost_to_remove, cost_to_add))


import math
(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
else:
	p = math.ceil(n / m)
	q = math.floor(n / m)
	inc = (p * m - n) * a
	dec = (n - q * m) * b
	print(min(inc, dec))


n = [int(num) for num in input().split()]
d = n[0] % n[1]
if d != 0:
	b = d * n[3]
	a = (n[1] - d) * n[2]
	print(min(a, b))
else:
	print(0)


(n, m, a, b) = map(int, input().split())
k = n // m
print(min((n - k * m) * b, ((k + 1) * m - n) * a))


data = [int(z) for z in input('').split()]
n = data[0]
m = data[1]
a = data[2]
b = data[3]
judge = n / m
if n / m == n // m:
	print(0)
else:
	fee1 = n % m * b
	fee2 = (int(n / m + 1) * m - n) * a
	if fee1 < fee2:
		print(fee1)
	else:
		print(fee2)


k = input('')
k = k.split()
n = int(k[0])
m = int(k[1])
a = int(k[2])
b = int(k[3])
print(min((m - n % m) * a, n % m * b))


import sys
(n, m, a, b) = list(map(int, input().split()))
if n % m == 0:
	print('0')
	sys.exit()
if n % m * b < (m - n % m) * a:
	print(n % m * b)
	sys.exit()
else:
	print((m - n % m) * a)
	sys.exit()


(n, m, a, b) = list(map(int, input().split()))
if n % m == 0:
	print(0)
else:
	k1 = n % m
	k2 = m - k1
	print(min(k1 * b, k2 * a))


import math

def main():
	[n, m, a, b] = map(int, input().split())
	n1 = math.floor(n / m) * m
	n2 = math.ceil(n / m) * m
	s1 = (n - n1) * b
	s2 = (n2 - n) * a
	print(min(s1, s2))
main()


def f(n, m, a, b):
	rest = n % m
	feedown = rest * b
	feeup = (m - rest) * a
	return min(feedown, feeup)
(n, m, a, b) = [int(i) for i in input().split()]
print(f(n, m, a, b))


import math
(n, m, a, b) = map(int, input().split())
t1 = n % m
t2 = m * math.ceil(n / m) - n
if not n % m:
	print(0)
else:
	print(min(t1 * b, t2 * a))


from functools import cmp_to_key
s = input()
s = s.split(' ')
n = int(s[0])
m = int(s[1])
a = int(s[2])
b = int(s[3])
k = n // m
y = n - m * k
x = m - y
print(min(x * a, y * b))


(n, m, a, b) = list(map(int, input().strip().split()))
if n % m == 0:
	print(0)
else:
	temp = n // m
	demo = n - temp * m
	demo = demo * b
	temp = temp + 1
	bui = temp * m - n
	bui = bui * a
	if bui >= demo:
		print(demo)
	else:
		print(bui)


(n, m, a, b) = map(int, input().split())
floor = n - n % m
ceil = floor + m
print(min(a * (ceil - n), b * (n - floor)))


import sys, collections
(n, m, a, b) = map(int, sys.stdin.readline().split())
if n % m == 0:
	print(0)
else:
	print(min(a * ((n // m + 1) * m - n), b * (n % m)))


from math import *
from sys import stdin, stdout
coder = 'sdpt'
[n, m, a, b] = list(map(int, stdin.readline().split()))
x = ceil(float(n) / float(m))
y = floor(float(n) / float(m))
build = (x * m - n) * a
dest = (n - y * m) * b
print(min(build, dest))


(n, m, a, b) = map(int, input().split())
tmp = n % m
if tmp == 0:
	print(0)
else:
	a_cost = a * (m - tmp)
	b_cost = b * tmp
	print(min(a_cost, b_cost))


import sys
(n, m, a, b) = map(int, input().split())
l = n // m * m
r = ((n - 1) // m + 1) * m
ans = min(b * (n - l), a * (r - n))
print(ans)


(n, m, a, b) = map(int, input().split())
extra = n % m
ans = min(b * extra, a * (m - extra))
print(ans)


import os, sys, math

def main():
	(n, m, A, B) = [int(x) for x in input().split()]
	a = n / m
	a_frac = a - math.floor(a)
	if a_frac <= A / (A + B):
		print(n % m * B)
	else:
		print((m - n % m) * A)

def debug(*args):
	if 'DEBUG' in os.environ:
		print(*args)
main()


def solve(n, m, a, b):
	r = n % m
	if r == 0:
		return 0
	(c1, c2) = (b * r, (m - r) * a)
	return min(c1, c2)

def driver():
	inp = [*map(lambda x: int(x), input().split())]
	(n, m, a, b) = (inp[0], inp[1], inp[2], inp[3])
	print(solve(n, m, a, b))
driver()


(n, m, a, b) = map(int, input().split())
a1 = int()
b1 = int()
a1 = a * (m - n % m)
b1 = b * (n % m)
print(min(a1, b1))


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print('0')
else:
	x = n % m
	min1 = b * x
	y = m - x
	min2 = a * y
	print(min(min1, min2))


(n, m, a, b) = list(map(int, input().strip().split()))
cost_of_demolition = n % m * b
cost_of_building = (m - n % m) * a
print(min(cost_of_building, cost_of_demolition))


(n, m, a, b) = map(int, input().split())
q = n // m
x = q * m
res = min((n - x) * b, (x + m - n) * a)
print(res)


(n, m, a, b) = list(map(int, input().split()))
l = n // m
r = n // m if n % m == 0 else n // m + 1
l *= m
r *= m
print(min(a * (r - n), b * (n - l)))


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
else:
	to_delete = n % m
	to_build = m - n % m
	if to_build * a > to_delete * b:
		print(to_delete * b)
	else:
		print(to_build * a)


(n, m, a, b) = [int(x) for x in input().split(' ')]
if n % m == 0:
	print(0)
else:
	rem = n % m
	add_amt = (m - rem) * a
	sub_amt = rem * b
	print(add_amt if add_amt < sub_amt else sub_amt)


from sys import stdin, stdout
(n, m, a, b) = map(int, stdin.readline().split())
ans = 0
if n % m == 0:
	ans = 0
else:
	r = n % m
	cost1 = b * r
	cost2 = (m - r) * a
	ans = min(cost2, cost1)
stdout.write(str(ans))


(n1, m1, a1, b1) = map(int, input().split())

def burli_estimating(n, m, a, b):
	if n > m:
		differ = n % m
		building = (m - differ) * a
		destroying = differ * b
		if building > destroying:
			return destroying
		return building
	elif n < m:
		building = (m - n) * a
		destroying = n * b
		return building if building < destroying else destroying
	elif n == m:
		return 0
	else:
		return 'something has gone wrong!'
print(burli_estimating(n1, m1, a1, b1))


(n, m, a, b) = [int(x) for x in input().strip().split()]
if n % m == 0:
	print(0)
else:
	x = n // m
	t1 = abs(n - x * m)
	t2 = abs(n - (x + 1) * m)
	print(min(t1 * b, t2 * a))


from array import array
from sys import stdin
import bisect
from bisect import *
import itertools
from itertools import *

def scan_gen():
	for line in stdin:
		yield from iter(line.split())
scan = scan_gen()

def nint():
	return int(next(scan))

def nintk(k):
	return tuple((nint() for _ in range(k)))

def nfloat():
	return float(next(scan))

def intar_init(size):
	return array('i', [0]) * size

def intar(size=None):
	if size == None:
		size = nint()
	arr = intar_init(size)
	for x in range(size):
		arr[x] = nint()
	return arr

def solve():
	(n, m, a, b) = nintk(4)
	d = n % m
	dd = m - d
	print(min(d * b, dd * a))
solve()


(n, m, a, b) = map(int, input().split())
n1 = n % m
n2 = m - n % m
print(min(n1 * b, n2 * a))


abcd = input().split()
m = int(abcd[0])
n = int(abcd[1])
a = int(abcd[2])
b = int(abcd[3])
t = m % n
if t == 0:
	print(0)
elif (n - t) * a >= t * b:
	print(t * b)
else:
	print((n - t) * a)


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
elif n < m:
	(temp1, temp2) = (m - n, n % m)
	(x, y) = (temp1 * a, temp2 * b)
	print(min(x, y))
else:
	i = n // m
	i = m * (i + 1) - n
	(temp1, temp2) = (n % m, i)
	(x, y) = (temp1 * b, temp2 * a)
	print(min(x, y))


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
else:
	import math
	top = math.ceil(n / m)
	down = math.floor(n / m)
	print(min((m * top - n) * a, (n - down * m) * b))


import io, sys, atexit, os
import math as ma
from sys import exit
from decimal import Decimal as dec
from itertools import permutations
from itertools import combinations

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

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

def nu():
	return int(input())

def find_gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def prod(n):
	s = 1
	while n != 0:
		s = s * (n % 10)
		n = n // 10
	return s

def check(xp):
	op = sorted(xp)
	if op == xp:
		return True
mm = 1000000007

def solve():
	t = 1
	for it in range(t):
		(n, m, a, b) = num()
		if n % m == 0:
			print(0)
		else:
			xp = n % m
			print(min(a * (m - xp), b * xp))
solve()


import sys
(n, m, a, b) = list(map(int, input().split()))
if n % m == 0:
	print('0')
	sys.exit()
x = n // m * m
y = (n // m + 1) * m
m1 = (y - n) * a
m2 = (n - x) * b
print(min(m1, m2))


(n, m, a, b) = [int(i) for i in input().split()]
c = n % m
d = m - c
if c == 0:
	print(0)
elif c * b >= d * a:
	print(d * a)
else:
	print(c * b)


s = input()
l = s.split(' ')
n = int(l[0])
m = int(l[1])
a = int(l[2])
b = int(l[3])
p = n % m
if p == 0:
	print(0)
else:
	pb = m - p
	pd = p
	if pb * a > pd * b:
		print(pd * b)
	else:
		print(pb * a)


s = input().split()
s = [int(i) for i in s]
(n, m, a, b) = s
if n % m == 0:
	print(0)
else:
	v1 = n % m * b
	v2 = (m - n % m) * a
	print(min(v1, v2))


while True:
	try:
		(n, m, a, b) = map(int, input().split())
		flg = False
		if n // m == 0:
			t = n * b
			t1 = (m - n) * a
			print(min(t, t1))
		elif n % m != 0:
			t = n % m * b
			t1 = (m - n % m) * a
			print(min(t, t1))
		else:
			print('0')
	except EOFError:
		break


import sys
from array import array
(n, m, a, b) = map(int, input().split())
d = n // m
print(min((n - d * m) * b, ((d + 1) * m - n) * a))


(n, m, a, b) = list(map(int, input().split()))
r = n % m
lom = r * b
st = (m - r) * a
print(min(lom, st))


(a, b, c, d) = [int(x) for x in input().split()]

def min(a, b):
	if a >= b:
		return b
	else:
		return a
a = min(a % b * d, (b - a % b) * c)
print(a)


p = input().rstrip().split(' ')
n = int(p[0])
m = int(p[1])
a = int(p[2])
b = int(p[3])
if n % m == 0:
	print(0)
elif n > m:
	f = (n - m) * b
	r = n // m
	h = m * r
	F = (n - h) * b
	H = m * (r + 1)
	s = (H - n) * a
	print(min(f, F, s))
else:
	f = n * b
	G = (m - n) * a
	print(min(f, G))


(n, m, a, b) = list(map(int, input().split()))
if n % m == 0:
	print(0)
else:
	t = n // m
	t *= m
	print(min(a * (t + m - n), b * (n - t)))


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
else:
	sn = n % m
	st = m - n % m
	if sn * b < st * a:
		print(sn * b)
	else:
		print(st * a)


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
else:
	B = n % m * b
	A = (m - n % m) * a
	print(min(A, B))


arrayInput = input().split(' ')
n = int(arrayInput[0])
m = int(arrayInput[1])
a = int(arrayInput[2])
b = int(arrayInput[3])
x = n % m
print(min(a * (m - x), b * x))


def ii():
	return int(input())

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

def li():
	return list(mi())
(n, m, a, b) = mi()
d = n % m
c1 = d * b
c2 = (m - d) * a
print(min(c1, c2))


c = input().split()
n = int(c[0])
m = int(c[1])
a = int(c[2])
b = int(c[3])
lower = m * (n // m)
upper = lower + m
print(min(a * (upper - n), b * (n - lower)))


def f(ll):
	(n, m, a, b) = ll
	c1 = n % m * b
	c2 = (m - n % m) * a
	return min(c1, c2)
l = list(map(int, input().split()))
print(f(l))


(n, m, a, b) = map(int, input().split())
to_demol = n % m
to_build = m - to_demol
t = min(a * to_build, b * to_demol)
print(t)


(n, m, a, b) = map(int, input().split())
ans = int('9' * 20)
ans = min(ans, n % m * b)
ans = min(ans, (m - n % m) * a)
print(ans)


(n, m, a, b) = [int(s) for s in input().split(' ')]
if n % m == 0:
	print('0')
	exit()
demolish = n % m
demolish *= b
build = (n // m + 1) * m - n
build *= a
print(min(build, demolish))


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
else:
	min = int(n / m) * m
	max = (int(n / m) + 1) * m
	if abs(max - n) * a < abs(min - n) * b:
		print(abs(max - n) * a)
	else:
		print(abs(min - n) * b)


import math
(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
else:
	df = n - m * math.floor(n / m)
	s1 = b * df
	s2 = a * (m - df)
	print(min(s1, s2))


from math import ceil, log
t = 1
for test in range(t):
	(n, m, a, b) = map(int, input().split())
	print(min(b * (n % m), a * (m - n % m)))


(n, m, a, b) = list(map(int, input().split()))
if n % m == 0:
	print('0')
else:
	xtraBoxes = n % m
	buildBoxes = (m - xtraBoxes) * a
	destroyBoxes = xtraBoxes * b
	if buildBoxes > destroyBoxes:
		print(destroyBoxes)
	else:
		print(buildBoxes)


def sell_boxes(boxes):
	temp = boxes[0] * boxes[3]
	return temp

def buy_boxes(boxes):
	temp = boxes[1] - boxes[0]
	temp = temp * boxes[2]
	return temp

def sell(boxes):
	mult = int(boxes[0] / boxes[1])
	if mult == 0:
		mult = 1
	temp = boxes[1] * mult
	return (boxes[0] - temp) * boxes[3]

def buy(boxes):
	mult = int(boxes[0] / boxes[1]) + 1
	if mult == 0:
		mult = 1
	boxes[1] = boxes[1] * mult
	return (boxes[1] - boxes[0]) * boxes[2]

def boxes():
	boxes = list(map(int, input().split()))
	if boxes[0] % boxes[1] == 0:
		return 0
	if boxes[0] < boxes[1]:
		temp_sell = sell_boxes(boxes)
		temp_buy = buy_boxes(boxes)
		if temp_sell < temp_buy:
			return int(temp_sell)
		return int(temp_buy)
	elif boxes[0] > boxes[1]:
		temp_sell = sell(boxes)
		temp_buy = buy(boxes)
		if temp_sell < temp_buy:
			return int(temp_sell)
		return int(temp_buy)
A = boxes()
print(A)


import sys
(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print('0')
elif n > m:
	count = n % m
	mul = (m - count) * a
	d = count * b
	if mul < d:
		print(mul)
	else:
		print(d)
elif n < m:
	count = (m - n) * a
	count1 = n * b
	if count > count1:
		print(count1)
	else:
		print(count)


(n, m, a, b) = map(int, input().split())
print(min(n % m * b, (m - n % m) * a))
f = 6
g = 9
h = g - f


(n, m, a, b) = map(int, input().split())
if n % m != 0:
	neg = n % m
	pos = m - neg
	if neg * b > pos * a:
		print(pos * a)
	else:
		print(neg * b)
else:
	print('0')


(n, m, a, b) = map(int, input().split())
ans = n % m * b
ans2 = (m - n % m) * a
print(min(ans, ans2))


import sys, math, queue
MOD = 998244353
sys.setrecursionlimit(1000000)
(n, m, a, b) = map(int, input().split())
x = a * (m - n % m)
y = b * (n % m)
print(min(x, y))


k = input().split()
(n, m, a, b) = (int(k[0]), int(k[1]), int(k[2]), int(k[3]))
if n % m == 0:
	print(0)
else:
	snos = n % m
	dostroi = m - n % m
	print(min(a * dostroi, b * snos))


(n, m, a, b) = [int(x) for x in input().split()]
if n % m == 0:
	print(0)
	exit()
rem = n % m
print(min(rem * b, (m - rem) * a))


(n, m, a, b) = map(int, input().split())
p = n % m
if p == 0:
	print(0)
else:
	s1 = (m - n % m) * a
	s2 = n % m * b
	print(min(s1, s2))


(m, n, a, b) = input().split()
m = int(m)
n = eval(n)
a = eval(a)
b = eval(b)
mod = m % n
if mod == 0:
	money = 0
else:
	money1 = mod * b
	money2 = (n - mod) * a
	if money1 < money2:
		money = money1
	else:
		money = money2
print('{}'.format(money))


import sys
import math
import bisect
import itertools
import random

def main():
	(n, m, a, b) = map(int, input().split())
	v1 = ((m - n) % m + m) % m * a
	v2 = n % m * b
	print(min(v1, v2))
main()


import math

def main():
	(n, m, a, b) = [int(t) for t in input().split()]
	if n % m == 0:
		print(0)
	else:
		down = int(m * math.floor(n / m))
		up = int(m * math.ceil(n / m))
		print(int(min(abs(down - n) * b, abs(up - n) * a)))
main()


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
else:
	if n > m:
		r = n // m
		x = m * r + m
		y = x - n
		z1 = y * a
		z2 = (n - m * r) * b
		print(min(z1, z2))
	if n < m:
		z1 = n * b
		z2 = (m - n) * a
		print(min(z1, z2))


(n, m, a, b) = map(int, input().split())
rem = n % m
brk = n % m
if rem == 0:
	build = 0
else:
	build = m * (n // m + 1) - n
cost1 = build * a
cost2 = brk * b
print(min(cost1, cost2))


num = [int(x) for x in input().split()]
n = num[0]
m = num[1]
a = num[2]
b = num[3]
count = 0
low = n % m
high = m - low
des = b * low
build = a * high
pay = min([des, build])
print(pay)


a = tuple(map(int, input().split()))
print(min((a[1] - a[0] % a[1]) * a[2], a[0] % a[1] * a[3]))


from sys import stdin, stdout

def rint():
	return map(int, stdin.readline().split())
(n, m, a, b) = rint()
r = n % m
print(min(r * b, a * (m - r)))


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
else:
	k = n // m
	s = k * m
	p = s + m
	x = abs(n - s)
	y = abs(n - p)
	print(min(y * a, x * b))


(n, m, a, b) = map(int, input().split())
if n % m == 0:
	print(0)
elif n > m:
	d = n % m
	c = (n // m + 1) * m - n
	t1 = c * a
	t2 = d * b
	print(min(t1, t2))
elif m > n:
	c = m - n
	d = n - m * (n // m)
	t1 = c * a
	t2 = d * b
	print(min(t1, t2))