import Control.Applicative
import Control.Monad

change :: Int -> Int -> Int
change m t
  | m >= 500  = change (m `mod` 500) (t + (m `div` 500))
  | m >= 100  = change (m `mod` 100) (t + (m `div` 100))
  | m >= 50   = change (m `mod` 50) (t + (m `div` 50))
  | m >= 10   = change (m `mod` 10) (t + (m `div` 10))
  | m >= 5    = change (m `mod` 5) (t + (m `div` 5))
  | m >= 1    = t + m
  | otherwise = t

main :: IO()
main = do
  line <- (read :: String -> Int) <$> getLine
  unless (line == 0) $
    do print $ change (1000 - line) 0
       main