x = []
y = []
x[0] = gets.chomp.to_i
i = 1
while x[i-1] != 0
  x[i] = gets.chomp.to_i
  i = i+1
end
i = i-1
x.delete_at(i)
i = i-1

for j in 0..i
 change = 1000-x[j] 
 y[j] = 0
   if change >= 500
      y[j] = y[j] + 1
      change = change-500
   end
   if change >= 100
      y[j] = y[j]+1
      change = change - 100
     while change >= 100
       y[j] = y[j] + 1
       change = change-100      
     end
     
   end
   if change >= 50
     y[j] = y[j]+1
     change = change-50
   end
   if change >= 10
      y[j] = y[j]+1
      change = change-10
     while change >= 10
       y[j] = y[j]+1
       change = change-10      
     end
   end
   if change >= 5
     y[j] = y[j]+1
     change = change-5
   end
   if change >= 1
      y[j] = y[j]+1
      change = change-1
     while change >= 1
       y[j] = y[j]+1
       change = change-1   
     end
   end
end
for k in 0..i
  puts y[k]
end 

