Andrew was very excited to participate in Olympiad of Metropolises. Days flew by quickly, and Andrew is already at the airport, ready to go home. He has $n$ rubles left, and would like to exchange them to euro and dollar bills. Andrew can mix dollar bills and euro bills in whatever way he wants. The price of one dollar is $d$ rubles, and one euro costs $e$ rubles.

Recall that there exist the following dollar bills: $1$, $2$, $5$, $10$, $20$, $50$, $100$, and the following euro bills — $5$, $10$, $20$, $50$, $100$, $200$ (note that, in this problem we do not consider the $500$ euro bill, it is hard to find such bills in the currency exchange points). Andrew can buy any combination of bills, and his goal is to minimize the total number of rubles he will have after the exchange.

Help him — write a program that given integers $n$, $e$ and $d$, finds the minimum number of rubles Andrew can get after buying dollar and euro bills.


-----Input-----

The first line of the input contains one integer $n$ ($1 \leq n \leq 10^8$) — the initial sum in rubles Andrew has. 

The second line of the input contains one integer $d$ ($30 \leq d \leq 100$) — the price of one dollar in rubles. 

The third line of the input contains integer $e$ ($30 \leq e \leq 100$) — the price of one euro in rubles.


-----Output-----

Output one integer — the minimum number of rubles Andrew can have after buying dollar and euro bills optimally.


-----Examples-----
Input
100
60
70

Output
40

Input
410
55
70

Output
5

Input
600
60
70

Output
0



-----Note-----

In the first example, we can buy just $1$ dollar because there is no $1$ euro bill.

In the second example, optimal exchange is to buy $5$ euro and $1$ dollar.

In the third example, optimal exchange is to buy $10$ dollars in one bill.

Source: codeforces | Difficulty: MEDIUM
Tags: Mathematics

Complete search


