x=int(eval(input()))

c=x//500

d=x%500

e=d//5

print((1000*c+e*5))
x=int(eval(input()))

a=x//500

b=x%500

c=b//5

print((1000*a+5*c))
x = int(eval(input()))
c = x // 500
d = x % 500
e = d // 5
print((1000 * c + e * 5))

x = int(eval(input()))
a = x // 500
b = x % 500
c = b // 5
print((1000 * a + 5 * c))

-c = x // 500 -d = x % 500 -e = d // 5 -print((1000 * c + e * 5)) +a = x // 500 +b = x % 500 +c = b // 5 +print((1000 * a + 5 * c))