Go: package main

import (
	"fmt"
	"os"
	"strconv"
	"strings"
	"bufio"
)

var (
	sc = bufio.NewScanner(os.Stdin)
	buf = make([]byte, 1000)
)

func scanText() string{
	sc.Scan()
	return sc.Text()
}

func scanTextArray(n int) []string {
	out := make([]string, n)
	for i, v := range(strings.Split(scanText(), "")) {
		out[i] = v
	}
	return out
}

func scanInt() int {
	out, e := strconv.Atoi(scanText())
	if e != nil {
        panic(e)
    }
	return out
}

func scanIntArray(n int) []int {
	out := make([]int, n)
	for i, v := range(strings.Split(scanText(), " ")) {
		num, e := strconv.Atoi(v)
		if e != nil {
			panic(e)
		}
		out[i] = num
	}
	return out
}
//  template end

var (
	coins = [6]int{500, 100, 50, 10, 5, 1}
)

func f(data int) {
	ans := 0
	change := 1000 - data
	for _, v := range coins {
		for ;v <= change; {
			change -= v
			ans++
		}
	}
	fmt.Println(ans)
}

func main() {
	sc.Buffer(buf, 10000)
	for{
		data := scanInt()
		if data == 0 {
			break
		}
		f(data)
	}
}

Python: while True:
    money=int(input())
    
    if money>0:
        oturi=1000-money
        a=oturi//500
        aa=oturi-a*500
        b=aa//100
        bb=aa-b*100
        c=bb//50
        cc=bb-c*50
        d=cc//10
        dd=cc-d*10
        e=dd//5
        ee=dd-e*5
        f=ee
        print(a+b+c+d+e+f)
    
    else:
        break

Python: p=1
x=0
while p==1:
        n=int(input())
        if n==0:
            break
        else:
            r=1000-n
            a=r//500
            r-=500*a
            b=r//100
            r-=100*b
            c=r//50
            r-=50*c
            d=r//10
            r-=10*d
            e=r//5
            r-=5*e
            x=a+b+c+d+e+r
            print(x)

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

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

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

Python: while True:
    n=int(input())
    if n==0:
        break
    else:
        a=1000-n
        i=a%10
        if i>=5:
            i=1+i-5
        else:
            pass
        j=a//10
        j2=j%10
        if j2>=5:
            j2=1+j2-5
        else:
            pass
        h=a//100
        if h>=5:
            h=1+h-5
        else:
            pass
    print(i+j2+h)

Python: while True:
    N=int(input())
    k=1000-N
    f=int(k/500)
    h=int((k%500)/100)
    i=int(((k%500)%100)/50)
    t=int((((k%500)%100)%50)/10)
    v=int(((((k%500)%100)%50)%10)/5)
    o=int((((((k%500)%100)%50)%10)%5)/1)

    if N==0:
        break
    print(f+h+i+t+v+o)

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

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: while True:
    coin=0
    n=int(input())
    if n==0:
        break
    else:
        money=1000-n
        if money//500>=1:
            coin1=int(money//500)
            money-=500*coin1
            coin+=coin1
        else:
            pass
        if money//100>=1:
            coin2=int(money//100)
            money-=100*coin2
            coin+=coin2
        else:
            pass
        if money//50>=1:
            coin3=int(money//50)
            money-=50*coin3
            coin+=coin3
        else:
            pass
        if money//10>=1:
            coin4=int(money//10)
            money-=10*coin4
            coin+=coin4
        else:
            pass
        if money//5>=1:
            coin5=int(money//5)
            money-=5*coin5
            coin+=coin5
        else:
            pass
        if money//1>=1:
            coin6=int(money//1)
            coin+=coin6
        else:
            pass
        
        print(coin)

Python: while True:
    n=int(input())
    if n == 0:
        break
    a=1000-n
    B=a//500
    b=a%500
    C=b//100
    c=b%100
    D=c//50
    d=c%50
    E=d//10
    e=d%10
    F=e//5
    f=e%5
    G=f//1
    print(B+C+D+E+F+G)

Python: list=[]
for i in range(5):
   x=int(input())
   if x==0:
      break
   else:
      list.append(x)

for i in range(len(list)):
   y=list[i]
   a=1000-y
   b=a//500
   c=a%500
   d=c//100
   e=c%100
   f=e//50
   g=e%50
   h=g//10
   i=g%10
   j =i//5
   k=i%5
   sum=b+d+f+h+j+k
   print(str(sum))

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

def count(x, y):
    ret = 0
    while x >= y:
        x -= y
        ret += 1
    return ret

while True:
    n = input()
    if n == 0:
        break
    ans = 0
    n = 1000 - n
    for val in coins:
        n,  t = count(n, val)
        ans += t

    print(ans)

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

Python: for i in range(5):
    e  = int(input())
    if e == 0:
        break
    e500 = (1000 - e)//500
    e100 = (1000 - e)%500//100
    e50 = (1000 - e)%500%100//50
    e10 = (1000 - e)%500%100%50//10
    e5 = (1000 - e)%500%100%50%10//5
    e1 = (1000 - e)%500%100%50%10%5//1
    print(e500+e100+e50+e10+e5+e1)

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

Python: for i in range(5):
    s=0
    q=0
    n=int(input())
    if n==0:
        break
    n=1000-n
    if n>=500:
        n-=500
        s+=1
    if n>=100:
        q=n//100
        n-=100*q
        s+=q
    if n>=50:
        n-=50
        s+=1
    if n>=10:
        q=n//10
        n-=10*q
        s+=q
    if n>=5:
        n-=5
        s+=1
    if n>=1:
        q=n//1
        s+=q
    print(s)

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: for e in iter(input,'0'):
 a,b=0,1000-int(e)
 for i in[500,100,50,10,5]:
  d,b=b//i,b%i
  a+=d
 print(b+a)

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

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: # coding: utf-8
# Your code here!

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

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

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

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