loop do
  price = gets.chomp.to_i
  break if price == 0
  change = 1000 - price
  coins = [500, 100, 50, 10, 5, 1]
  coin_num = 0

  coins.each do |coin|
    coin_num += change / coin
    change = change % coin
  end

  puts coin_num
end