def calcCoin(num, price, coin)
    if price >= coin then
        num += price/coin
        price -= coin * (price/coin)
    end
    return num, price
end
loop do
    x = gets.to_i
    break if x == 0
    num = 0
    y = 1000 - x
    [500,100,50,10,5,1].each do |coin|
        num, y = calcCoin(num, y, coin)
    end
    puts num
end