Solutions:
def solvea():
	n = int(input())
	d = int(input())
	e = 5 * int(input())
	best = 0
	for enr in range(n // e + 1):
		curr = e * enr
		curr += d * ((n - curr) // d)
		if curr <= n and curr > best:
			best = curr
	print(n - best)

def solveb():
	a = int(input())
	b = int(input())
	n = int(input())
	ans = 0
	for i in range(a + 1):
		for j in range(b + 1):
			if i + j == n:
				ans += 1
	print(ans)
solvea()


n = int(input())
a = int(input())
b = int(input())
ans = n
b *= 5
for i in range(n // a + 1):
	t = n - i * a
	z = t - t // b * b
	ans = min(ans, z)
print(ans)


n = int(input())
d = int(input())
e = int(input())
mn = n % d
e *= 5
if n < e:
	print(n % d)
else:
	while n > d:
		cur = n % e
		now = cur % d
		mn = min(mn, now)
		n -= d
	print(mn)


n = int(input())
d = int(input())
e = int(input())
ans = n
for i in range(n):
	if i * 5 * e <= n:
		ans = min(ans, (n - i * 5 * e) % d)
	else:
		break
print(ans)


n = int(input())
d = int(input())
e = int(input())
up = n // (e * 5)
ans = n
for i in range(0, up + 1):
	e1 = n - e * 5 * i
	e2 = e1 - e1 // d * d
	ans = min(ans, e2)
print(ans)


n = int(input())
d = int(input())
e = int(input())
ans = n % d
for i in range(100000):
	if 5 * e * i <= n:
		k = n - 5 * e * i
		if ans > k % d:
			ans = k % d
print(ans)


n = int(input())
d = int(input())
e = int(input())
ans = n
e *= 5
a = max(d, e)
c = min(d, e)
b = n // a
for i in range(0, b + 1):
	s = i * a
	t = (n - s) % c
	ans = min(t, ans)
print(ans)


from sys import setcheckinterval, stdin
setcheckinterval(1000)
iin = lambda : int(stdin.readline())
lin = lambda : list(map(int, stdin.readline().split()))
n = iin()
d = iin()
e = iin()
e *= 5
mn = min(d, e)
mx = max(d, e)
ans = n
for i in range(n // mx + 1):
	ans = min(ans, (n - i * mx) % mn)
print(ans)


def currency(n, d, e):
	e *= 5
	x = n
	for i in range(0, n + 1, e):
		x = min(x, (n - i) % d)
	return x
N = int(input())
D = int(input())
E = int(input())
print(currency(N, D, E))


(n, d, e) = [int(input()) for _ in range(3)]
DOLLAR_BILLS = [1, 2, 5, 10, 20, 50, 100]
EURO_BILLS = [5, 10, 20, 50, 100, 200]
min_rest = float('inf')
for i in range(0, n // e + 1):
	if i % 5 != 0:
		continue
	rest = (n - i * e) % d
	min_rest = min(min_rest, rest)
print(min_rest)


def min(u, v):
	if u < v:
		return u
	else:
		return v
n = int(input())
d = int(input())
u = int(input())
u *= 5
ans = 1000000000
for i in range(0, n + 1):
	took = u * i
	if took > n:
		break
	ans = min(ans, (n - took) % d)
print(ans)


n = int(input())
d = int(input())
e = int(input())
e *= 5
m = 10 ** 8
for i in range(n + 1):
	k = (n - i) // e
	w = i // d
	m = min(m, n - k * e - w * d)
print(m)


b = int(input())
g = int(input())
n = int(input())
if 5 * n % g == 0:
	print(b % g)
else:
	s = []
	t = 0
	while 1:
		if t * 5 * n <= b:
			s.append((b - 5 * t * n) % g)
			t += 1
		else:
			break
	print(min(s))


n = int(input())
d = int(input())
e = int(input())
i = 0
result = 1000000000
while i * e <= n:
	temp = n - i * e
	result = min(temp % d, result)
	i += 5
print(result)


n = int(input())
d = int(input())
e = int(input())
ans = 1000000000.0
for i in range(n // (e * 5) + 1):
	ans = min((n - i * e * 5) % d, ans)
print(ans)


summ = int(input())
d = int(input())
e = int(input()) * 5
min_resid = summ
iters = 0
n_eur = summ // e
while n_eur >= 0 and min_resid > 0 and (iters < d):
	resid = (summ - n_eur * e) % d
	if resid < min_resid:
		min_resid = resid
	n_eur -= 1
	iters += 1
print(min_resid)


import sys
input = sys.stdin.readline
n = int(input())
d = int(input())
e = int(input())
D = d * 1
E = e * 5
x = n % D
ANS = x
for i in range(x, min(n, x + D * 1000) + 1, D):
	ANS = min(ANS, i % E)
print(ANS)


import sys
import math
input = sys.stdin.readline
n = int(input())
d = int(input())
e = int(input())
e = 5 * e
fe = 0
minx = n + 1
while e * fe <= n:
	cur = e * fe
	rem = (n - cur) % d
	minx = min(minx, rem)
	fe += 1
print(minx)


n = int(input())
d = int(input())
e = int(input())
bills = sorted([x * d for x in [1]] + [x * e for x in [5]])
mi = n
for i in range(0, n + 1, bills[0]):
	mi = min(mi, n - i - (n - i) // bills[1] * bills[1])
print(mi)


n = int(input())
d = int(input())
e = int(input())
e = e * 5
b = 0
m = n
while b <= n:
	if m > (n - b) % d:
		m = (n - b) % d
	b = b + e
print(m)


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
(n,) = I()
(d,) = I()
(e,) = I()
e *= 5
ans = MOD
for i in range(min(10 ** 7 + 1, n + 1)):
	j = n - i
	ans = min(ans, n - i // d * d - j // e * e)
print(ans)


rib = int(input())
dol_in_rib = int(input())
eur_in_rib = int(input()) * 5
rib %= max(dol_in_rib, eur_in_rib) * 501
(i, j, min_rib) = (0, 0, 101)
while i <= rib // dol_in_rib + 1:
	j = 0
	while j <= (rib - i * dol_in_rib) // eur_in_rib + 1:
		current_rib = rib - i * dol_in_rib - j * eur_in_rib
		if current_rib < min_rib and current_rib > -1:
			min_rib = current_rib
		j += 1
	i += 1
print(min_rib)


n = int(input())
d = int(input())
e = int(input())
res = n
e = e * 5
for i in range(0, n // d + 1):
	val = (n - d * i) % e
	if val < res:
		res = val
print(res)


r = int(input())
d = int(input())
e = int(input()) * 5
rem = r % e
for x in range(r // e + 1):
	rem2 = (r - x * e) % d
	if rem2 < rem:
		rem = rem2
print(rem)


(n, d, e) = (int(input()), int(input()), int(input()) * 5)
minn = n
for i in range(d):
	for j in range(e):
		if i + j != 0:
			k = n % (d * i + e * j) % d % e
			if minn > k:
				minn = k
print(minn)


n = int(input())
d = int(input())
e = 5 * int(input())
ans = n
(d, e) = (min([d, e]), max([d, e]))
for i in range(e):
	if n - i * d >= 0:
		ans = min([ans, (n - i * d) % e])
print(ans)


n = int(input())
d = int(input())
e = int(input())
mi = n
for i in range(0, n + 1, 5 * e):
	mi = min(mi, n - i - (n - i) // d * d)
print(mi)


r = int(input())
d = int(input())
e = int(input())
minm = r
i = 0
j = 0
while r - i * 5 * e >= 0 and minm != 0:
	if (r - i * 5 * e) % d < minm:
		minm = (r - i * 5 * e) % d
	i += 1
print(minm)


a = int(input())
b = int(input())
c = int(input()) * 5
ans = a % b
k = 1
while k * c <= a:
	ans = min(ans, (a - c * k) % b)
	k += 1
print(ans)


import sys
import math
import bisect

def solve(n, d, e):
	min_val = 10 ** 18
	for i in range(n // (5 * e) + 1):
		val = (n - i * 5 * e) % d
		min_val = min(min_val, val)
	return min_val

def main():
	n = int(input())
	d = int(input())
	e = int(input())
	ans = solve(n, d, e)
	print(ans)
main()


n = int(input())
a = int(input())
b = int(input())
smallb = b * 5
x = n // a
minimum = n + 1
for j in range(x, -1, -1):
	diff = n - a * j
	diff -= diff // smallb * smallb
	if diff < minimum:
		minimum = diff
print(minimum)


q = int(input())
w = int(input())
e = int(input()) * 5
w = min(w, e)
e = max(w, e)
x = 5000
for i in range(q // e + 1):
	x = min((q - e * i) % w, x)
print(x)


n = int(input())
d = int(input())
e = int(input())
max_d = n // d
best = n
for i in range(max_d + 1):
	now = n - i * d
	now -= now // (5 * e) * (5 * e)
	if now < best:
		best = now
print(best)


n = int(input())
d = int(input())
e = int(input()) * 5
m = n % d
for i in range(2000):
	if n > 0:
		m1 = n % e
		if m > m1:
			m = m1
	n -= d
print(m)


from math import gcd
n = int(input())
d = int(input())
e = int(input()) * 5
g = gcd(d, e)
ans = 9999999999999999999
for i in range(min(n // e + 1, d * e // g + 10)):
	ost = n - e * i
	ans = min(ans, ost - ost // d * d)
print(ans)


(n, d, e) = (int(input()) for _ in range(3))
e *= 5
print(min(((n - i * e) % d for i in range(1 + min(d, n // e)))))


n = int(input())
d = int(input())
e = int(input())
a = n // d
n = min([(n - i * d) % (5 * e) for i in range(a + 1)])
print(n)


x = int(input())
y = int(input())
z = int(input())
y_n = int(x / y)
z_n = int(x / z)
z_real_n = int(z_n / 5) * 5
y_r = (x / y - y_n) * y
mins = x
for i in range(int((z_real_n + 1) / 5) + 1):
	m = ((x - i * z * 5) / y - int((x - i * z * 5) / y)) * y
	if m < mins:
		mins = m
print(round(min(mins, y_r)))


n = int(input())
d = int(input())
e = int(input())
t = 5 * e
ans = n
now = 0
while now <= n:
	ans = min(ans, (n - now) % d)
	now += t
print(ans)


n = int(input())
d = int(input())
e = int(input())
kq = []
test = []
kq.append(d)
kq.append(e * 5)
kq.sort()
a = n
while a >= 0:
	b = a % min(kq)
	test.append(b)
	a = a - max(kq)
print(min(test))


n = int(input())
d = int(input())
e = int(input())
A = []
f = int(n / (5 * e))
for x in range(0, f + 1):
	r = (n - 5 * x * e) % d
	A.append(r)
print(min(A))


from __future__ import print_function, division, unicode_literals
__metaclass__ = type
try:
	input = raw_input
	range = xrange
except NameError:
	pass
import sys
from math import gcd

def solveCoprime(N: int, D: int, fiveE: int) -> int:
	k = D * fiveE - D - fiveE
	if N > k:
		return 0
	DP = [False] * (N + 1)
	DP[0] = True
	hi = 0
	for n in range(1, N + 1):
		for delt in (D, fiveE):
			if n >= delt:
				DP[n] |= DP[n - delt]
		if DP[n]:
			hi = n
	return N - hi

def solve(N: int, D: int, E: int) -> int:
	d = gcd(D, 5 * E)
	rem = solveCoprime(N // d, D // d, 5 * E // d)
	return d * rem + N % d

def main() -> None:
	(N, D, E) = (int(input()) for _ in range(3))
	print(solve(N, D, E))
main()


n = int(input())
d = int(input())
e = int(input())
ans = 10 ** 9
x = n // (e * 5)
for i in range(x + 1):
	ans = min(ans, (n - i * (e * 5)) % d)
if ans == 10 ** 9:
	ans = n % d
print(ans)


n = int(input())
d = int(input())
e = int(input())
minim = n
for i in range(n // (5 * e) + 1):
	ost = (n - i * 5 * e) % d
	if ost < minim:
		minim = ost
print(minim)


n = int(input())
d = int(input())
e = int(input())
if n % d == 0 or n % (e * 5) == 0:
	print(0)
else:
	i = 0
	m = 10 ** 9
	while e * 5 * i <= n:
		p3 = n - e * 5 * i
		p4 = p3 % d
		if p4 < m:
			m = p4
		i += 1
		if m == 0:
			break
	print(m)


def gcd(x, y):
	if y == 0:
		return x
	return gcd(y, x % y)
n = int(input())
d = int(input())
e = int(input())
e *= 5
if n > 10 * e * d:
	print(n % gcd(e, d))
	exit(0)
ans = [0]
for i in range(1, n + 1):
	ans.append(i)
	if i >= d:
		ans[i] = min(ans[i], ans[i - d])
	if i >= e:
		ans[i] = min(ans[i], ans[i - e])
print(ans[n])


dollars = [1, 2, 5, 10, 20, 50, 100]
euros = [5, 10, 20, 100, 200]
r = int(input())
dr = int(input())
er = int(input())
dm = dr * dollars[0]
em = er * euros[0]
md = r // dm * dm
me = r // em * em
e = me
d = 0
run = True
mr = dm
while run:
	rr = r - e
	rr %= dm
	if rr < mr:
		mr = rr
	if mr == 0:
		run = False
	e -= em
	run = e >= 0
print(mr)


n = int(input())
d = int(input())
e = int(input())
a = n % d % (e * 5)
b = n % (e * 5) % d
c = n % (d + e * 5)
ans = n % d
n -= 5 * e
while n >= 0:
	ans = min(ans, n % d)
	n -= 5 * e
print(min(min(a, b), ans))


I = input
(n, d, e) = (int(I()), int(I()), int(I()))
i = n // d
j = 0
ans = 1000000000.0
while i >= 0:
	fx = i * d + 5 * j * e
	if fx <= n:
		ans = min(ans, n - fx)
		j += 1
	else:
		i -= 1
print(ans)


n = int(input())
d = int(input())
e = int(input())
ans = n % d
while e * 5 <= n:
	n -= e * 5
	ans = min(ans, n % d)
print(ans)


n = int(input())
d = int(input())
e = int(input())
mi = n
i = 0
while i * 5 * e <= n:
	mi = min(mi, (n - i * 5 * e) % d)
	i += 1
print(mi)


n = int(input())
d = int(input())
e = int(input()) * 5
(maxi, mini) = (e, d)
if maxi < mini:
	(maxi, mini) = (mini, maxi)
ans = mini
val = 0
while val <= n:
	ans = min(ans, (n - val) % mini)
	val = val + maxi
	if ans == 0:
		break
print(ans)


r = int(input())
d = int(input())
e = 5 * int(input())
rem = r % d
a = r // e
rem = min(rem, r % e)
if rem > 0:
	for i in range(a + 1):
		b = r - i * e
		if b < 0 or rem == 0:
			break
		rem = min(rem, b % d)
print(rem)


n = int(input())
d = int(input())
e = int(input())
e *= 5
res = 1000000000000000
for i in range(1000):
	if i * d > n:
		break
	res = min(res, (n - i * d) % e)
print(res)


n = int(input())
d = int(input())
e = int(input())
ans = float('inf')
n_d = 0
while n_d * d <= n:
	ans = min(ans, (n - n_d * d) % (5 * e))
	n_d += 1
print(ans)


n = int(input())
d = int(input())
e = int(input())
m = 10000000000
for i in range(0, n + 1, 5 * e):
	m = min(m, (n - i) % d)
print(m)


import sys
from collections import defaultdict
input = lambda : sys.stdin.readline().rstrip()
n = int(input())
d = int(input())
e = int(input())
dollar = d
euro = 5 * e
ans = n
if n % dollar == 0 or n % euro == 0:
	print(0)
	exit()
for i in range(n // dollar + 1):
	k = n - i * dollar
	k -= k // euro * euro
	ans = min(ans, k)
for i in range(n // euro + 1):
	k = n - i * euro
	k -= k // dollar * dollar
	ans = min(ans, k)
print(ans)


n = int(input())
d = int(input())
e = int(input())
max_d = n // d
max_e = n // e - n // e % 5
r_min = n
for i in range(0, max_e + 1, 5):
	res = n - i * e
	cur_rub = res - res // d * d
	if cur_rub < r_min:
		r_min = cur_rub
print(r_min)


z = input
(a, b, c) = (int(z()), int(z()), int(z()) * 5)
print(min(((a - i) % b for i in range(0, a + 1, c))))


def solve(n, d, e):
	r = n
	for f in range(n // (5 * e) + 1):
		s = (n - f * 5 * e) % d
		r = min(r, s)
	return r
n = int(input())
d = int(input())
e = int(input())
print(solve(n, d, e))


n = int(input())
d = int(input())
e = int(input())
ans = n
k = e * 5
i = 0
while i * k <= n:
	ans = min(ans, (n - i * k) % d)
	i += 1
print(ans)


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

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

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect
from collections import deque, defaultdict, OrderedDict
N = 1000001
p = 10 ** 9 + 7
fact = [None] * (N + 1)
fact[0] = 1
for i in range(1, N + 1):
	fact[i] = fact[i - 1] * i % p

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	for _ in range(1):
		n = ri()
		d = ri()
		e = ri()
		ans = n
		i = 0
		while i * 5 * e <= n:
			ans = min(ans, (n - i * 5 * e) % d)
			i += 1
		print(ans)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


n = int(input())
d = int(input())
e = 5 * int(input())
m1 = max(n % d, n % e)
m2 = min(n % d, n % e)
i = 0
ans = n
while i * e <= n:
	ans = min(ans, (n - i * e) % d)
	i += 1
print(ans)


n = int(input())
d = int(input())
e = int(input())
x1 = d
x2 = 5 * e
ans = 10 ** 8
if n % x1 == 0 or n % x2 == 0:
	print(0)
else:
	for y2 in range(n // x2 + 1):
		r = n - ((n - y2 * x2) // x1 * x1 + y2 * x2)
		ans = min(ans, r)
	print(ans)


sum = int(input())
dollar = int(input())
euro = int(input())
sol = 10 ** 18
for i in range(0, sum // 5 + 5, 5):
	sum_remained = sum - i * euro
	if sum_remained < 0:
		break
	sol = min(sol, sum - i * euro - sum_remained // dollar * dollar)
print(sol)


d = [1, 2, 5, 10, 20, 50, 100]
e = [5, 10, 20, 50, 100, 200]
n = int(input())
x = int(input()) * 1
y = int(input()) * 5
s = n
for c in range(n // y + 1):
	current = c * y
	s = min(s, (n - current) % x)
print(s)


n = int(input())
d = int(input())
e = int(input()) * 5
s = []
if e > n:
	otv = n % d
elif d > n:
	otv = n % e
else:
	for i in range(d + 2):
		p = e * i
		if p > n:
			break
		k = (n - p) % d
		s.append(k)
	otv = min(s)
print(otv)


n = int(input())
d = int(input())
e = int(input()) * 5
ans = 101
for i in range(0, 10000):
	if e * i <= n:
		ans = min(ans, (n - e * i) % d)
print(ans)


n = int(input())
d = int(input())
e = int(input())
e *= 5
dollar = 0
ans = n
while d * dollar <= n:
	val = n - d * dollar
	ans = min(ans, val % e)
	dollar += 1
print(ans)


import sys

def rint():
	return list(map(int, sys.stdin.readline().split()))
n = int(input())
d = int(input())
e = int(input())
d_min = d
e_min = 5 * e
(min1, min2) = sorted([d_min, e_min])
ans = 10 ** 10
for i in range(0, min1 + 1):
	tmp = n - i * min2
	if tmp < 0:
		break
	tmp = tmp % min1
	ans = min(tmp, ans)
print(ans)


ruble = int(input())
dollar_price = int(input())
euro_price = int(input()) * 5
if ruble < dollar_price and ruble < euro_price:
	print(ruble)
else:
	if dollar_price < euro_price:
		smaller_price = dollar_price
		bigger_price = euro_price
	else:
		smaller_price = euro_price
		bigger_price = dollar_price
	left_ruble = ruble - ruble // smaller_price * smaller_price
	min_val = left_ruble
	for i in range(ruble // smaller_price):
		left_ruble += smaller_price
		tmp = left_ruble // bigger_price
		left_ruble -= bigger_price * tmp
		if left_ruble < min_val:
			min_val = left_ruble
	print(min_val)


n = int(input())
d = int(input())
e = int(input())
e *= 5
if n % e == 0:
	print(0)
elif n % d == 0:
	print(0)
else:
	nn = n
	res = 987654321000
	while True:
		nn -= e
		if nn < 0:
			break
		res = min(res, nn % d)
	nn = n
	while True:
		nn -= d
		if nn < 0:
			break
		res = min(res, nn % e)
	if res == 987654321000:
		print(n)
	else:
		print(res)


import sys
input = lambda : sys.stdin.readline().strip('\r\n')
n = int(input())
d = int(input())
e = int(input())
ans = n
i = 0
while i * 5 * e <= n:
	ans = min(ans, (n - i * 5 * e) % d)
	i += 1
print(ans)


n = int(input())
d = int(input())
e = int(input()) * 5
k = n // e
low = n % e % d
for i in range(k):
	n1 = n
	n1 -= i * e
	low1 = n1 % d
	if low1 < low:
		low = low1
print(low)


n = int(input())
(d, e) = (int(input()), int(input()))
e = e * 5
ans = 10 ** 8
for i in range(n // e + 1):
	t = n - i * e
	t -= t // d * d
	ans = min(ans, t)
print(ans)


import sys
input = sys.stdin.readline
n = int(input())
d = int(input())
e = int(input()) * 5
ans = n
for i in range(n // e + 2):
	m = n - i * e
	if m >= 0:
		ans = min(ans, m % d)
print(ans)


rubles = int(input())
dollar = int(input())
euro = 5 * int(input())
remain_d = rubles % dollar
remain_e = rubles % euro
max_d = rubles // dollar
max_e = rubles // euro
remain = min(remain_d, remain_e)
for i in range(max_d):
	if (rubles - i * dollar) % euro < remain:
		remain = (rubles - i * dollar) % euro
for i in range(max_e):
	if (rubles - i * euro) % euro < remain:
		remain = (rubles - i * euro) % dollar
print(remain)


def main():
	a = int(input())
	b = int(input())
	c = int(input())
	ori = a
	x = 0
	while 5 * x * c <= ori:
		a = min(a, (ori - 5 * x * c) % b)
		x += 1
	print(a)
main()


n = int(input())
d = int(input())
e = int(input())
e *= 5
m = 1000000000
if n >= e:
	for i in range(1, n // e + 1):
		m = min(m, (n - i * e) % d)
if n >= d:
	for i in range(1, n // d + 1):
		m = min(m, (n - i * d) % e)
if n < d and n < e:
	m = n
print(m)


n = int(input())
a = int(input())
b = int(input())
b = 5 * b
while n >= a or n >= b:
	if n % a < n % b:
		n -= a
	else:
		n -= b
print(n)


from sys import stdin
from math import gcd, sqrt
from collections import deque
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
(n, d, e) = (I(), I(), I())
m = []
a = [0, 1, 2, 5, 10, 20, 50, 100]
b = [5, 10, 20, 50, 100, 200]
for i in range(n // d + 1):
	r = n - d * i
	for j in b:
		m += (r % (j * e),)
print(min(m))


n = int(input())
d = int(input())
e = int(input())
e *= 5
up = int(n // d) + 2
ans = n
for i in range(up):
	ans = min((n - d * i) % e, ans)
print(ans)


r = int(input())
d = int(input())
e = int(input())
x_max = r // (e * 5)
res_min = r % (e * 5) % d
for i in range(x_max):
	t = (r - (x_max - i - 1) * e * 5) % d
	if t < res_min:
		res_min = t
print(res_min)


n = int(input())
d = int(input())
e = int(input())
k = n // d
b = []
for i in range(0, k + 1):
	t = (n - d * i) // e
	b.append(i * d + (t - t % 5) * e)
print(n - max(b))


n = int(input())
d = int(input())
e = int(input())
e = e * 5
l1 = [n % d]
for i in range(1, n // e + 1):
	k = n - e * i
	l1.append(k % d)
print(min(l1))


from math import gcd
dollar = [1, 2, 5, 10, 20, 50, 100]
euro = [5, 10, 20, 50, 100, 200]
n = int(input())
d = int(input())
e = int(input())
for i in range(len(dollar)):
	dollar[i] *= d
for i in range(len(euro)):
	euro[i] *= e
(x, y) = (dollar[0], euro[0])
curr = float('inf')
for i in range(n // y + 1):
	curr = min(curr, (n - i * y) % x)
print(curr)


n = int(input())
d = int(input())
e = int(input())
m = n
take_away = 0
while m - 5 * e >= 0:
	m -= 5 * e
	take_away += 1
m %= d
minimum = m
while take_away != 0:
	m += 5 * e
	minimum = min(minimum, m % d)
	take_away -= 1
print(minimum)


n = int(input())
d = int(input())
e = int(input())
a = d
b = 5 * e
best = 10 ** 10
for b_count in range(n // b + 1):
	rem = n - b_count * b
	rem %= a
	best = min(best, rem)
print(best)


r = int(input())
d = int(input())
e = int(input())
ans = r % d
while r >= 5 * e:
	r -= 5 * e
	ans = min(ans, r % d)
print(ans)


n = int(input())
d = int(input())
e = int(input())
ans = n
if n % d == 0 or n % (e * 5) == 0:
	ans = 0
else:
	for i in range(0, n // (5 * e) + 1):
		tmp = n
		if n >= i * 5 * e:
			tmp = n - i * 5 * e
		tmp -= d * (tmp // d)
		if tmp < ans:
			ans = tmp
print(ans)


a = int(input())
b = int(input())
c = int(input())
m = b * 1
n = c * 5
g = a
for i in range(a // m + 1):
	y = a - i * m
	k = a - y // n * n - i * m
	if g > k:
		g = k
print(g)


n = int(input())
nnn = n
doll = int(input())
euro = int(input())
n = n % doll
res = n
for _ in range(nnn // doll):
	if res == 0:
		break
	n = (n + doll) % (5 * euro)
	res = min(res, n)
print(res)


n = int(input())
d = int(input())
e = int(input())
(e, ans, mul) = (5 * e, 1000000000000000.0, 0)
while True:
	dd = d * mul
	ans = min((n - dd) % e, ans)
	if dd >= n:
		break
	mul += 1
print(ans)


n = int(input())
d = int(input())
e = int(input())
minn = n
for i in range(n // d + 1):
	tmp = n - d * i
	sum = n - d * i - tmp // (5 * e) * (5 * e)
	minn = min(minn, sum)
print(minn)


(n, d, e) = (int(input()), int(input()), int(input()) * 5)
mn1 = min(n % d % e, n % e % d)
for i in range(1, n // d + 1):
	if (n - d * i) % e < mn1:
		mn1 = (n - d * i) % e
	if (n - e * i) % d < mn1 and n - e * i >= 0:
		mn1 = (n - e * i) % d
print(mn1)


n = int(input())
d = int(input())
e = int(input())
e = 5 * e
r = n % d
l = n // e
for i in range(1, l + 1):
	r = min(r, (n - i * e) % d)
print(r)


n = int(input())
d = int(input())
e = int(input())
min_d = d
min_e = 5 * e
tempd = n % min_d
tempe = n % min_e
max_d = int(n / d)
max_e = int(n / min_e)
min_r = min(tempd, tempe)
for i in range(max_d):
	if (n - i * min_d) % min_e < min_r:
		min_r = (n - i * min_d) % min_e
for i in range(max_e):
	if (n - i * min_e) % min_d < min_r:
		min_r = (n - i * min_e) % min_d
print(min_r)


def left_off(euro, usd, rubles):
	leftoff = rubles
	eur = 5 * euro
	rub = rubles
	max_eur = int(rub / eur)
	max_usd = 0
	for i in range(max_eur + 1):
		rub = rubles
		rub = rub - i * eur
		max_usd = int(rub / usd)
		rub = rub - max_usd * usd
		if rub == 0:
			return 0
		if rub < leftoff:
			leftoff = rub
	return leftoff
rub = int(input())
usd = int(input())
eur = int(input())
print(left_off(eur, usd, rub))


from sys import stdin, stdout
n = int(stdin.readline())
d = int(stdin.readline())
e = int(stdin.readline())
i = 0
ans = n
while i * 5 * e <= n:
	ans = min(ans, (n - i * 5 * e) % d)
	i += 1
stdout.write(str(ans))


import sys

def input():
	return sys.stdin.readline()[:-1]
n = int(input())
d = int(input())
e = int(input()) * 5
i = 0
ans = n
while i * e <= n:
	ans = min(ans, (n - i * e) % d)
	i += 1
print(ans)


import sys
n = int(sys.stdin.readline().strip('\n'))
d = int(sys.stdin.readline().strip('\n'))
e = int(sys.stdin.readline().strip('\n')) * 5
mn = n % d
while n >= e:
	n -= e
	mn = min(mn, n % d)
print(mn)


from math import floor
n = int(input())
d = int(input())
e = int(input())
e = 5 * e
maxd = n // d
maxe = n // e
_min = n
for d_count in range(maxd + 1):
	e_count = min((n - d * d_count) // e, maxe)
	val = n - d_count * d - e_count * e
	_min = min(_min, val)
print(_min)


init = int(input())
d = int(input())
e = int(input())
dol = d
eur = e * 5
DP = [init for _ in range(init // eur + 1)]
for i in range(init // eur + 1):
	DP[i] = (init - eur * i) % dol
print(min(DP))


n = int(input())
d = int(input())
e = int(input())
e = 5 * e
a = n // e
b = n // d
v = []
for i in range(a + 1):
	v.append((n - i * e) % d)
print(min(v))


n = int(input())
d = int(input())
e = int(input())
minRemainder = 1000000000000
index = 0
while 5 * e * index <= n:
	if minRemainder > (n - 5 * e * index) % d:
		minRemainder = (n - 5 * e * index) % d
	index += 1
print(minRemainder)


n = int(input())
d = int(input())
e = int(input())
min_e = 5 * e
i = 1
remain = n
for i in range(n, -1, -(i * min_e)):
	remain = min(remain, i % d)
print(remain)


import sys

def optimal_sol(n, d, e):
	avail_dollar = [1, 2, 5, 10, 20, 50, 100]
	avail_euro = [200, 100, 50, 20, 10, 5]
	ans = n
	for i in range(0, n + 1):
		if i * d <= n:
			ans = min(ans, (n - i * d) % (e * 5))
		else:
			break
	return ans
n = int(input())
d = int(input())
e = int(input())
out = optimal_sol(n, d, e)
print(out)


na = input()
da = input()
ea = input()
n = int(na)
d = int(da)
e = int(ea)
dr = n % d
while n >= e * 5:
	n -= e * 5
	dr = min(dr, n % d)
print(dr)


n = int(input())
d = int(input())
e = int(input())
ans = 100000000000.0 + 11
e *= 5
i = 0
while i * d <= n:
	ans = min(ans, n - i * d - (n - i * d) // e * e)
	i += 1
print(ans)


import sys
input = sys.stdin.readline

def getInt():
	return int(input())

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

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

def getStr():
	return input().strip()

def addDictList(d, key, val):
	if key not in d:
		d[key] = []
	d[key].append(val)

def addDictInt(d, key, val):
	if key not in d:
		d[key] = 0
	d[key] = val

def addDictCount(d, key):
	if key not in d:
		d[key] = 0
	d[key] += 1

def addDictSum(d, key, val):
	if key not in d:
		d[key] = 0
	d[key] += val
n = getInt()
d = getInt()
e = getInt()
m1 = max(d, e * 5)
m2 = min(d, e * 5)
res = n % m1
for x in range(n // m1 + 1):
	res = min(res, (n - x * m1) % m2)
print(res)


n = int(input(''))
d = int(input(''))
e = int(input(''))
e *= 5
ans = n
for i in range(0, n // d + 1):
	a = (n - d * i) % e
	if a < ans:
		ans = a
print(ans)


import math
import itertools
import collections

def getdict(n):
	d = {}
	if type(n) is list:
		for i in n:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
	else:
		for i in range(n):
			t = ii()
			if t in d:
				d[t] += 1
			else:
				d[t] = 1
	return d

def cdiv(n, k):
	return n // k + (n % k != 0)

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

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

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

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)
n = ii()
d = ii()
e = ii()
ans = min(n % d, n % (e * 5) % d)
while n > 0:
	ans = min(ans, n % d)
	n -= 5 * e
print(ans)


import sys
n = int(input())
d = int(input())
e = int(input())
e = e * 5
if e == d:
	print(n % d)
	sys.exit()
x = n // e
min = n
for i in range(0, x + 1):
	g = (n - e * i) % d
	if g < min:
		min = g
print(min)


n = int(input())
d = int(input())
e = int(input())
m = 100000001
h = n // (5 * e) * 5
while h >= 0:
	f = n - h * e
	if f >= d:
		f = f % d
	if f < m:
		m = f
	h -= 5
print(m)


(r, d, e) = (int(input()), int(input()), 5 * int(input()))
ans = r
i = 0
while i * e <= r:
	ans = min(ans, (r - i * e) % d)
	i += 1
print(ans)


n = int(input())
d = int(input())
e = int(input())
a = n % d
z = 5 * e
while n >= z:
	n -= z
	a = min(n % d, a)
print(a)


from math import *
from collections import *
from itertools import *
get = lambda : int(input())
getl = lambda : [int(x) for x in input().split()]

def main():
	n = get()
	d = get()
	e = get()
	e *= 5
	ans = 10 ** 9
	last = n // d + 1
	for i in range(last):
		ans = min(ans, (n - i * d) % e)
	print(ans)
main()


import sys
input = sys.stdin.readline
r = int(input())
d = int(input())
e = int(input())
ans = int(10000000000.0)
for i in range(0, r + 1, e * 5):
	ans = min(ans, (r - i) % d)
print(ans)


n = int(input())
d = int(input())
e = 5 * int(input())
ans = []
for i in range(n // e + 1):
	ans.append((n - e * i) % d)
print(min(ans))


import sys
import copy
from math import *
from collections import deque

def eprint(*args):
	print(*args, file=sys.stderr)
zz = 1
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output2.txt', 'w')
n = int(input())
a = int(input())
b = int(input())
b *= 5
if b > a:
	(a, b) = (b, a)
mini = n
while n >= 0:
	mini = min(mini, n % a)
	n -= b
print(mini)


n = int(input())
d = int(input())
e = int(input()) * 5
max_d = n // d
max_e = n // e
real_d = max_d
aux = n - real_d * d
real_e = 0
while aux >= e:
	aux -= e
	read_e += 1
rem = aux
for i in range(max_d):
	aux += d
	real_d -= 1
	while aux >= e:
		aux -= e
		real_e += 1
	rem = min(aux, rem)
print(rem)


source = input
amount = int(source())
price_dollar = int(source())
price_euro = int(source())

def get_answer(amount, p1, p2):
	best = amount
	for other in range(p1):
		a1 = amount - other * p2
		if a1 == 0:
			return 0
		if a1 < 0:
			break
		change = a1 % p1
		best = min(change, best)
	return best
print(get_answer(amount, price_dollar, price_euro * 5))


n = int(input())
d = int(input())
e = int(input()) * 5
(d, e) = (max(d, e), min(d, e))
v = n
for i in range(e):
	m = n - i * d
	if m < 0:
		break
	v = min(v, m % e)
print(v)


import math

class Read:

	@staticmethod
	def int():
		return int(input())

	@staticmethod
	def list(sep=' '):
		return input().split(sep)

	@staticmethod
	def list_int(sep=' '):
		return list(map(int, input().split(sep)))

def solve():
	r = Read.int()
	d = Read.int()
	e = Read.int() * 5
	m = math.inf
	for i in range(r // e + 1):
		t = (r - i * e) % d
		if t < m:
			m = t
		if m == 0:
			print(0)
			return
	print(m)
query_count = 1
for j in range(query_count):
	solve()


from collections import defaultdict as dc
import sys
import math

def inp():
	p = sys.stdin.readline()
	return int(p)

def value(n, x, y, a, b):
	return n - (x * a + y * b * 5)
n = inp()
p = inp()
q = inp()
a = n // p
b = n // (q * 5)
if b == 0:
	print(n - a * p)
else:
	ans = float('inf')
	for i in range(a, -1, -1):
		z = n - i * p
		l = z // (q * 5)
		ans = min(ans, n - i * p - l * q * 5)
	print(ans)


rs = int(input())
dol = int(input())
euro = int(input())
mindol = dol
mineuro = euro * 5
m = 0
minde = 0
mined = []
minde = rs % mindol
n = 0
while n <= rs // mineuro:
	k = rs - mineuro * n
	mined.append(k % mindol)
	n += 1
print(min(minde, min(mined)))


n = int(input())
a = int(input())
b = int(input())
b *= 5
(a, b) = (max(a, b), min(a, b))
Max = 0
for i in range(1 + n // a):
	j = (n - i * a) // b
	Max = max(Max, i * a + j * b)
print(n - Max)


n = int(input())
e = int(input())
d1 = int(input())
d = 5 * d1
n1 = n
while n1 >= 0:
	if n1 % d < n1 % e:
		if n1 >= d:
			n1 -= d
		else:
			break
	elif n1 >= e:
		n1 -= e
	else:
		break
n2 = n
while n2 >= 0:
	if n2 % e < n2 % d:
		if n1 >= e:
			n1 -= e
		else:
			break
	elif n1 >= d:
		n1 -= d
	else:
		break
print(min(n1, n2))


N = int(input())
d = int(input())
e = int(input())
e *= 5
ans = N
for i in range(10 ** 8):
	if d * i > N:
		break
	tmp = N - d * i
	ans = min(tmp % e, ans)
print(ans)


N = int(input())
d = int(input())
e = int(input())
e *= 5
c = e
ats = N % d
while c <= N:
	ats = min(ats, (N - c) % d)
	c += e
print(ats)


n = int(input())
d = int(input())
e = int(input()) * 5
while n >= e or n >= d:
	if n % d < n % e:
		n -= d
	else:
		n -= e
print(n)


n = int(input())
d = int(input())
e = int(input())
e = 5 * e
ans = 10 ** 18
for i in range(0, n // e + 1):
	ans = max(0, min(ans, n - i * e - (n - i * e) // d * d))
print(ans)


n = int(input())
d = int(input())
e = int(input())
td = d * 1
te = e * 5
flg = 1
mini = n
i = 0
while flg:
	if i >= 1 and te * i <= n:
		no = n - te * i
		rem = no % td
		i += 1
		if rem < mini:
			mini = rem
	elif te * i > n:
		flg = 0
	else:
		no = n
		rem = no % td
		if rem < mini:
			mini = rem
		i += 1
print(mini)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from functools import cmp_to_key
from heapq import *
from math import gcd, factorial, floor, ceil
from copy import deepcopy
from collections import deque
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for pp in range(t):
		solve(pp)

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def google(p):
	print('Case #' + str(p) + ': ', end='')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
inf = pow(10, 20)
mod = 10 ** 9 + 7

def solve(case):
	n = int(inp())
	d = int(inp())
	c = int(inp())
	temp = c * 5
	ans = inf
	taken = 0
	while taken * temp <= n:
		tot = temp * taken
		ans = min(ans, (n - tot) % d)
		taken += 1
	print(ans)
testcase(1)


n = int(input())
d = int(input())
e = int(input()) * 5
if d < e:
	(d, e) = (e, d)
dol = n // d
ost = n % d
eur = ost // e
ost = ost % e
min_ost = ost
while dol > 0:
	dol = dol - 1
	ost = ost + d
	eur = eur + ost // e
	ost = ost % e
	min_ost = min(ost, min_ost)
print(min_ost)


import sys
import math
get_string = lambda : sys.stdin.readline().strip()
get_list = lambda : list(map(int, sys.stdin.readline().strip().split()))
get_int = lambda : int(sys.stdin.readline())
n = get_int()
d = get_int()
e = get_int()
ans = n
i = 0
while 5 * i * e <= n:
	ans = min(ans, (n - 5 * i * e) % d)
	i += 1
print(ans)


import math
elem = int(input())
dollar = int(input())
euro = int(input()) * 5
min = elem % dollar
if elem % euro < min:
	min = elem % euro
if min != 0:
	while True:
		elem = elem - euro
		if elem < 0:
			break
		if elem % dollar < min:
			min = elem % dollar
print(min)


n = int(input())
d = int(input())
e = int(input())
k = 10 ** 6
z = float('inf')
for i in range(k + 1):
	if n < i * d:
		break
	r = (n - i * d) % (e * 5)
	z = min(z, r)
for j in range(k + 1):
	if n < i * e * 5:
		break
	r = (n - i * e * 5) % d
	z = min(z, r)
print(z)


def do():
	(r, d, e) = (int(input()), int(input()), int(input()))
	e *= 5
	nd = 0
	res = r
	while d * nd <= r:
		ne = (r - d * nd) // e
		res = min(res, r - d * nd - e * ne)
		nd += 1
	return res
print(do())


n = int(input())
d = int(input())
e = int(input())
dollar = 1 * d
euro = 5 * e
ans = 1000000000000000.0
temp = 0
while temp <= n:
	ans = min(ans, (n - temp) % dollar)
	temp += euro
print(ans)


import math
n = int(input())
d = int(input())
e = int(input())
l = [1, 2, 5, 10, 20, 50, 100]
r = [5, 10, 20, 50, 100, 200]
l = [i * d for i in l]
r = [i * e for i in r]
total = 0
for i in range(n // l[0] + 1):
	t = (n - i * l[0]) // r[0] * r[0] + i * l[0]
	total = max(total, t)
print(n - total)


n = int(input())
d = int(input())
e = int(input())
e = 5 * e
ans = int(100000000)
for i in range((n + d) // d):
	r = n - i * d - e * ((n - i * d) // e)
	ans = min(ans, r)
if ans == 100000000:
	ans = n
print(ans)


n = int(input())
d = int(input())
e = int(input())
check = e * 5
ans = n
l = n % d
while ans >= check:
	if (ans - check) % d < l:
		l = (ans - check) % d
	ans = ans - check
print(l)


import os, sys
from io import IOBase, BytesIO
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys

class ostream:

	def __lshift__(self, a):
		sys.stdout.write(str(a))
		return self
cout = ostream()
endl = '\n'

def solution():
	n = int(input())
	d = int(input())
	e = int(input())
	i = 0
	ans = 2000000000.0
	while i * 5 * e <= n:
		ans = min(ans, (n - i * 5 * e) % d)
		i += 1
	print(ans)
t = 1
for _ in range(t):
	solution()


n = int(input())
d = int(input())
e = int(input())
d1 = 1
e1 = 5
mv = n % (e1 * e)
for i in range(n, -1, -d):
	v = i % (e1 * e)
	mv = min(mv, v)
print(mv)


def main():
	(n, d, e) = (int(input()) for _ in 'nde')
	e *= 5
	if n > e:
		(d, e) = (e, d)
	(ff, res) = ([False] * d, [])
	for r in range(n, -1, -e):
		r %= d
		if ff[r]:
			break
		ff[r] = True
	print(min((r for (r, f) in enumerate(ff) if f)))
main()


n = int(input())
d = int(input())
e = int(input()) * 5
ans = 10 ** 8 + 1
for i in range(d):
	k = n - e * i
	if k < 0:
		break
	ans = min(ans, k % d)
print(ans)


from sys import stdin

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

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

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

def si():
	return stdin.readline()
p = r = ii()
d = ii()
e = ii() * 5
x = r // e + 1
for _ in range(x):
	p = min(p, r % d)
	r -= e
print(p)


(n, d, e) = (int(input()), int(input()), int(input()))
f = 5 * e
k = 0
mini = 99999999
while k <= n:
	x = k
	j = (n - k) // d * d
	if mini > n - (x + j):
		mini = n - (x + j)
	k += f
	if mini == 0:
		break
print(mini)


r = int(input())
d = int(input())
e = int(input())
e = 5 * e
ans1 = 10 ** 10
ans2 = 10 ** 10
for i in range(1, 10 ** 8 + 1):
	if r - d * i < 0:
		break
	else:
		a = r - d * i
		a1 = a // e
		ans = a - a1 * e
	ans1 = min(ans1, ans)
for i in range(1, 10 ** 8 + 1):
	if r - e * i < 0:
		break
	else:
		a = r - e * i
		a1 = a // d
		ans = a - a1 * d
	ans2 = min(ans2, ans)
print(min(ans1, ans2, r))


ruble = int(input())
usdrur = int(input())
eurrur = int(input())
ans = []
for i in range(ruble // (5 * eurrur) + 1):
	ans += [(ruble - i * 5 * eurrur) % usdrur]
print(min(ans))


I0 = lambda : map(int, input().split())
I1 = lambda : int(input())
I2 = lambda : list(map(int, input().split()))
n = I1()
d = I1()
e = I1()
e *= 5
M = float('inf')
j = n // e
for _ in range(j + 1):
	yy = n - _ * e
	yy -= yy // d * d
	M = min(M, yy)
print(M)


n = int(input())
d = int(input())
e = int(input())
if n % d == 0 or n % (e * 5) == 0:
	print(0)
else:
	da = [1, 2, 5, 10, 20, 50, 100]
	ea = [5, 10, 20, 50, 100, 200]
	e1 = e * 5
	b = n // e1
	a = 0
	rublesrem = n % d
	for i in range(1, b + 1, 1):
		temp = n - i * e1
		a = temp % d
		rublesrem = min(rublesrem, a)
	print(rublesrem)


n = int(input())
d = int(input())
e = int(input())
e *= 5
if e > d:
	tmp = e
	tmp2 = d
else:
	tmp = d
	tmp2 = e
a = n % tmp
min_a = a % tmp2
b = n // tmp
if b < tmp:
	minb = b
else:
	minb = tmp
for i in range(1, minb):
	a += tmp
	if min_a > a % tmp2:
		min_a = a % tmp2
if n % d > min_a:
	print(min_a)
else:
	print(n % d)


n = int(input())
d = int(input())
e = int(input())
db = n // d
eb = n // e
mn = n
for i in range(0, db + 1):
	z = n - i * d
	rem = z % (5 * e)
	if rem < mn:
		mn = rem
print(mn)


rubles = int(input())
val_d = int(input())
val_e = int(input())
minD = val_d
minE = val_e * 5
curr1 = max(minD, minE)
curr2 = min(minD, minE)
nCurr1 = 0
while rubles >= 0:
	rubles -= curr1
	nCurr1 += 1
rubles += curr1
nCurr1 -= 1
nCurr2 = 0
while rubles >= 0:
	rubles -= curr2
	nCurr2 += 1
rubles += curr2
nCurr2 -= 1
minRubles = rubles
while nCurr1 > 0:
	nCurr1 -= 1
	rubles += curr1
	while rubles >= 0:
		rubles -= curr2
		nCurr2 += 1
	rubles += curr2
	nCurr2 -= 1
	if rubles <= minRubles:
		minRubles = rubles
print(minRubles)


n = int(input())
d = int(input())
e = int(input())
dif = 10 ** 10
i = 0
while i * 5 * e <= n:
	dif = min(dif, (n - i * 5 * e) % d)
	i += 1
print(dif)


import math
import sys
from collections import defaultdict
rt = lambda : map(int, input().split())
ri = lambda : int(input())
rl = lambda : list(map(int, input().split()))

def main():
	n = ri()
	e = ri()
	d = ri() * 5
	res = math.inf
	for i in range(n // d + 1):
		res = min(res, n - i * d - (n - i * d) // e * e)
	print(res)
main()


n = int(input())
d = int(input())
e = int(input())
a = e * 5
m = 10000000
for i in range(min(100, n // a + 1)):
	if (n - a * i) % d < m:
		m = (n - a * i) % d
print(m)


def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a
n = int(input())
d = int(input())
e = int(input())
e = 5 * e
min = 100000000.0
m = n // e + 1
for i in range(0, m):
	x = n - e * i
	x = x % d
	if x < min:
		min = x
print(min)


(n, d, e) = (int(input()) for _ in range(3))
e *= 5
minleft = d - 1
for x in range(n // e + 1):
	if x * e > n:
		break
	left = (n - x * e) % d
	minleft = min(left, minleft)
print(minleft)


class cinn:

	def __init__(self):
		self.x = []

	def cin(self, t=int):
		if len(self.x) == 0:
			a = input()
			self.x = a.split()
			self.x.reverse()
		return self.get(t)

	def get(self, t):
		return t(self.x.pop())

	def clist(self, n, t=int):
		l = [0] * n
		for i in range(n):
			l[i] = self.cin(t)
		return l

	def clist2(self, n, t1=int, t2=int, t3=int, tn=2):
		l = [0] * n
		for i in range(n):
			if tn == 2:
				a1 = self.cin(t1)
				a2 = self.cin(t2)
				l[i] = (a1, a2)
			elif tn == 3:
				a1 = self.cin(t1)
				a2 = self.cin(t2)
				a3 = self.cin(t3)
				l[i] = (a1, a2, a3)
		return l

	def clist3(self, n, t1=int, t2=int, t3=int):
		return self.clist2(self, n, t1, t2, t3, 3)

	def cout(self, i, ans=''):
		if ans == '':
			print('Case #' + str(i + 1) + ':', end=' ')
		else:
			print('Case #' + str(i + 1) + ':', ans)

	def printf(self, thing):
		print(thing, end='')

	def countlist(self, l, s=0, e=None):
		if e == None:
			e = len(l)
		dic = {}
		for el in range(s, e):
			if l[el] not in dic:
				dic[l[el]] = 1
			else:
				dic[l[el]] += 1
		return dic

	def talk(self, x):
		print(x, flush=True)

	def dp1(self, k):
		L = [-1] * k
		return L

	def dp2(self, k, kk):
		L = [-1] * k
		for i in range(k):
			L[i] = [-1] * kk
		return L

	def isprime(self, n):
		if n == 1 or n == 0:
			return False
		for i in range(2, int(n ** 0.5 + 1)):
			if n % i == 0:
				return False
		return True

	def factors(self, n):
		from functools import reduce
		return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

	def nthprime(self, n):
		i = 0
		s = 2
		L = []
		while i < n:
			while not self.isprime(s):
				s += 1
			L.append(s)
			s += 1
			i += 1
		return L

	def matrixin(self, m, n, t=int):
		L = []
		for i in range(m):
			p = self.clist(n, t)
			L.append(p)
		return L

	def seive(self, k):
		n = k + 1
		L = [True] * n
		L[1] = False
		L[0] = False
		for i in range(2, n):
			if L[i] == True:
				for j in range(2 * i, n, i):
					L[j] = False
		return L

	def seiven(self, n, L):
		i = 0
		for j in range(len(L)):
			if L[j] == True:
				i += 1
			if i == n:
				return j

	def matrixin2(self, m, t=int):
		L = []
		for i in range(m):
			iny = self.cin(str)
			lsmall = []
			for el in iny:
				lsmall.append(t(el))
			L.append(lsmall)
		return L
c = cinn()
n = c.cin()
dol = c.cin()
eu = c.cin()
mini = 100000000000000000000000000
tempn = n
for i in range(0, dol * eu + 1):
	tempn = n
	tempn -= i * dol
	if tempn < 0:
		break
	mini = min(mini, tempn % (eu * 5))
print(mini)


n = int(input())
d = int(input())
e = int(input())
m = n
i = 0
while n - 5 * e * i >= 0:
	if m > (n - 5 * e * i) % d:
		m = (n - 5 * e * i) % d
	i += 1
print(m)


n = int(input())
d = int(input())
e = int(input())
e = 5 * e
curr = 0
mini = n
while curr <= n:
	if (n - curr) % e < mini:
		mini = (n - curr) % e
	curr += d
print(mini)


(n, d, e) = (int(input()), int(input()), 5 * int(input()))
ost_d = n // d
i = 0
min_count = n % d
while i <= ost_d:
	k = (n - i * d) % e
	if min_count > k:
		min_count = k
	i += 1
if n % d == 0 or n % e == 0:
	print(0)
else:
	print(min_count)


n = int(input())
d = int(input())
e = int(input())
e1 = 5 * e
a = n // d
b = n // e1
res = min(n % e1 % d, n % d % e1)
for i in range(b):
	r = (n - (b - i - 1) * e1) % d
	res = min(res, r)
print(res)


(rub, usd, eur) = [int(input()) for _ in range(3)]
if rub % usd == 0:
	print(0)
elif rub % eur == 0 and rub // eur % 5 == 0:
	print(0)
else:
	min_balance = rub % usd
	for i in range(1, 1 + rub // eur // 5):
		balance = (rub - eur * i * 5) % usd
		if balance < min_balance:
			min_balance = balance
	print(min_balance)


from math import *
from collections import deque
from copy import deepcopy
import sys

def inp():
	return sys.stdin.readline().rstrip('\r\n')

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

def strmulti():
	return map(str, inp().split())

def lis():
	return list(map(int, inp().split()))

def lcm(a, b):
	return a * b // gcd(a, b)

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def stringlis():
	return list(map(str, inp().split()))

def out(var):
	sys.stdout.write(str(var))

def printlist(a):
	print(' '.join((str(a[i]) for i in range(len(a)))))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
n = int(inp())
d = int(inp())
e = int(inp())
ans = n
i = 0
while e * 5 * i <= n:
	ans = min(ans, (n - i * 5 * e) % d)
	i += 1
print(ans)


n = int(input())
d = int(input())
e = int(input())
e = 5 * e
ans = float('inf')
for i in range(0, n + 1):
	rem1 = i - i // d * d
	j = n - i + rem1
	rem2 = j - j // e * e
	ans = min(rem2, ans)
print(ans)


n = int(input())
e = int(input())
d = 5 * int(input())
minRemainder = n
maximumDollars = n // d
for i in range(maximumDollars + 1):
	newRemain = (n - i * d) % e
	minRemainder = min(minRemainder, newRemain)
print(minRemainder)


(total, usd, euro) = (int(input()), int(input()), int(input()))
out = total % usd
euro_div = 5 * euro
while euro_div <= total:
	out = min(out, (total - euro_div) % usd)
	if out == 0:
		break
	euro_div += 5 * euro
print(out)


n = int(input())
d = int(input())
e = int(input())
i = 0
fin = n
while i * 5 * e <= n:
	left = (n - i * 5 * e) % d
	fin = min(fin, left)
	i += 1
print(fin)


def start_program():
	(rubles, d_e, e_e) = (int(input()), int(input()), int(input()))
	(ans, i) = (rubles, 0)
	while i * 5 * e_e <= rubles:
		ans = min(ans, (rubles - i * 5 * e_e) % d_e)
		i += 1
	print(ans)
start_program()


n = int(input())
d = int(input())
e = int(input()) * 5
r = n
for i in range(d):
	if i * e > n:
		continue
	p = n - i * e - (n - i * e) // d * d
	r = min(r, p)
print(r)


n = int(input())
d = int(input())
e = int(input())
k = n % d
while n >= 5 * e:
	n -= 5 * e
	k = min(k, n % d)
print(k)


n = int(input())
a = int(input())
b = int(input())
c = n
for i in range(a + 1):
	if b * 5 * i <= n:
		c = min(c, (n - b * 5 * i) % a)
print(c)


n = int(input())
dd = int(input())
ee = int(input()) * 5
ans = 1 << 50
for eva in range(0, n + 1, ee):
	nn = n - eva
	nn = nn % dd
	ans = min(nn, ans)
print(ans)


n = int(input())
a = int(input())
b = int(input())
ans = n % a
while n >= b * 5:
	n -= b * 5
	ans = min(n % a, ans)
print(ans)


n = int(input())
d = int(input())
e = int(input())
answer = n % d
while n >= 5 * e:
	n -= 5 * e
	answer = min(answer, n % d)
print(answer)


from sys import stdin
n = int(stdin.readline().strip())
d = int(stdin.readline().strip())
e = int(stdin.readline().strip())
x = n // 5
ans = n
for j in range(x + 2):
	y = e * j * 5
	z = n - y
	if z < 0:
		break
	z1 = z // d
	z = z - z1 * d
	ans = min(ans, z)
print(ans)


n = int(input())
d = int(input())
e = int(input())
p1 = 1 * d
p2 = 5 * e
left = n
for i in range(0, n // p2 + 1):
	left = min(left, (n - i * p2) % p1)
print(left)


r = int(input())
d = int(input())
e = int(input())
ans = r % d
k = 1
while k * 5 * e <= r:
	ans = min(ans, (r - 5 * e * k) % d)
	k += 1
print(ans)


(n, d, e) = [int(input()) for _ in range(3)]
ans = float('inf')
for dollar in range(n // d + 2):
	if dollar * d > n:
		continue
	ans = min(ans, (n - dollar * d) % (e * 5))
print(ans)


import math
n = int(input())
d = int(input()) * 1
e = int(input()) * 5
imax = math.floor(n / d)
mi = n
for i in range(0, imax + 1):
	if n - i * d >= 0:
		j = math.floor((n - i * d) / e)
		mi = min(mi, n - i * d - j * e)
print(mi)


import os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
d = int(input())
e = int(input())
dv = 1 * d
ev = 5 * e
mv = n % dv
for i in range(n, -1, -ev):
	v = i % dv
	mv = min(mv, v)
print(mv)


n = int(input())
d = int(input())
e = int(input())
mn = 99999999999
while n >= 0:
	res = n % d
	if res < mn:
		mn = res
	n = n - 5 * e
print(mn)


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

def fi():
	return float(input())

def si():
	return input()

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

def li():
	return list(mi())
n = ii()
d = ii()
e = ii()
ans = n % d
while n >= 5 * e:
	n -= e * 5
	ans = min(ans, n % d)
print(ans)


n = int(input())
d = int(input())
e = int(input())
e = 5 * e
a = n // d
b = 0
mm = n - a * d
while a > 0 and mm != 0:
	a -= 1
	while n - a * d - (b + 1) * e >= 0:
		b += 1
		if mm > n - a * d - b * e:
			mm = n - a * d - b * e
print(mm)


n = int(input())
d = int(input())
e = int(input())
num_euros = 0
ans = n
while num_euros * 5 * e <= n:
	ans = min(ans, (n - num_euros * 5 * e) % d)
	num_euros += 1
print(ans)


rub = int(input())
dol = int(input())
eur = int(input())
cnt = rub
i = 0
while i * 5 * eur <= rub:
	cnt = min(cnt, (rub - i * eur * 5) % dol)
	i += 1
print(cnt)


rubes = int(input())
d = int(input())
e = int(input()) * 5
ans = rubes
for i in range(rubes // e + 1):
	ans = min(ans, (rubes - i * e) % d)
print(ans)


from collections import defaultdict, deque
from heapq import heappush, heappop
import sys
import math
import bisect

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

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

def LS():
	return [list(x) for x in sys.stdin.readline().split()]

def S():
	res = list(sys.stdin.readline())
	if res[-1] == '\n':
		return res[:-1]
	return res

def IR(n):
	return [I() for i in range(n)]

def LIR(n):
	return [LI() for i in range(n)]

def SR(n):
	return [S() for i in range(n)]

def LSR(n):
	return [LS() for i in range(n)]
sys.setrecursionlimit(1000000)
mod = 1000000007

def A():
	(n, d, e) = IR(3)
	e *= 5
	ans = n
	m = n // e
	for i in range(m + 1):
		ans = min(ans, n - d * (n // d))
		n -= e
	print(ans)
	return

def B():
	return

def C():
	return

def D():
	return

def E():
	return

def F():
	return

def G():
	return

def H():
	return
A()


n = int(input())
d = int(input())
e = int(input()) * 5
q = n // e
ans = n
for i in range(q + 1):
	h = (n - i * e) % d
	ans = min(ans, h)
print(ans)


n = int(input())
d = int(input())
e = int(input())
ne = n // e
mi = 100000001
su = 0
e = e * 5
for i in range(ne + 1):
	Dd = n - e * i
	if Dd >= 0:
		nd = Dd // d
		su = nd * d + i * e
	if su > 0:
		if n % su < mi:
			mi = n % su
if mi != 100000001:
	print(mi)
else:
	print(n)


n = int(input())
d = int(input())
e = int(input())
ans = min(n % d, n % (5 * e))
if d < 5 * e:
	T = int(n / (5 * e))
	c = 5 * e
	c2 = d
else:
	T = int(n / d)
	c = d
	c2 = 5 * e
for i in range(T + 1):
	ans = min(ans, n - i * c - c2 * int((n - i * c) / c2))
print(ans)


n = int(input())
d = int(input())
e = 5 * int(input())
min_r = min(n % e, n % d)
for i in range(n // d):
	if (n - i * d) % e < min_r:
		min_r = (n - i * d) % e
for i in range(n // e):
	if (n - i * e) % d < min_r:
		min_r = (n - i * e) % d
print(min_r)


n = int(input())
d = int(input())
e = int(input())
dv = 1 * d
ev = 5 * e
mv = n % dv
for i in range(n, -1, -ev):
	v = i % dv
	mv = min(mv, v)
print(mv)


n = int(input())
d = int(input())
e = int(input())
e = e * 5
c = e
p = n % d
while c <= n:
	p = min(p, (n - c) % d)
	c += e
print(p)


ii = lambda : int(input())
mi = lambda : map(int, input().split())
li = lambda : list(mi())
si = lambda : input()
msi = lambda : map(int, stdin.readline().split())
lsi = lambda : list(msi())
(n, d, e) = (ii(), ii(), ii())
i = 0
ans = n
while i * e * 5 <= n:
	ans = min(ans, (n - i * e * 5) % d)
	i += 1
print(ans)


n = int(input())
d = int(input())
e = int(input())
ans = n
for a in range(0, n // d + 1):
	b = (n - a * d) // (5 * e)
	ans = min(ans, n - (a * d + b * e * 5))
print(ans)


n = int(input())
d = int(input())
e = int(input())
arr = 100
(a, b) = (min(d, 5 * e), max(d, 5 * e))
for i in range(0, n // b + 1):
	arr = min(arr, (n - i * b) % a)
print(arr)


n = int(input())
d = int(input())
e = int(input()) * 5
m = 100
euro = 0
while euro <= n:
	m = min(m, n - euro - (n - euro) // d * d)
	euro += e
print(m)


def pgcd(a, b):
	if b == 0:
		return a
	else:
		r = a % b
		return pgcd(b, r)

def ppcm(a, b):
	if a == 0 or b == 0:
		return 0
	else:
		return a * b // pgcd(a, b)
n = int(input())
d = int(input())
e = int(input())
n = n
m = n
n1 = n
while n > 0 and n > min(5 * e, d) and (m > 0):
	if m > n % d:
		m = n % d
	n -= 5 * e
	if n < 5 * e:
		while n > 0 and n > d:
			if n > d:
				n -= d
			elif m > n:
				m = n
while n1 > 0 and n1 > min(5 * e, d) and (m > 0):
	if m > n1 % (5 * e):
		m = n1 % (5 * e)
	n1 -= d
	if n1 < d:
		while n1 > 0 and n1 > 5 * e:
			if n1 > 5 * e:
				n1 -= 5 * e
			elif m > n1:
				m = n1
print(m)


n = int(input())
d = int(input())
e = int(input())
min_ost = n
for i in range(n // (5 * e) + 1):
	temp = n - i * 5 * e
	if temp >= 0:
		min_ost = min(min_ost, temp - temp // d * d)
print(min_ost)


from bisect import bisect_left
n = int(input())
d = int(input())
e = int(input())
ans = []
x = [d, e * 5]
for i in range(n // min(x) + 1):
	o = n
	if o - i * x[0] >= 0:
		o = o - i * x[0]
	no = o // x[1]
	o = o - no * x[1]
	ans.append(o)
print(min(ans))


n = int(input())
d = int(input())
e = int(input()) * 5
b = n
l = []
while b % d != 0:
	l.append(b % d)
	if b >= e:
		b = b - e
	else:
		break
l.append(b % d)
print(min(l))


n = int(input())
d = int(input())
e = int(input())
e *= 5
t = max(d, e)
m = min(d, e)
ans = t
for i in range(n // t + 1):
	tmp = n - i * t
	tmp %= m
	ans = min(tmp, ans)
print(ans)


n = int(input())
d = int(input())
e = int(input())
e = e * 5
edge = n // e
r = min(n % e, n % d)
while edge != 0:
	x = n - e * edge
	r = min(x % d, r)
	edge -= 1
print(r)


import sys
import math
input = sys.stdin.readline
rubel = int(input())
dol = int(input())
eur = int(input())
eur = eur * 5
if dol < eur:
	temp = dol
	dol = eur
	eur = temp
maxi = rubel // dol
mini = 1000000000
for i in range(maxi + 1):
	curr = i * dol
	curr = curr + (rubel - curr) // eur * eur
	if rubel - curr < mini:
		mini = rubel - curr
print(mini)


n = int(input())
d = int(input())
e = int(input())
a = n % d
x = a
k = n // d
for _ in range(k):
	if a + d <= n:
		a += d
		if a % (5 * e) < x:
			x = a % (5 * e)
	else:
		break
print(x)


from math import *
n = int(input())
d = int(input())
e = int(input())
minn = n
for i in range(floor(n / d + 1)):
	minn = min(minn, n - d * i - floor((n - d * i) / (5 * e)) * 5 * e)
print(minn)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import gcd, ceil

def pre(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi

def prod(a):
	ans = 1
	for each in a:
		ans = ans * each
	return ans

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
for _ in range(int(input()) if not True else 1):
	n = int(input())
	d = int(input())
	e = int(input())
	ans = n
	i = 0
	while True:
		if i * d > n:
			break
		left = n - i * d - 5 * e * ((n - i * d) // (5 * e))
		ans = min(ans, left)
		i += 1
	print(ans)


input = __import__('sys').stdin.readline
print = __import__('sys').stdout.write
n = int(input())
d = int(input())
e = int(input())
do = d
eu = e * 5
k = True
if do > eu:
	r = n // do
else:
	k = False
	r = n // eu
mmmm = n
if k:
	for i in range(0, r + 1):
		tmp_n = n
		tmp_n -= do * i
		mmmm = min(mmmm, tmp_n % eu)
else:
	for i in range(0, r + 1):
		tmp_n = n
		tmp_n -= eu * i
		mmmm = min(mmmm, tmp_n % do)
print(str(mmmm))


def main():
	v = int(input())
	d = int(input())
	e = int(input())
	dollars = [1, 2, 5, 10, 20, 50, 100]
	euros = [5, 10, 20, 50, 100, 200]
	mins = {v}
	for i in range(1, v // d + 1):
		v1 = v - i * d
		v1 = v1 - v1 // (e * 5) * e * 5
		mins.add(v1)
	for i in range(1, v // (e * 5) + 1):
		v2 = v - e * 5 * i
		v2 = v2 - v2 // d * d
		mins.add(v2)
	print(min(mins))
main()


n = int(input())
d = int(input())
e = int(input())
if n % (5 * e) == 0 or n % d == 0:
	print(0)
else:
	minimum = n
	for i in range(0, n, 5 * e):
		r = (n - i) % d
		if minimum > r:
			minimum = r
	print(minimum)


n = int(input())
d = int(input())
e = int(input())
s = n
i = 0
x = 5 * e
while i * x <= n:
	s = min(s, (n - i * x) % d)
	i += 1
print(s)


n = int(input())
d = int(input())
e = int(input())
maxdol = n // d
remainder = n % d
ans = remainder
for i in range(min(50002, maxdol)):
	remainder += d
	if remainder >= 5 * e:
		remainder -= 5 * e
	ans = min(ans, remainder)
print(ans)