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

data = []
while (input = $stdin.gets.to_i) > 0 do
  data << input
end

data.each do |n|
  count = 0
  i = 0
  total = 1000 - n
  while total > 0 do
    if total - coin[i] >= 0
      total -= coin[i]
      count += 1
    else
      i += 1
    end
  end
  puts count
end