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

loop do
  n = gets.strip.to_i
  break if n == 0
  change = 1000 - n
  count = 0

  for coin in COINS
    count += change / coin
    change = change % coin
  end

  puts count
end
