Go: ///
// File:  0521.go
// Author: ymiyamoto
//
// Created on Wed Mar  7 22:11:28 2018
//
package main

import (
	"fmt"
)

var money int

func main() {
	for {
		fmt.Scan(&money)
		if money == 0 {
			break
		}
		rem := 1000 - money

		count := 0
		for _, coin := range []int{500, 100, 50, 10, 5, 1} {
			count += rem / coin
			rem %= coin
		}
		fmt.Println(count)
	}
}

Python: while True:
    n = int(input())
    if n >= 1:
        x = 1000 - n
        a = x%500
        b = a%100
        c = b%50
        d = c%10
        e = d%5
        y = x//500 + a//100 + b//50 + c//10 + d//5 +e//1
        print(y)
    if n == 0:
        break

Python: import sys

coins = [1,5,10,50,100,500]

for i in sys.stdin:
    if int(i) == 0:
        break

    n =1000-int(i)
    c = [ i for i in range(n+1) ]
    for i in range(len(coins)):
        coin = coins[i]
        if coin <= n:
            c[coin] = 1
            coins.append(coin)

    for coin in coins:
        for p in range(coin+1, n+1):
            minc = c[p]
            if minc == 1:
                continue
            c[p] = c[p-coin]+1 if minc > c[p-coin] + 1 else minc

    print(c[n])

Python: while True:
    x=int(input())
    y=1000-x
    a=0
    b=0
    a+=y//500
    b=y%500
    a+=b//100
    b=b%100
    a+=b//50
    b=b%50
    a+=b//10
    b=b%10
    a+=b//5
    b=b%5
    a+=b//1
    if x==0:
        break
    print(int(a))

Python: #!/usr/bin/env python
#coding:utf-8

while True:
    coin = [500,100,50,10,5,1]
    oturi = 1000 - int(input())
    if oturi == 1000:
        break
    ret = 0
    for c in coin:
        ret += oturi // c
        oturi %= c
    print(ret)

Python: for e in iter(input,'0'):
 b,c=1000-int(e),0
 for i in[500,100,50,10,5]:
  d,b=b//i,b%i
  c+=d
 print(b+c)

Python: while True:
    n=int(input())
    count=0
    if n==0:
        break
    x=1000-n
    if x>=500:
        count+=1
        x-=500
    a=x//100
    count+=a
    if a>0 :
        x=x-a*100
    if x>=50 :
        count+=1
        x-=50
    b=x//10
    count+=b
    if b>0 :
        x=x-b*10
    if x>=5 :
        count+=1
        x-=5
    count+=x
    
    print(count)

Python: c, o= 0, 1000-int(input())
def change(v):
    global c, o
    if o>= v:
        t= o//v
        o, c= o-(v*t), c+t

for l in [500, 100, 50, 10, 5, 1]: change(l)
print(int(c+o))

Python: def main():
    N = 1000 - int(input())
    while N != 1000:
        ans = 0
        while N >= 500:
            ans += 1
            N -= 500
        while N >= 100:
            ans += 1
            N -= 100
        while N >= 50:
            ans += 1
            N -= 50
        while N >= 10:
            ans += 1
            N -= 10
        while N >= 5:
            ans += 1
            N -= 5
        while N >= 1:
            ans += 1
            N -= 1
        print(ans)
        N = 1000 - int(input())

if __name__ == '__main__':
    main()

Python: for e in iter(input,'0'):
 b,c=1000-int(e),0
 for i in[500,100,50,10,5]:
  d,b=divmod(b,i)
  c+=d
 print(b+c)

Python: coins = (500, 100, 50, 10, 5, 1)
while True:
    n = int(input())
    if not n:
        break
    m, c = 0, 1000 - n
    for coin in coins:
        mt, c = divmod(c, coin)
        m += mt
        if not c:
            break
    print(m)

Python: while True:
    m=int(input())
    p=1000-m
    y500=p//500
    x500=p%500
    y100=x500//100
    x100=x500%100
    y50=x100//50
    x50=x100%50
    y10=x50//10
    x10=x50%10
    y5=x10//5
    x5=x10%5
    if m==0:
        break
    print(y500+y100+y50+y10+y5+x5)

Python: c, o= 0, 1000-int(input())
if o>= 500:
    o, c= o-500, c+1
if o>= 100:
    t= o//100
    o, c= o-(100*t), c+t
if o>= 10:
    t= o//10
    o, c= o-(10*t), c+t
if o>= 5:
    t= o//5
    o, c= o-(5*t), c+t
print(c)

Python: while True:
    t = int(input())
    if t==0:
        break
    n = (1000-t)//500+((1000-t)%500)//100+((1000-t)%100)//50+((1000-t)%50)//10+((1000-t)%10)//5+((1000-t)%5)
    print(n)

