c=[]
ans=[]

i=0
while a[i]>=0 do
c[i]=gets.chomp.to_i
i=i+1
break a[i]=0
end

for k in 0..c.length-1
  if c[k]==0
     ans[k]="\n"
  else
    s1=(1000-c[k])/500
    r1=(1000-c[k])%500
    s2=r1/100
    r2=r1%100
    s3=r2/50
    r3=r2%50
    s4=r3/10
    r4=r3%10
    s5=r4/5
    r5=r4%5

    ans[i]=s1+s2+s3+s4+s5+r5
  end
end

puts ans