x = int(eval(input()))

n = x//500

x -= 500*n

m = x//5

print((1000*n+5*m))
X = int(eval(input()))

n = X//500

m = X%500

n1 = m//5

print((n*1000+n1*5))
x = int(eval(input()))
n = x // 500
x -= 500 * n
m = x // 5
print((1000 * n + 5 * m))

X = int(eval(input()))
n = X // 500
m = X % 500
n1 = m // 5
print((n * 1000 + n1 * 5))

-x = int(eval(input())) -n = x // 500 -x -= 500 * n -m = x // 5 -print((1000 * n + 5 * m)) +X = int(eval(input())) +n = X // 500 +m = X % 500 +n1 = m // 5 +print((n * 1000 + n1 * 5))