Python: i = 1
while i <= 5:
    x = int(input())
    if x == 0:
        break
    y = 1000 - x
    c_1 = y//500
    c_2 = y%500//100
    c_3 = y%500%100//50
    c_4 = y%500%100%50//10
    c_5 = y%500%100%50%10//5
    c_6 = y%500%100%50%10%5//1
    print(c_1+c_2+c_3+c_4+c_5+c_6)

Python: a=0
while a<=1000:
    a=int(input())
    if a==0:
        break
    else:
        x=1000-a
        b=x//500#500coin
        c=x%500
        d=c//100#100coin
        e=c%100
        f=e//50
        g=e%50
        h=g//10#
        i=g%10
        j=i//5#
        k=i%5#
    print(b+d+f+h+j+k)

Python: while True:
  n = int(input())
  if not n: break
  ans = 0
  m = 1000 - n
  for i in [500,100,50,10,5,1]:
    ans += m // i
    m = m % i
  print(ans)

Python: while True:
    n=int(input())
    if n==0:
        break
    else:
        sum=0
        sum+=(1000-n)//500
        sum+=(1000-n)%500//100
        sum+=(1000-n)%500%100//50
        sum+=(1000-n)%500%100%50//10
        sum+=(1000-n)%500%100%50%10//5
        sum+=(1000-n)%500%100%50%10%5//1
        print(sum)

Python: i=0
l = [int(input()) for _ in range(5)]
while i<5:
    if(l[i]==0):
        break
    else:    
        a=1000-l[i]
        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
        print(b+c+d+e+f+g)
        i=i+1

Python: for e in iter(input,'0'):
 a,b=0,1000-int(e)
 for i in[500,100,50,10,5]:
  a+=b//i
  b%=i
 print(a+b)

Python: for i in range(1,5+1) :
    n = int(input())
    x = 1000-n
    if n == 0 :
        break
    
    else :
        a = x//500
        b = (x-500*a)//100
        c = (x-500*a-100*b)//50
        d = (x-500*a-100*b-50*c)//10
        e = (x-500*a-100*b-50*c-10*d)//5
        f = x-500*a-100*b-50*c-10*d-5*e
        print(a+b+c+d+e+f)

Python: while True:
    x=int(input())
    if x==0:
        break
    oturi=1000-x
    a=oturi//500
    b=oturi//100-a*5
    c=(oturi%100)//50
    d=(oturi%100)//10-c*5
    e=(oturi%10)//5
    f=(oturi%10)-e*5
    kazu=a+b+c+d+e+f
    print(kazu)

Python: # coding: utf-8
# Your code here!
while True:
   x = int(input())
   if x == 0:
      break
   
   y = 1000 - x;
   a = y % 500
   b = a % 100
   c = b % 50
   d = c % 10
   e = d % 5
   z = y // 500 + a // 100 + b // 50 + c // 10 + d // 5 + e
   
   print(z)

Python: while 1:
    n = int(input())
    if n == 0:
        break

    charge = 1000 - n
    cnt = 0
    if charge >= 500:
        cnt += 1
        charge -= 500
    cnt += charge // 100
    charge %= 100

    if charge >= 50:
        cnt += 1
        charge -= 50
    cnt += charge // 10
    charge %= 10

    if charge >= 5:
        cnt += 1
        charge -= 5
    cnt += charge

    print(cnt)

Python: x=1000
while True:
    y=int(input())
    if y>0:
        a=(x-y)//500
        b=(x-y)%500//100
        c=(x-y)%100//50
        d=(x-y)%50//10
        e=(x-y)%10//5
        f=(x-y)%5
        print(a+b+c+d+e+f)
    else:
        break

Python: while True:
    a=int(input())
    if a==0:
        break
    b=(1000-a)//500
    c=(1000-a)%500//100
    d=(1000-a)%500%100//50
    e=(1000-a)%500%100%50//10
    f=(1000-a)%500%100%50%10//5
    g=(1000-a)%500%100%50%10%5
    print(b+c+d+e+f+g)

Python: a = [500, 100, 50, 10, 5, 1]
while True:
	b = 1000-int(input())
	if b == 1000:
	        break
	c = 0
	for i in a:
		c += b // i
		b %= i 
	print(c)

Python: while True:
    n = int(input())
    if n == 0:
        break
    p = 1000-n
    
    c = 0
    for k in [500, 100, 50, 10, 5, 1]:
        if p == 0:
            break
        c += p // k
        p %= k
        
    print(c)

Python: COINS = [500, 100, 50, 10, 5, 1]

change = 1000 - int(input())
while change != 1000:
    total = 0
    for coin in COINS:
        total += change // coin
        change %= coin

    print(total)

    change = 1000 - int(input())