while true
    x = gets.to_i
    break if x == 0
    x = 1000 - x
    a, x = x.divmod 500
    b, x = x.divmod 100
    c, x = x.divmod 50
    d, x = x.divmod 10
    e, f = x.divmod 5
    puts a + b + c + d + e + f
end
