calChange :: [Int] -> Int -> Int
calChange [] _ = 0
calChange _ 0 = 0
calChange (c:t) p = p `div` c + calChange t (p `mod` c)

main::IO()
main = do
  price <- fmap ((1000 -) .read) getLine :: IO Int
  if price == 1000 then return ()
  else do print(calChange [500, 100, 50, 10, 5, 1] price)
          main

