### constant

COINS = [500, 100, 50, 10, 5, 1]

### main

loop do
  n = gets.strip.to_i
  break if n == 0

  change = 1000 - n
  total = 0

  for coin in COINS
    nc = change / coin
    change -= coin * nc
    total += nc
  end

  puts total
end