
exchange :: Int -> [Int] -> [Int]
exchange amount coins | amount >= 500 = exchange (amount - 500) (500 : coins)
                      | amount >= 5   = exchange (amount - 5) (5 : coins)
                      | amount > 0    = exchange (amount - 5) (1 : coins)
                      | otherwise     = coins

totalValue :: [Int] -> [Int]
totalValue coins = map value coins

value :: Int -> Int
value 500 = 1000
value 5   = 5
value _   = 0

main = do
    str <- getLine
    let num = read str :: Int
    print $ sum $ totalValue $ exchange num []
