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: w=[500,100,50,10,5,1]
while True:
    x=1000-int(input())
    if x == 1000 : break
    y=0
    for i in w:
        y=y+x//i
        x=x%i
    print(y)

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

Python: i = 0
while i < 5:
    fee = int(input())
    if fee == 0:
        break
    
    refee = 1000 - fee
    
    C500 = refee     // 500
    C100 = refee%500 // 100
    C50  = refee%100 // 50
    C10  = refee%50  // 10
    C5   = refee%10  // 5
    C1   = refee%5
    
    sum = C500 + C100 + C50 + C10 + C5 + C1
    print(sum)

Python: while True :
    i = int(input())
    a = 0
    if i == 0 :
        break
    else :
        b = 1000 - i
        
        if b >= 500 :
            a += 1
            b -= 500
        
        while b >= 100 :
            a += 1
            b -= 100
        
        while b >= 50 :
            a += 1
            b -= 50
            
        while b >= 10 :
            a += 1
            b -= 10
            
        while b >= 5 :
            a += 1
            b -= 5
            
        a += b   
    
    print(a)

Python: coins = [500, 100, 50, 10, 5, 1]
if __name__ == '__main__':
    p = int(input())
    ch = 1000 - p

    count = 0
    for coin in coins:
        n = ch // coin
        ch -= coin * n
        count += n
    
    print(count)

Python: N=0
for i in range(7):
  N=int(input())
  if N==0:
    break
  N=1000-N
  print(N//500+(N//100)%5+(N//50)%2+(N//10)%5+(N//5)%2+N%5)

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

Python: while True:
    i=int(input())
    if i==0:
        break
    s1=(1000-i)//500
    s2=(1000-i)%500//100
    s3=(1000-i)%500%100//50
    s4=(1000-i)%500%100%50//10
    s5=(1000-i)%500%100%50%10//5
    s6=(1000-i)%500%100%50%10%5//1
    print(s1+s2+s3+s4+s5+s6)

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:
    Y = int(input())
    if Y == 0:
        break
    X = 1000 - Y
    a = X // 500
    X = X - a * 500
    b = X // 100
    X = X - b * 100
    c = X // 50
    X = X - c * 50
    d = X // 10
    X = X - d * 10
    e = X // 5
    X = X - e * 5
    f = X // 1
    
    print(a+b+c+d+e+f)

Python: 
while True:
    x=int(input())
    
    if x==0:
        break
    
    y=1000-x

    m1=y//500
    m2=(y-(500*m1))//100
    m3=((y-(500*m1))-(100*m2))//50
    m4=(((y-(500*m1))-(100*m2))-(50*m3))//10
    m5=((((y-(500*m1))-(100*m2))-(50*m3))-(10*m4))//5
    m6=((((y-(500*m1))-(100*m2))-(50*m3))-(10*m4))-5*m5
    
    z=m1+m2+m3+m4+m5+m6
    
    print(z)

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: for i in range(5):
    a=int(input())
    if a == 0:
        break
    b=1000-a
    if b > 500:
        c=b-(500*(b//500))
        d=c-(100*(c//100))
        e=d-(50*(d//50))
        f=e-(10*(e//10))
        g=f-(5*(f//5))
        print((b//500)+(c//100)+(d//50)+(e//10)+(f//5)+g)
    elif b > 100:
        g=b-(100*(b//100))
        h=g-(50*(g//50))
        i=h-(10*(h//10))
        j=i-(5*(i//5))
        print((b//100)+(g//50)+(h//10)+(i//5)+(j))
    elif b > 50:
        k=b-(50*(b//50))
        l=k-(10*(k//10))
        m=l-(5*(l//5))
        print((b//50)+(k//10)+(l//5)+(m))
    elif b > 10:
        n=b-(10*(b//10))
        o=n-(5*(n//5))
        print((b//10)+(n//5)+(o))
    elif b > 5:
        p=b-(5*(b//5))
        print((b//5)+(p))
    else:
        print(b)

Python: while True:
    x=int(input())
    if x==0:
        break
    a=1000-x
    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
    print(b+d+f+h+j+k)

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

Python: while True:
    a=1000-int(input())
    if a==1000:
        break
    c=[500,100,50,10,5,1]
    ans=0
    for i in c:
        ans+=a//i
        a=a%i
    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):
    a=int(input())
    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
    if a==0:break
    print(b+c+d+e+f+g)

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

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

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

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

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

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

x = 1000 - int(input())

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

ret = 0
for coin in coins:
    n = x//coin
    x = x - n*coin
    ret += n

print(ret)

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