#encoding:utf-8 No.0521 2014.11.11
while (price = gets.chomp.to_i) != 0
  change = 1000 - price
  count = 0
  until change == 0
    if change >= 500
      change -= 500
      count += 1
    elsif change >= 100
      change -= 100
      count += 1
    elsif change >= 50
      change -= 50
      count += 1
    elsif change >= 10
      change -= 10
      count += 1
    elsif change >= 5
      change -= 5
      count += 1
    else
      count += change
      change = 0
    end
  end
  puts count
end