x = int(eval(input()))



ans = 0

ans += x//500 * 1000

x %= 500

ans += x//5 * 5

print(ans)
x = int(eval(input()))



c500 = x//500

x %= 500

c5 = x//5

ans = c500*1000 + c5*5

print(ans)
x = int(eval(input()))
ans = 0
ans += x // 500 * 1000
x %= 500
ans += x // 5 * 5
print(ans)

x = int(eval(input()))
c500 = x // 500
x %= 500
c5 = x // 5
ans = c500 * 1000 + c5 * 5
print(ans)

-ans = 0 -ans += x // 500 * 1000 +c500 = x // 500 -ans += x // 5 * 5 +c5 = x // 5 +ans = c500 * 1000 + c5 * 5