while n = gets
  n = n.chomp.to_i
  if n>0
    a=1000-n
    b=a/500
    c=(a%500)/100
    d=(a%500%100)/50
    e=(a%500%100%50)/10
    f=(a%500%100%50%10)/5
    g=(a%500%100%50%10%5)/1
    puts b+c+d+e+f+g
  end
end