import Control.Monad (replicateM)

main = do
    n <- readLn
    if n == 0 then return () else do
        print $ solve n
        main

solve :: Int -> Int
solve n = counts [500,100,50,10,5,1] (1000-n)
    where
    counts [] _ = 0
    counts (c:cs) r = (r `div` c) + counts cs (r `mod` c)